zoukankan      html  css  js  c++  java
  • 和声搜索算法java实现


    public class Demo {

    public static void main(String[] args) {

    HarmonySearch hs = new HarmonySearch();
    hs.setBW(.2);
    hs.setNVAR(5);
    hs.setHMCR(.9);
    hs.setHMS(5);
    hs.setPAR(.4);
    hs.setMaxIteration(100000);

    double low[] = { 2.0, 3.0, 1.0, 1.0, 1.0 };
    double high[] = { 5.0, 6.0, 2.0, 2.0, 2.0 };
    hs.setBounds(low, high);
    hs.mainLoop();

    }

    }

    public class HarmonySearch {

    private int NVAR;
    private int HMS;
    private int maxIter;
    private double PAR;
    private double BW;
    private double HMCR;
    private double low[];
    private double high[];
    private double NCHV[];
    private double bestFitHistory[];
    private double bestHarmony[];
    private double worstFitHistory[];
    private double HM[][];
    static int generation = 0;
    private boolean terminationCriteria = true;

    RandomGenerator randGen = new RandomGenerator(1234);

    public void setMaxIteration(int maxIter) {
    this.maxIter = maxIter;
    }

    public void setNVAR(int NVAR) {
    this.NVAR = NVAR;
    }

    public void setPAR(double PAR) {
    this.PAR = PAR;
    }

    public void setHMCR(double HMCR) {
    this.HMCR = HMCR;
    }

    public void setBW(double BW) {
    this.BW = BW;
    }

    public void setHMS(int HMS) {
    this.HMS = HMS;
    }

    public void setArrays() {

    low = new double[NVAR];
    high = new double[NVAR];
    NCHV = new double[NVAR];
    bestHarmony = new double[NVAR + 1];
    bestFitHistory = new double[maxIter + 1];
    worstFitHistory = new double[maxIter + 1];
    HM = new double[HMS][NVAR + 1];
    }

    public void setBounds(double low[], double high[]) {

    setArrays();
    this.low = low;
    this.high = high;
    }

    public void initiator() {

    int i;
    double curFit;

    for (i = 0; i < HMS; i++) {
    for (int j = 0; j < NVAR; j++) {
    HM[i][j] = randGen.randVal(low[j], high[j]);
    System.out.print(HM[i][j] + " ");
    }
    curFit = fitness(HM[i]);
    HM[i][NVAR] = curFit; // the fitness is stored in the last column
    // of HM
    System.out.print(HM[i][NVAR] + " ");
    System.out.println();
    // updateHarmonyMemory(curFit);
    }
    }

    public double fitness(double x[]) {
    double fit = 0;

    fit = x[1] + x[4] + x[2] + x[0] + x[3];

    return fit;
    }

    public boolean stopCondition() {
    if (generation > maxIter)
    terminationCriteria = false;
    return terminationCriteria;

    }

    public void updateHarmonyMemory(double newFitness) {

    // find worst harmony
    double worst = HM[0][NVAR];
    int worstIndex = 0;
    for (int i = 0; i < HMS; i++)
    if (HM[i][NVAR] > worst) {
    worst = HM[i][NVAR];
    worstIndex = i;
    }
    worstFitHistory[generation] = worst;
    // update harmony
    if (newFitness < worst) {
    for (int k = 0; k < NVAR; k++)
    HM[worstIndex][k] = NCHV[k];
    HM[worstIndex][NVAR] = newFitness;
    }

    // find best harmony
    double best = HM[0][NVAR];
    int bestIndex = 0;
    for (int i = 0; i < HMS; i++)
    if (HM[i][NVAR] < best) {
    best = HM[i][NVAR];
    bestIndex = i;
    }
    bestFitHistory[generation] = best;
    if (generation > 0 && best != bestFitHistory[generation - 1]) {
    for (int k = 0; k < NVAR; k++)
    bestHarmony[k] = HM[bestIndex][k];
    bestHarmony[NVAR] = best;
    }
    }

    private void memoryConsideration(int varIndex) {

    NCHV[varIndex] = HM[randGen.randVal(0, HMS - 1)][varIndex];
    }

    private void pitchAdjustment(int varIndex) {

    double rand = randGen.ran1();
    double temp = NCHV[varIndex];
    if (rand < 0.5) {
    temp += rand * BW;
    if (temp < high[varIndex])
    NCHV[varIndex] = temp;
    } else {
    temp -= rand * BW;
    if (temp > low[varIndex])
    NCHV[varIndex] = temp;
    }

    }

    private void randomSelection(int varIndex) {

    NCHV[varIndex] = randGen.randVal(low[varIndex], high[varIndex]);

    }

    public void mainLoop() {

    long startTime = System.currentTimeMillis();
    initiator();

    while (stopCondition()) {

    for (int i = 0; i < NVAR; i++) {
    if (randGen.ran1() < HMCR) {
    memoryConsideration(i);
    if (randGen.ran1() < PAR)
    pitchAdjustment(i);
    } else
    randomSelection(i);
    }

    double currentFit;
    currentFit = fitness(NCHV);
    updateHarmonyMemory(currentFit);
    generation++;

    }
    long endTime = System.currentTimeMillis();
    System.out.println("Execution time : " + (endTime - startTime) / 1000.0
    + " seconds");
    System.out.println("best :" + bestHarmony[NVAR]);
    for (int i = 0; i < NVAR; i++)
    System.out.print(" " + bestHarmony[i]);
    }

    }

    import java.util.ArrayList;


    public class RandomGenerator {

    private final long IA= 16807;
    private long IM= 2147483647;
    private double AM= (1.0/IM);
    private long IQ= 127773;
    private long IR= 2836;
    private int NTAB= 32;
    private double NDIV= (1+(IM-1)/NTAB);
    private double EPS= 1.2e-7;
    private double RNMX= (1.0-EPS);
    private long iy;
    private long iv[]= new long [NTAB];
    private long idum;


    ArrayList<RandomGenerator> arr = new ArrayList<RandomGenerator>();

    RandomGenerator(){
    sran1((int)System.currentTimeMillis());
    }

    RandomGenerator(int seed){
    sran1(seed);
    }

    public void sran1(int seed){

    int j;
    long k;

    idum = seed;
    if (idum < 1) idum=1;
    for (j=NTAB+7;j>=0;j--) {
    k=(idum)/IQ;
    idum=IA*(idum-k*IQ)-IR*k;
    if (idum < 0) idum += IM;
    if (j < NTAB) iv[j] = idum;
    }
    iy=iv[0];

    }


    public double ran1(){

    int j;
    long k;
    double temp;

    k=(idum)/IQ;
    idum=IA*(idum-k*IQ)-IR*k;
    if (idum < 0) idum += IM;
    j = (int)(iy / NDIV);
    iy=iv[j];
    iv[j] = idum;
    temp=AM*iy;
    if (temp > RNMX) return RNMX;
    else return temp;
    }

    public double randVal(double low, double high){

    return (float)(ran1()*(high-low)+low);
    }


    public int randVal(int low, int high){

    return (int)(Math.floor(ran1()*(high-low)+low+.5));
    }


    }

  • 相关阅读:
    简单了解enum
    PowerDesigner CDM 生成PDM时,外键的命名规则
    HADOOP docker(五):hadoop用户代理 Proxy user
    记一次云安全的安全事件应急响应
    docker学习笔记--重基础使用
    Elasticsearch学习随笔(二)-- Index 和 Doc 查询新建API总结
    Elasticsearch学习随笔(一)--原理理解与5.0核心插件部署过程
    ntopng-一款流量审计框架的安装以及应用
    Suricata规则编写——常用关键字
    浅谈运维中的安全问题-FTP篇
  • 原文地址:https://www.cnblogs.com/altlb/p/6768849.html
Copyright © 2011-2022 走看看