Kaydet (Commit) 51387dc2 authored tarafından Todor Balabanov's avatar Todor Balabanov Kaydeden (comit) Julien Nabet

Formatting - Eclipse IDE Java Conventions with spaces for indentation.

Change-Id: I0c3e50ef25bda0bc4ae59665a07848fe75507121
Reviewed-on: https://gerrit.libreoffice.org/72185Reviewed-by: 's avatarJulien Nabet <serval2412@yahoo.fr>
Tested-by: Jenkins
üst c5338e3a
......@@ -44,25 +44,25 @@ import net.adaptivebox.space.BasicPoint;
public class DEPSAgent implements ILibEngine {
//Describes the problem to be solved
// Describes the problem to be solved
private ProblemEncoder problemEncoder;
//Forms the goodness landscape
// Forms the goodness landscape
private IGoodnessCompareEngine qualityComparator;
//store the point that generated in current learning cycle
// store the point that generated in current learning cycle
private SearchPoint trailPoint;
//temp variable
// temp variable
private AbsGTBehavior selectGTBehavior;
//the own memory: store the point that generated in old learning cycle
// the own memory: store the point that generated in old learning cycle
private BasicPoint pold_t;
//the own memory: store the point that generated in last learning cycle
// the own memory: store the point that generated in last learning cycle
private BasicPoint pcurrent_t;
//the own memory: store the personal best point
// the own memory: store the personal best point
private SearchPoint pbest_t;
//Generate-and-test Behaviors
// Generate-and-test Behaviors
private DEGTBehavior deGTBehavior;
private PSGTBehavior psGTBehavior;
public double switchP = 0.5;
......@@ -88,7 +88,7 @@ public class DEPSAgent implements ILibEngine {
}
private AbsGTBehavior getGTBehavior() {
if (Math.random()<switchP) {
if (Math.random() < switchP) {
return deGTBehavior;
} else {
return psGTBehavior;
......@@ -97,23 +97,23 @@ public class DEPSAgent implements ILibEngine {
public void setGTBehavior(AbsGTBehavior gtBehavior) {
if (gtBehavior instanceof DEGTBehavior) {
deGTBehavior = ((DEGTBehavior)gtBehavior);
deGTBehavior = ((DEGTBehavior) gtBehavior);
deGTBehavior.setPbest(pbest_t);
return;
}
if (gtBehavior instanceof PSGTBehavior) {
psGTBehavior = ((PSGTBehavior)gtBehavior);
psGTBehavior = ((PSGTBehavior) gtBehavior);
psGTBehavior.setMemPoints(pbest_t, pcurrent_t, pold_t);
return;
}
}
public void generatePoint() {
// generates a new point in the search space (S) based on
// its memory and the library
// generates a new point in the search space (S) based on
// its memory and the library
selectGTBehavior = this.getGTBehavior();
selectGTBehavior.generateBehavior(trailPoint, problemEncoder);
//evaluate into goodness information
// evaluate into goodness information
problemEncoder.evaluate(trailPoint);
}
......@@ -125,4 +125,3 @@ public class DEPSAgent implements ILibEngine {
return trailPoint;
}
}
......@@ -23,7 +23,7 @@ import net.adaptivebox.knowledge.SearchPoint;
import net.adaptivebox.problem.ProblemEncoder;
abstract public class AbsGTBehavior {
//The referred social library
// The referred social library
protected Library socialLib;
public void setLibrary(Library lib) {
......@@ -34,4 +34,3 @@ abstract public class AbsGTBehavior {
abstract public void testBehavior(SearchPoint trailPoint, IGoodnessCompareEngine qualityComparator);
}
......@@ -37,11 +37,11 @@ import net.adaptivebox.problem.ProblemEncoder;
import net.adaptivebox.space.BasicPoint;
public class DEGTBehavior extends AbsGTBehavior implements ILibEngine {
private static final int DVNum = 2; //Number of differential vectors, normally be 1 or 2
public double FACTOR = 0.5; //scale constant: (0, 1.2], normally be 0.5
public double CR = 0.9; //crossover constant: [0, 1], normally be 0.1 or 0.9
private static final int DVNum = 2; // Number of differential vectors, normally be 1 or 2
public double FACTOR = 0.5; // scale constant: (0, 1.2], normally be 0.5
public double CR = 0.9; // crossover constant: [0, 1], normally be 0.1 or 0.9
//the own memory: store the point that generated in last learning cycle
// the own memory: store the point that generated in last learning cycle
private SearchPoint pbest_t;
public void setPbest(SearchPoint pbest) {
......@@ -51,16 +51,14 @@ public class DEGTBehavior extends AbsGTBehavior implements ILibEngine {
/**
* Crossover and mutation for a single vector element done in a single step.
*
* @param index
* Index of the trial vector element to be changed.
* @param trialVector
* Trial vector reference.
* @param globalVector
* Global best found vector reference.
* @param differenceVectors
* List of vectors used for difference delta calculation.
* @param index Index of the trial vector element to be changed.
* @param trialVector Trial vector reference.
* @param globalVector Global best found vector reference.
* @param differenceVectors List of vectors used for difference delta
* calculation.
*/
private void crossoverAndMutation(int index, double trialVector[], double globalVector[], BasicPoint differenceVectors[]) {
private void crossoverAndMutation(int index, double trialVector[], double globalVector[],
BasicPoint differenceVectors[]) {
double delta = 0D;
for (int i = 0; i < differenceVectors.length; i++) {
......@@ -110,11 +108,10 @@ public class DEGTBehavior extends AbsGTBehavior implements ILibEngine {
}
private SearchPoint[] getReferPoints() {
SearchPoint[] referPoints = new SearchPoint[DVNum*2];
for(int i=0; i<referPoints.length; i++) {
referPoints[i] = socialLib.getSelectedPoint(RandomGenerator.intRangeRandom(0, socialLib.getPopSize()-1));
SearchPoint[] referPoints = new SearchPoint[DVNum * 2];
for (int i = 0; i < referPoints.length; i++) {
referPoints[i] = socialLib.getSelectedPoint(RandomGenerator.intRangeRandom(0, socialLib.getPopSize() - 1));
}
return referPoints;
}
}
......@@ -63,20 +63,22 @@ import net.adaptivebox.space.BasicPoint;
import net.adaptivebox.space.DesignSpace;
public class PSGTBehavior extends AbsGTBehavior {
// Two normally choices for (c1, c2, weight), i.e., (2, 2, 0.4), or (1.494, 1.494, 0.729)
// The first is used in dissipative PSO (cf. [4]) as CL>0, and the second is achieved by using
// Two normally choices for (c1, c2, weight), i.e., (2, 2, 0.4), or (1.494,
// 1.494, 0.729)
// The first is used in dissipative PSO (cf. [4]) as CL>0, and the second is
// achieved by using
// constriction factors (cf. [3])
public double c1=2;
public double c2=2;
public double weight = 0.4; //inertia weight
public double c1 = 2;
public double c2 = 2;
public double weight = 0.4; // inertia weight
public double CL=0; //See ref[4], normally be 0.001~0.005
public double CL = 0; // See ref[4], normally be 0.001~0.005
//the own memory: store the point that generated in old learning cycle
// the own memory: store the point that generated in old learning cycle
private BasicPoint pold_t;
//the own memory: store the point that generated in last learning cycle
// the own memory: store the point that generated in last learning cycle
private BasicPoint pcurrent_t;
//the own memory: store the personal best point
// the own memory: store the personal best point
private SearchPoint pbest_t;
public void setMemPoints(SearchPoint pbest, BasicPoint pcurrent, BasicPoint pold) {
......@@ -91,21 +93,21 @@ public class PSGTBehavior extends AbsGTBehavior {
DesignSpace designSpace = problemEncoder.getDesignSpace();
int DIMENSION = designSpace.getDimension();
double deltaxb, deltaxbm;
for (int b=0;b<DIMENSION;b++) {
if (Math.random()<CL) {
for (int b = 0; b < DIMENSION; b++) {
if (Math.random() < CL) {
designSpace.mutationAt(trailPoint.getLocation(), b);
} else {
deltaxb = weight*(pcurrent_t.getLocation()[b]-pold_t.getLocation()[b])
+ c1*Math.random()*(pbest_t.getLocation()[b]-pcurrent_t.getLocation()[b])
+ c2*Math.random()*(gbest_t.getLocation()[b]-pcurrent_t.getLocation()[b]);
//limitation for delta_x
deltaxbm = 0.5*designSpace.getMagnitudeIn(b);
if(deltaxb<-deltaxbm) {
deltaxb = weight * (pcurrent_t.getLocation()[b] - pold_t.getLocation()[b])
+ c1 * Math.random() * (pbest_t.getLocation()[b] - pcurrent_t.getLocation()[b])
+ c2 * Math.random() * (gbest_t.getLocation()[b] - pcurrent_t.getLocation()[b]);
// limitation for delta_x
deltaxbm = 0.5 * designSpace.getMagnitudeIn(b);
if (deltaxb < -deltaxbm) {
deltaxb = -deltaxbm;
} else if (deltaxb>deltaxbm) {
} else if (deltaxb > deltaxbm) {
deltaxb = deltaxbm;
}
trailPoint.getLocation()[b] = pcurrent_t.getLocation()[b]+deltaxb;
trailPoint.getLocation()[b] = pcurrent_t.getLocation()[b] + deltaxb;
}
}
}
......@@ -115,7 +117,6 @@ public class PSGTBehavior extends AbsGTBehavior {
Library.replace(qualityComparator, trailPoint, pbest_t);
pold_t.importLocation(pcurrent_t);
pcurrent_t.importLocation(trailPoint);
}
}
}
......@@ -24,7 +24,7 @@ import net.adaptivebox.global.BasicBound;
public class EvalElement {
//The weight for each response (target)
// The weight for each response (target)
private static final double weight = 1;
/**
* The expected range of the response value, forms the following objective:
......@@ -44,26 +44,25 @@ public class EvalElement {
public BasicBound targetBound = new BasicBound();
public boolean isOptType() {
return ((targetBound.minValue==BasicBound.MINDOUBLE&&targetBound.maxValue==BasicBound.MINDOUBLE)||
(targetBound.minValue==BasicBound.MAXDOUBLE&&targetBound.maxValue==BasicBound.MAXDOUBLE));
return ((targetBound.minValue == BasicBound.MINDOUBLE && targetBound.maxValue == BasicBound.MINDOUBLE)
|| (targetBound.minValue == BasicBound.MAXDOUBLE && targetBound.maxValue == BasicBound.MAXDOUBLE));
}
public double evaluateCONS(double targetValue) {
if(targetValue<targetBound.minValue) {
return weight*(targetBound.minValue-targetValue);
if (targetValue < targetBound.minValue) {
return weight * (targetBound.minValue - targetValue);
}
if(targetValue>targetBound.maxValue) {
return weight*(targetValue-targetBound.maxValue);
if (targetValue > targetBound.maxValue) {
return weight * (targetValue - targetBound.maxValue);
}
return 0;
}
public double evaluateOPTIM(double targetValue) {
if(targetBound.maxValue==BasicBound.MINDOUBLE) { //min mode
return weight*targetValue;
} else { //max
return -weight*targetValue;
if (targetBound.maxValue == BasicBound.MINDOUBLE) { // min mode
return weight * targetValue;
} else { // max
return -weight * targetValue;
}
}
}
......@@ -37,21 +37,20 @@ public class EvalStruct {
evalElems[index] = dim;
}
//convert response values into encoded information double[2]
// convert response values into encoded information double[2]
public void evaluate(double[] evalRes, double[] targetValues) {
evalRes[0] = evalRes[1] = 0;
for(int i=0; i<evalElems.length; i++) {
for (int i = 0; i < evalElems.length; i++) {
if (evalElems[i].isOptType()) {
//The objectives (OPTIM type)
//The multi-objective will be translated into single-objective
// The objectives (OPTIM type)
// The multi-objective will be translated into single-objective
evalRes[1] += evalElems[i].evaluateOPTIM(targetValues[i]);
} else {
//The constraints (CONS type)
//If evalRes[0] equals to 0, then be a feasible point, i.e. satisfies
// all the constraints
// The constraints (CONS type)
// If evalRes[0] equals to 0, then be a feasible point, i.e. satisfies
// all the constraints
evalRes[0] += evalElems[i].evaluateCONS(targetValues[i]);
}
}
}
}
......@@ -19,6 +19,6 @@
package net.adaptivebox.encode;
public interface IEncodeEngine{
public interface IEncodeEngine {
double[] getEncodeInfo();
}
......@@ -20,11 +20,12 @@
package net.adaptivebox.global;
public class BasicBound {
public static final double MINDOUBLE= -1e308;
public static final double MAXDOUBLE= 1e308;
public static final double MINDOUBLE = -1e308;
public static final double MAXDOUBLE = 1e308;
public double minValue = MINDOUBLE;
public double maxValue = MAXDOUBLE;
public BasicBound() {
}
......@@ -34,11 +35,11 @@ public class BasicBound {
}
public double getLength() {
return Math.abs(maxValue-minValue);
return Math.abs(maxValue - minValue);
}
public double boundAdjust(double value){
if(value > maxValue) {
public double boundAdjust(double value) {
if (value > maxValue) {
value = maxValue;
} else if (value < minValue) {
value = minValue;
......@@ -46,20 +47,18 @@ public class BasicBound {
return value;
}
public double annulusAdjust (double value) {
if(value > maxValue) {
double extendsLen = (value-maxValue)%getLength();
value = minValue+extendsLen;
public double annulusAdjust(double value) {
if (value > maxValue) {
double extendsLen = (value - maxValue) % getLength();
value = minValue + extendsLen;
} else if (value < minValue) {
double extendsLen = (minValue-value)%getLength();
value = maxValue-extendsLen;
double extendsLen = (minValue - value) % getLength();
value = maxValue - extendsLen;
}
return value;
}
public double getRandomValue(){
public double getRandomValue() {
return RandomGenerator.doubleRangeRandom(minValue, maxValue);
}
}
......@@ -25,63 +25,60 @@ package net.adaptivebox.global;
import java.util.Random;
public class RandomGenerator {
/**
* Pseudo-random number generator instance.
*/
private static Random PRNG = new Random();
/**
* Pseudo-random number generator instance.
*/
private static Random PRNG = new Random();
/**
* This function returns a random integer number between the lowLimit and
* upLimit.
*
* @param lowLimit
* lower limits upLimit The upper limits (between which the
* random number is to be generated)
* @return int return value Example: for find [0,1,2]
*/
public static int intRangeRandom(int lowLimit, int upLimit) {
int num = lowLimit + PRNG.nextInt(upLimit - lowLimit + 1);
return num;
}
/**
* This function returns a random float number between the lowLimit and
* upLimit.
*
* @param lowLimit
* lower limits upLimit The upper limits (between which the
* random number is to be generated)
* @return double return value
*/
public static double doubleRangeRandom(double lowLimit, double upLimit) {
double num = lowLimit + PRNG.nextDouble() * (upLimit - lowLimit);
return num;
}
/**
* This function returns a random integer number between the lowLimit and
* upLimit.
*
* @param lowLimit lower limits upLimit The upper limits (between which the
* random number is to be generated)
* @return int return value Example: for find [0,1,2]
*/
public static int intRangeRandom(int lowLimit, int upLimit) {
int num = lowLimit + PRNG.nextInt(upLimit - lowLimit + 1);
return num;
}
public static int[] randomSelection(int maxNum, int times) {
if (maxNum < 0) {
maxNum = 0;
}
/**
* This function returns a random float number between the lowLimit and upLimit.
*
* @param lowLimit lower limits upLimit The upper limits (between which the
* random number is to be generated)
* @return double return value
*/
public static double doubleRangeRandom(double lowLimit, double upLimit) {
double num = lowLimit + PRNG.nextDouble() * (upLimit - lowLimit);
return num;
}
if (times < 0) {
times = 0;
}
public static int[] randomSelection(int maxNum, int times) {
if (maxNum < 0) {
maxNum = 0;
}
int[] all = new int[maxNum];
for (int i = 0; i < all.length; i++) {
all[i] = i;
}
if (times < 0) {
times = 0;
}
/* https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle */
int[] indices = new int[Math.min(maxNum, times)];
for (int i = 0, j, value; i < indices.length; i++) {
j = intRangeRandom(i, all.length - 1);
int[] all = new int[maxNum];
for (int i = 0; i < all.length; i++) {
all[i] = i;
}
value = all[j];
all[j] = all[i];
indices[i] = all[i] = value;
}
/* https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle */
int[] indices = new int[Math.min(maxNum, times)];
for (int i = 0, j, value; i < indices.length; i++) {
j = intRangeRandom(i, all.length - 1);
return indices;
value = all[j];
all[j] = all[i];
indices[i] = all[i] = value;
}
return indices;
}
}
......@@ -51,7 +51,8 @@ public class ACRComparator implements IGoodnessCompareEngine, IUpdateCycleEngine
public ACRComparator(Library lib, int T) {
socialPool = lib;
this.T = T;
//set the (epsilon_t|t=0) as the maximum CONS value among the SearchPoints in the library
// set the (epsilon_t|t=0) as the maximum CONS value among the SearchPoints in
// the library
epsilon_t = lib.getExtremalVcon(true);
}
......@@ -65,7 +66,7 @@ public class ACRComparator implements IGoodnessCompareEngine, IUpdateCycleEngine
}
public int compare(double[] fit1, double[] fit2) {
if(Math.max(fit1[0], fit2[0])<=Math.max(0, epsilon_t)) { //epsilon>0
if (Math.max(fit1[0], fit2[0]) <= Math.max(0, epsilon_t)) { // epsilon>0
return compare(fit1[1], fit2[1]);
} else {
return compare(fit1[0], fit2[0]);
......@@ -73,16 +74,16 @@ public class ACRComparator implements IGoodnessCompareEngine, IUpdateCycleEngine
}
public void updateCycle(int t) {
//calculates the ratio
double rn = (double)socialPool.getVconThanNum(epsilon_t)/(double)socialPool.getPopSize();
if(t>TthR*T &&T!=-1) { //Forcing sub-rule
// calculates the ratio
double rn = (double) socialPool.getVconThanNum(epsilon_t) / (double) socialPool.getPopSize();
if (t > TthR * T && T != -1) { // Forcing sub-rule
epsilon_t *= BETAF;
} else { //Ratio-keeping sub-rules
if(rn>RU) {
epsilon_t *= BETAL; //Shrink
} else { // Ratio-keeping sub-rules
if (rn > RU) {
epsilon_t *= BETAL; // Shrink
}
if(rn<RL) {
epsilon_t *= BETAU; //Relax
if (rn < RL) {
epsilon_t *= BETAU; // Relax
}
}
}
......
......@@ -28,17 +28,18 @@ package net.adaptivebox.goodness;
public class BCHComparator implements IGoodnessCompareEngine {
/* check the magnitude of two array, the frontal is more important
**/
/*
* check the magnitude of two array, the frontal is more important
**/
private static int compareArray(double[] fit1, double[] fit2) {
for (int i=0; i<fit1.length; i++) {
if (fit1[i]>fit2[i]) {
return LARGER_THAN; //Large than
} else if (fit1[i]<fit2[i]){
return LESS_THAN; //Less than
for (int i = 0; i < fit1.length; i++) {
if (fit1[i] > fit2[i]) {
return LARGER_THAN; // Large than
} else if (fit1[i] < fit2[i]) {
return LESS_THAN; // Less than
}
}
return IGoodnessCompareEngine.EQUAL_TO; //same
return IGoodnessCompareEngine.EQUAL_TO; // same
}
public int compare(double[] fit1, double[] fit2) {
......
......@@ -29,10 +29,9 @@ public abstract interface IGoodnessCompareEngine {
int LESS_THAN = 0;
/**
* check the magnitude of two IEncodeEngine
* LARGER_THAN: goodness1 is worse than goodness2
* LESS_THAN: goodness1 is better than goodness2
* EQUAL_TO : goodness1 is equal to goodness2
* check the magnitude of two IEncodeEngine LARGER_THAN: goodness1 is worse than
* goodness2 LESS_THAN: goodness1 is better than goodness2 EQUAL_TO : goodness1
* is equal to goodness2
**/
int compare(double[] goodness1, double[] goodness2);
}
......@@ -25,6 +25,3 @@ package net.adaptivebox.knowledge;
public interface ILibEngine {
void setLibrary(Library lib);
}
......@@ -33,9 +33,9 @@ public class Library {
private final SearchPoint[] libPoints;
private int gIndex = -1;
public Library(int number, ProblemEncoder problemEncoder){
public Library(int number, ProblemEncoder problemEncoder) {
libPoints = new SearchPoint[number];
for (int i=0; i<number; i++) {
for (int i = 0; i < number; i++) {
libPoints[i] = problemEncoder.getEncodedSearchPoint();
}
}
......@@ -45,7 +45,7 @@ public class Library {
}
public void refreshGbest(IGoodnessCompareEngine qualityComparator) {
gIndex = tournamentSelection(qualityComparator, getPopSize()-1, true);
gIndex = tournamentSelection(qualityComparator, getPopSize() - 1, true);
}
public int getPopSize() {
......@@ -56,9 +56,11 @@ public class Library {
return libPoints[index];
}
public static boolean replace(IGoodnessCompareEngine comparator, SearchPoint outPoint, SearchPoint tobeReplacedPoint) {
public static boolean replace(IGoodnessCompareEngine comparator, SearchPoint outPoint,
SearchPoint tobeReplacedPoint) {
boolean isBetter = false;
if(comparator.compare(outPoint.getEncodeInfo(), tobeReplacedPoint.getEncodeInfo())<IGoodnessCompareEngine.LARGER_THAN) {
if (comparator.compare(outPoint.getEncodeInfo(),
tobeReplacedPoint.getEncodeInfo()) < IGoodnessCompareEngine.LARGER_THAN) {
tobeReplacedPoint.importPoint(outPoint);
isBetter = true;
}
......@@ -68,9 +70,10 @@ public class Library {
public int tournamentSelection(IGoodnessCompareEngine comparator, int times, boolean isBetter) {
int[] indices = RandomGenerator.randomSelection(getPopSize(), times);
int currentIndex = indices[0];
for (int i=1; i<indices.length; i++) {
int compareValue = comparator.compare(libPoints[indices[i]].getEncodeInfo(), libPoints[currentIndex].getEncodeInfo());
if (isBetter == (compareValue<IGoodnessCompareEngine.LARGER_THAN)) {
for (int i = 1; i < indices.length; i++) {
int compareValue = comparator.compare(libPoints[indices[i]].getEncodeInfo(),
libPoints[currentIndex].getEncodeInfo());
if (isBetter == (compareValue < IGoodnessCompareEngine.LARGER_THAN)) {
currentIndex = indices[i];
}
}
......@@ -78,9 +81,9 @@ public class Library {
}
public double getExtremalVcon(boolean isMAX) {
double val=BasicBound.MINDOUBLE;
for(int i=0; i<libPoints.length; i++) {
if(libPoints[i].getEncodeInfo()[0]>val==isMAX) {
double val = BasicBound.MINDOUBLE;
for (int i = 0; i < libPoints.length; i++) {
if (libPoints[i].getEncodeInfo()[0] > val == isMAX) {
val = libPoints[i].getEncodeInfo()[0];
}
}
......@@ -88,9 +91,9 @@ public class Library {
}
public int getVconThanNum(double allowedCons) {
int num=0;
for(int i=0; i<libPoints.length; i++) {
if(libPoints[i].getEncodeInfo()[0]<=allowedCons) {
int num = 0;
for (int i = 0; i < libPoints.length; i++) {
if (libPoints[i].getEncodeInfo()[0] <= allowedCons) {
num++;
}
}
......@@ -98,6 +101,3 @@ public class Library {
}
}
......@@ -24,15 +24,16 @@ import net.adaptivebox.global.BasicBound;
import net.adaptivebox.space.BasicPoint;
public class SearchPoint extends BasicPoint implements IEncodeEngine {
//store the encode information for goodness evaluation
//encodeInfo[0]: the sum of constraints (if it equals to 0, then be a feasible point)
//encodeInfo[1]: the value of objective function
// store the encode information for goodness evaluation
// encodeInfo[0]: the sum of constraints (if it equals to 0, then be a feasible
// point)
// encodeInfo[1]: the value of objective function
private final double[] encodeInfo = new double[2];
private double objectiveValue;
public SearchPoint(int dim) {
super(dim);
for(int i=0; i<encodeInfo.length; i++) {
for (int i = 0; i < encodeInfo.length; i++) {
encodeInfo[i] = BasicBound.MAXDOUBLE;
}
}
......@@ -49,7 +50,7 @@ public class SearchPoint extends BasicPoint implements IEncodeEngine {
importEncodeInfo(point.getEncodeInfo());
}
//Replace self by given point
// Replace self by given point
public void importPoint(SearchPoint point) {
importLocation(point);
importEncodeInfo(point);
......@@ -57,15 +58,15 @@ public class SearchPoint extends BasicPoint implements IEncodeEngine {
}
public double getObjectiveValue() {
return objectiveValue;
return objectiveValue;
}
public void setObjectiveValue(double objectiveValue) {
this.objectiveValue = objectiveValue;
this.objectiveValue = objectiveValue;
}
public boolean isFeasible() {
return encodeInfo[0] == 0; //no constraint violations
return encodeInfo[0] == 0; // no constraint violations
}
}
\ No newline at end of file
......@@ -34,11 +34,11 @@ import net.adaptivebox.space.DesignDim;
import net.adaptivebox.space.DesignSpace;
public abstract class ProblemEncoder {
//Store the calculated results for the responses
private final double[] tempResponseSet; //temp values
private final double[] tempLocation; //temp values
// Store the calculated results for the responses
private final double[] tempResponseSet; // temp values
private final double[] tempLocation; // temp values
//the search space (S)
// the search space (S)
private final DesignSpace designSpace;
// For evaluate the response vector into encoded vector double[2]
......@@ -55,27 +55,27 @@ public abstract class ProblemEncoder {
return designSpace;
}
//set the default information for each dimension of search space (S)
protected void setDefaultXAt(int i, double min, double max, double grain) {
// set the default information for each dimension of search space (S)
protected void setDefaultXAt(int i, double min, double max, double grain) {
DesignDim dd = new DesignDim();
dd.grain = grain;
dd.paramBound = new BasicBound(min, max);
designSpace.setElemAt(dd, i);
}
//set the default information for evaluation each response
protected void setDefaultYAt(int i, double min, double max) {
// set the default information for evaluation each response
protected void setDefaultYAt(int i, double min, double max) {
EvalElement ee = new EvalElement();
ee.targetBound = new BasicBound(min, max);
evalStruct.setElemAt(ee, i);
}
//get a fresh point
// get a fresh point
public SearchPoint getFreshSearchPoint() {
return new SearchPoint(designSpace.getDimension());
}
//get an encoded point
// get an encoded point
public SearchPoint getEncodedSearchPoint() {
SearchPoint point = getFreshSearchPoint();
designSpace.initializeGene(point.getLocation());
......@@ -83,26 +83,25 @@ public abstract class ProblemEncoder {
return point;
}
//evaluate the point into encoded information
// evaluate the point into encoded information
public void evaluate(SearchPoint point) {
//copy to temp point
// copy to temp point
System.arraycopy(point.getLocation(), 0, this.tempLocation, 0, tempLocation.length);
//mapping the temp point to original search space S
// mapping the temp point to original search space S
designSpace.getMappingPoint(tempLocation);
//calculate based on the temp point
// calculate based on the temp point
calcTargets(tempResponseSet, tempLocation);
evalStruct.evaluate(point.getEncodeInfo(), tempResponseSet);
point.setObjectiveValue(tempResponseSet[0]);
}
//calculate each response, must be implemented
// calculate each response, must be implemented
abstract protected double calcTargetAt(int index, double[] VX);
// calculate all the responses VY[] based on given point VX[]
private void calcTargets(double[] VY, double[] VX) {
for(int i=0; i<VY.length; i++) {
for (int i = 0; i < VY.length; i++) {
VY[i] = calcTargetAt(i, VX);
}
}
}
......@@ -43,21 +43,23 @@ import net.adaptivebox.knowledge.SearchPoint;
public class SCAgent {
//Describes the problem to be solved (encode the point into intermediate information)
// Describes the problem to be solved (encode the point into intermediate
// information)
private ProblemEncoder problemEncoder;
//Forms the goodness landscape
// Forms the goodness landscape
private IGoodnessCompareEngine specComparator;
//the coefficients of SCAgent
// the coefficients of SCAgent
private static final int TaoB = 2;
//The early version set TaoW as the size of external library (NL), but 4 is often enough
// The early version set TaoW as the size of external library (NL), but 4 is
// often enough
private static final int TaoW = 4;
//The referred external library
// The referred external library
private Library externalLib;
//store the point that generated in current learning cycle
// store the point that generated in current learning cycle
private SearchPoint trailPoint;
//the own memory: store the point that generated in last learning cycle
// the own memory: store the point that generated in last learning cycle
private SearchPoint pcurrent_t;
public void setExternalLib(Library lib) {
......@@ -75,9 +77,9 @@ public class SCAgent {
}
public SearchPoint generatePoint() {
//generate a new point
// generate a new point
generatePoint(trailPoint);
//evaluate the generated point
// evaluate the generated point
problemEncoder.evaluate(trailPoint);
return trailPoint;
}
......@@ -85,49 +87,50 @@ public class SCAgent {
private void generatePoint(ILocationEngine tempPoint) {
SearchPoint Xmodel, Xrefer, libBPoint;
// choose Selects a better point (libBPoint) from externalLib (L) based
// on tournament selection
// choose Selects a better point (libBPoint) from externalLib (L) based
// on tournament selection
int xb = externalLib.tournamentSelection(specComparator, TaoB, true);
libBPoint = externalLib.getSelectedPoint(xb);
// Compares pcurrent_t with libBPoint
// The better one becomes model point (Xmodel)
// The worse one becomes refer point (Xrefer)
if(specComparator.compare(pcurrent_t.getEncodeInfo(), libBPoint.getEncodeInfo())==IGoodnessCompareEngine.LARGER_THAN) {
// Compares pcurrent_t with libBPoint
// The better one becomes model point (Xmodel)
// The worse one becomes refer point (Xrefer)
if (specComparator.compare(pcurrent_t.getEncodeInfo(),
libBPoint.getEncodeInfo()) == IGoodnessCompareEngine.LARGER_THAN) {
Xmodel = libBPoint;
Xrefer = pcurrent_t;
} else {
Xmodel = pcurrent_t;
Xrefer = libBPoint;
}
// observational learning: generates a new point near the model point, which
// the variation range is decided by the difference of Xmodel and Xrefer
// observational learning: generates a new point near the model point, which
// the variation range is decided by the difference of Xmodel and Xrefer
inferPoint(tempPoint, Xmodel, Xrefer, problemEncoder.getDesignSpace());
}
//1. Update the current point into the external library
//2. Replace the current point by the generated point
// 1. Update the current point into the external library
// 2. Replace the current point by the generated point
public void updateInfo() {
//Selects a bad point kw from TaoW points in Library
// Selects a bad point kw from TaoW points in Library
int xw = externalLib.tournamentSelection(specComparator, TaoW, false);
//Replaces kw with pcurrent_t
// Replaces kw with pcurrent_t
externalLib.getSelectedPoint(xw).importPoint(pcurrent_t);
//Replaces pcurrent_t (x(t)) with trailPoint (x(t+1))
// Replaces pcurrent_t (x(t)) with trailPoint (x(t+1))
pcurrent_t.importPoint(trailPoint);
}
// 1---model point, 2---refer point
private boolean inferPoint(ILocationEngine newPoint, ILocationEngine point1,ILocationEngine point2, DesignSpace space){
double[] newLoc = newPoint.getLocation();
double[] real1 = point1.getLocation();
double[] real2 = point2.getLocation();
}
for (int i=0; i<newLoc.length; i++) {
newLoc[i] = real1[i]*2-real2[i];
//boundary handling
newLoc[i] = space.boundAdjustAt(newLoc[i], i);
newLoc[i] = RandomGenerator.doubleRangeRandom(newLoc[i], real2[i]);
}
return true;
}
// 1---model point, 2---refer point
private boolean inferPoint(ILocationEngine newPoint, ILocationEngine point1, ILocationEngine point2,
DesignSpace space) {
double[] newLoc = newPoint.getLocation();
double[] real1 = point1.getLocation();
double[] real2 = point2.getLocation();
for (int i = 0; i < newLoc.length; i++) {
newLoc[i] = real1[i] * 2 - real2[i];
// boundary handling
newLoc[i] = space.boundAdjustAt(newLoc[i], i);
newLoc[i] = RandomGenerator.doubleRangeRandom(newLoc[i], real2[i]);
}
return true;
}
}
......@@ -21,7 +21,7 @@
package net.adaptivebox.space;
public class BasicPoint implements ILocationEngine {
//store the location information in the search space (S)
// store the location information in the search space (S)
private final double[] location;
public BasicPoint(int dim) {
......
......@@ -27,21 +27,20 @@ public class DesignDim {
// To discrete space with the given step. For example, for an integer variable,
// The grain value can be set as 1.
public double grain = 0;
public BasicBound paramBound = new BasicBound(); //the range of a parameter
public BasicBound paramBound = new BasicBound(); // the range of a parameter
public boolean isDiscrete() {
return grain!=0;
return grain != 0;
}
public double getGrainedValue(double value) {
if(grain==0) {
if (grain == 0) {
return value;
} else if(grain>0) {
return paramBound.minValue+Math.rint((value-paramBound.minValue)/grain)*grain;
} else if (grain > 0) {
return paramBound.minValue + Math.rint((value - paramBound.minValue) / grain) * grain;
} else {
return paramBound.maxValue-Math.rint((paramBound.maxValue-value)/grain)*grain;
return paramBound.maxValue - Math.rint((paramBound.maxValue - value) / grain) * grain;
}
}
}
......@@ -27,59 +27,48 @@
package net.adaptivebox.space;
public class DesignSpace {
//The information of all the dimension
// The information of all the dimension
private DesignDim[] dimProps;
public DesignSpace(int dim) {
dimProps = new DesignDim[dim];
}
public void setElemAt(DesignDim elem, int index) {
dimProps[index] = elem;
}
public int getDimension() {
if (dimProps==null) {
if (dimProps == null) {
return -1;
}
return dimProps.length;
}
public double boundAdjustAt(double val, int dim){
public double boundAdjustAt(double val, int dim) {
return dimProps[dim].paramBound.boundAdjust(val);
}
public void mutationAt(double[] location, int i){
public void mutationAt(double[] location, int i) {
location[i] = dimProps[i].paramBound.getRandomValue();
}
public double getMagnitudeIn(int dimensionIndex) {
return dimProps[dimensionIndex].paramBound.getLength();
}
public void initializeGene(double[] tempX){
for(int i=0;i<tempX.length;i++) tempX[i] = dimProps[i].paramBound.getRandomValue(); //Global.RandomGenerator.doubleRangeRandom(9.8, 10);
public void initializeGene(double[] tempX) {
for (int i = 0; i < tempX.length; i++)
tempX[i] = dimProps[i].paramBound.getRandomValue(); // Global.RandomGenerator.doubleRangeRandom(9.8, 10);
}
public void getMappingPoint(double[] point) {
for(int i=0; i<getDimension(); i++) {
for (int i = 0; i < getDimension(); i++) {
point[i] = dimProps[i].paramBound.annulusAdjust(point[i]);
if(dimProps[i].isDiscrete()) {
if (dimProps[i].isDiscrete()) {
point[i] = dimProps[i].getGrainedValue(point[i]);
}
}
}
}
......@@ -20,6 +20,6 @@
package net.adaptivebox.space;
public interface ILocationEngine{
public interface ILocationEngine {
double[] getLocation();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment