📄 abyss.java
字号:
// Insert the individual into REFSET2 refSet2_.add(individual); // Update distances in REFSET2 for (int j = 0; j < refSet2_.size();j++){ for (int k = 0; k < refSet2_.size();k++){ if (i != j){ double aux = distance_.distanceBetweenSolutions(refSet2_.get(j),refSet2_.get(k)); if (aux < refSet2_.get(j).getDistanceToSolutionSet()){ refSet2_.get(j).setDistanceToSolutionSet(aux); } // if } // if } // for } // for } // for } else { // Update the reference set from the subset generation result Solution individual; for (int i = 0; i < subSet_.size();i++){ individual = (Solution)improvementOperator_.execute(subSet_.get(i)); evaluations_ += improvementOperator_.getEvaluations(); if (refSet1Test(individual)){ //Update distance of RefSet2 for (int indSet2 = 0; indSet2 < refSet2_.size();indSet2++) { double aux = distance_.distanceBetweenSolutions(individual, refSet2_.get(indSet2)); if (aux < refSet2_.get(indSet2).getDistanceToSolutionSet()) { refSet2_.get(indSet2).setDistanceToSolutionSet(aux); } // if } // for } else { refSet2Test(individual); } // if } subSet_.clear(); } } // referenceSetUpdate /** * Tries to update the reference set 2 with a <code>Solution</code> * @param solution The <code>Solution</code> * @return true if the <code>Solution</code> has been inserted, false * otherwise. * @throws JMException */ public boolean refSet2Test(Solution solution) throws JMException{ if (refSet2_.size() < refSet2Size_){ solution.setDistanceToSolutionSet(distance_.distanceToSolutionSet(solution,refSet1_)); double aux = distance_.distanceToSolutionSet(solution,refSet2_); if (aux < solution.getDistanceToSolutionSet()) { solution.setDistanceToSolutionSet(aux); } refSet2_.add(solution); return true; } solution.setDistanceToSolutionSet(distance_.distanceToSolutionSet(solution,refSet1_)); double aux = distance_.distanceToSolutionSet(solution,refSet2_); if (aux < solution.getDistanceToSolutionSet()) { solution.setDistanceToSolutionSet(aux); } double peor = 0.0; int index = 0; for (int i = 0; i < refSet2_.size();i++){ aux = refSet2_.get(i).getDistanceToSolutionSet(); if (aux > peor){ peor = aux; index = i; } } if (solution.getDistanceToSolutionSet() < peor){ refSet2_.remove(index); //Update distances in REFSET2 for (int j = 0; j < refSet2_.size();j++){ aux = distance_.distanceBetweenSolutions(refSet2_.get(j),solution); if (aux < refSet2_.get(j).getDistanceToSolutionSet()){ refSet2_.get(j).setDistanceToSolutionSet(aux); } } solution.unMarked(); refSet2_.add(solution); return true; } return false; } // refSet2Test /** * Tries to update the reference set one with a <code>Solution</code>. * @param solution The <code>Solution</code> * @return true if the <code>Solution</code> has been inserted, false * otherwise. */ public boolean refSet1Test(Solution solution){ boolean dominated = false; int flag; int i = 0; while (i < refSet1_.size()){ flag = dominance_.compare(solution,refSet1_.get(i)); if (flag == -1) { //This is: solution dominates refSet1_.remove(i); } else if (flag == 1) { dominated = true; i++; } else { flag = equal_.compare(solution,refSet1_.get(i)); if (flag == 0) { return true; } // if i++; } // if } // while if (!dominated){ solution.unMarked(); if (refSet1_.size() < refSet1Size_) { //refSet1 isn't full refSet1_.add(solution); } else { archive_.add(solution); } // if } else { return false; } // if return true; } // refSet1Test /** * Implements the subset generation method described in the scatter search * template * @return Number of solutions created by the method * @throws JMException */ public int subSetGeneration() throws JMException{ Solution [] parents = new Solution[2]; Solution [] offSpring; subSet_.clear(); //All pairs from refSet1 for (int i = 0; i < refSet1_.size();i++){ parents[0] = refSet1_.get(i); for (int j = i+1; j < refSet1_.size();j++){ parents[1] = refSet1_.get(j); if (!parents[0].isMarked() || !parents[1].isMarked()){ //offSpring = parent1.crossover(1.0,parent2); offSpring = (Solution [])crossoverOperator_.execute(parents); problem_.evaluateConstraints(offSpring[0]); problem_.evaluateConstraints(offSpring[1]); problem_.evaluate(offSpring[0]); problem_.evaluate(offSpring[1]); evaluations_ += 2; if (evaluations_ < maxEvaluations){ subSet_.add(offSpring[0]); subSet_.add(offSpring[1]); } parents[0].marked(); parents[1].marked(); } } } // All pairs from refSet2 for (int i = 0; i < refSet2_.size();i++){ parents[0] = refSet2_.get(i); for (int j = i+1; j < refSet2_.size();j++){ parents[1] = refSet2_.get(j); if (!parents[0].isMarked() || !parents[1].isMarked()){ //offSpring = parents[0].crossover(1.0,parent2); offSpring = (Solution []) crossoverOperator_.execute(parents); problem_.evaluateConstraints(offSpring[0]); problem_.evaluateConstraints(offSpring[1]); problem_.evaluate(offSpring[0]); problem_.evaluate(offSpring[1]); evaluations_+=2; if (evaluations_ < maxEvaluations){ subSet_.add(offSpring[0]); subSet_.add(offSpring[1]); } parents[0].marked(); parents[1].marked(); } } } return subSet_.size(); } // subSetGeneration /** * Runs of the AbYSS algorithm. * @return a <code>SolutionSet</code> that is a set of non dominated solutions * as a result of the algorithm execution * @throws JMException */ public SolutionSet execute() throws JMException { // STEP 1. Initialize parameters initParam(); // STEP 2. Build the initial solutionSet Solution solution; for (int i = 0; i < solutionSetSize_; i++) { solution = diversificationGeneration(); problem_.evaluateConstraints(solution); problem_.evaluate(solution); evaluations_++; solution = (Solution)improvementOperator_.execute(solution); evaluations_ += improvementOperator_.getEvaluations(); solutionSet_.add(solution); } // fpr // STEP 3. Main loop int newSolutions = 0; while (evaluations_ < maxEvaluations) { referenceSetUpdate(true); newSolutions = subSetGeneration(); while (newSolutions > 0) { // New solutions are created referenceSetUpdate(false); if (evaluations_ >= maxEvaluations) return archive_; newSolutions = subSetGeneration(); } // while // RE-START if (evaluations_ < maxEvaluations){ solutionSet_.clear(); // Add refSet1 to SolutionSet for (int i = 0; i < refSet1_.size();i++){ solution = refSet1_.get(i); solution.unMarked(); solution = (Solution)improvementOperator_.execute(solution); evaluations_ += improvementOperator_.getEvaluations(); solutionSet_.add(solution); } // Remove refSet1 and refSet2 refSet1_.clear(); refSet2_.clear(); // Sort the archive and insert the best solutions distance_.crowdingDistanceAssignment(archive_, problem_.getNumberOfObjectives()); archive_.sort(crowdingDistance_); int insert = solutionSetSize_ / 2; if (insert > archive_.size()) insert = archive_.size(); if (insert > (solutionSetSize_ - solutionSet_.size())) insert = solutionSetSize_ - solutionSet_.size(); // Insert solutions for (int i = 0; i < insert; i++){ solution = new Solution(archive_.get(i)); //solution = improvement(solution); solution.unMarked(); solutionSet_.add(solution); } // Create the rest of solutions randomly while (solutionSet_.size() < solutionSetSize_){ solution = diversificationGeneration(); problem_.evaluateConstraints(solution); problem_.evaluate(solution); evaluations_++; solution = (Solution)improvementOperator_.execute(solution); evaluations_ += improvementOperator_.getEvaluations(); solution.unMarked(); solutionSet_.add(solution); } // while } // if } // while // STEP 4. Return the archive return archive_; } // execute} // AbYSS
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -