public void evaluate(Solution solution) throws JMException {
double vars[] = scaleVariables(solution);
double[] xI = new double[numberOfObjectives_ - 1];
//matp1中 2-1= 1
double[] xII = new double[numberOfVariables_ - numberOfObjectives_ + 1];
//matp1中 50-2+1= 49
for (int i = 0; i < numberOfObjectives_ - 1; i++)
xI[i] = vars[i];
//XI中只含有第一个变量
for (int i = numberOfObjectives_ - 1; i < numberOfVariables_; i++)
//for(i=1;i<50;i++)
xII[i - numberOfObjectives_ + 1] = vars[i];
//XII中含有第二个变量到最后一个变量
//当i=numberOfObjectives_ - 1时,i - numberOfObjectives_ + 1=0
//当i=numberOfVariables_-1时,i - numberOfObjectives_ + 1=numberOfVariables_-numberOfObjectives_=48 其实是第49个变量
xII = transformVariables(xII);
//旋转和偏移
double[] f = new double[numberOfObjectives_];
double g = evalG(xII);
for (int i = 0; i < numberOfObjectives_; i++)
f[i] = 1 + g;
solution.setGFunValue(1 + g);
for (int i = 0; i < numberOfObjectives_; i++) {
for (int j = 0; j < numberOfObjectives_ - (i + 1); j++)
f[i] *= Math.cos(Math.pow(xI[j], alpha_) * 0.5 * Math.PI);
if (i != 0) {
int aux = numberOfObjectives_ - (i + 1);
f[i] *= Math.sin(Math.pow(xI[aux], alpha_) * 0.5 * Math.PI);
} // if
} // for
for (int i = 0; i < numberOfObjectives_; i++)
solution.setObjective(startObjPos_ + i, f[i]);
}
double evalG(double[] xII) throws JMException {
if (gType_.equalsIgnoreCase("sphere"))
return GFunctions.getSphere(xII);
else if (gType_.equalsIgnoreCase("rosenbrock"))
return GFunctions.getRosenbrock(xII);
else if (gType_.equalsIgnoreCase("ackley"))
return GFunctions.getAckley(xII);
else if (gType_.equalsIgnoreCase("griewank"))
return GFunctions.getGriewank(xII);
else if (gType_.equalsIgnoreCase("rastrigin"))
return GFunctions.getRastrigin(xII);
else if (gType_.equalsIgnoreCase("mean"))
return GFunctions.getMean(xII);
else {
System.out.println("Error: g function type " + gType_ + " invalid");
return Double.NaN;
}
}
package momfo.problems.base;
public class GFunctions {
public static double getSphere(double x[]) {
double sum = 0;
for (int i = 0; i < x.length; i++)
sum += (x[i] * x[i]);
return sum;
}
public static double getRosenbrock(double x[]) {
double sum = 0;
for (int i = 0; i < x.length - 1; i++) {
double t = 100 * (x[i] * x[i] - x[i + 1]) * (x[i] * x[i] - x[i + 1]) + (1 - x[i]) * (1 - x[i]);
sum += t;
}
return sum;
}
public static double getAckley(double x[]) {
double sum1 = 0;
double sum2 = 0;
for (int i = 0; i < x.length; i++) {
sum1 += ((x[i] * x[i]) / x.length);
sum2 += (Math.cos(2 * Math.PI * x[i]) / x.length);
}
return -20 * Math.exp(-0.2 * Math.sqrt(sum1)) - Math.exp(sum2) + 20 + Math.E;
}
public static double getGriewank(double x[]) {
int k = 1;
double sum = 0;
double prod = 1;
for (int i = 0; i < x.length; i++) {
sum += (x[i] * x[i]);
prod *= (k * Math.cos(x[i] / Math.sqrt(i + 1)));
}
return k + sum / 4000 - prod;
}
public static double getRastrigin(double x[]) {
double result = 0.0;
double a = 10.0;
double w = 2 * Math.PI;
for (int i = 0; i < x.length; i++) {
result += x[i] * x[i] - a * Math.cos(w * x[i]);
}
result += a * x.length;
return result;
}
public static double getMean(double x[]) {
double mean = 0;
for (int i = 0; i < x.length; i++)
mean += Math.abs(x[i]);
mean /= x.length;
return 9 * mean;
}
}
//生成N*dim的testXII double[N][dim]数组
//由于XII是solution去掉开头维度的且恢复为solution最大值和最小值范围向量
//例如对于MATP1 问题即是一个解向量第二维到第50维度的子向量构成的新向量,对应维度的最大值和最小值都是problem定义的
int N = 100;//即我们总共取100个点
int dim = 49;//因为对于MATP1问题来说,决策变量个数减去目标个数加一=50-2+1=49
double problemup = 100;//问题的上界,对于MATP1问题而言上界是100
double problemlw = -100;//问题的下界,对于MATP1问题而言下界是-100
double[][] testXII = new double[N][dim];
for (int i = 0; i < testXII.length; i++) {
for (int j = 0; j < testXII[i].length; j++)
testXII[i][j] = (double) i / N * (problemup - problemlw)+(problemlw);
}
tools.printdoubleTwoDarray(testXII);
double[] outputofSphere = new double[N];
double[] outputofRosenbrocknew = new double[N];
double[] outputofAckley = new double[N];
double[] outputofGriewank = new double[N];
double[] outputofRastrigin = new double[N];
double[] outputofMean = new double[N];
GFunctions Gfunc = new GFunctions();
for (int i = 0; i < testXII.length; i++) {
outputofSphere[i] = Gfunc.getSphere(testXII[i]);
outputofRosenbrocknew[i]=Gfunc.getRosenbrock(testXII[i]);
outputofAckley[i]=Gfunc.getAckley(testXII[i]);
outputofGriewank[i]=Gfunc.getGriewank(testXII[i]);
outputofRastrigin[i]=Gfunc.getRastrigin(testXII[i]);
outputofMean[i]=Gfunc.getMean(testXII[i]);
}
float[] rgb1 = {0, 0, 0};//黑色-circle
float[] rgb2 = {(float) 0, (float) 0, (float) 255};//蓝黑-concave
float[] rgb3 = {(float) 255, (float) 0, (float) 0};//红色-concave
float[] rgb4 = {(float) 0, (float) 255, (float) 0};//绿色-circle
float[] rgb5 = {(float) 255, (float) 255, (float) 0};//黄色-convex
float[] rgb6 = {(float) 255, (float) 0, (float) 255};//紫色-circle
float alpha = (float) 1;
Scatter scatter1 = tools.SetponintOneD(testXII,outputofSphere, rgb1, alpha, 5);
Scatter scatter2 = tools.SetponintOneD(testXII,outputofRosenbrocknew, rgb2, alpha, 5);
Scatter scatter3 = tools.SetponintOneD(testXII,outputofAckley, rgb3, alpha, 5);
Scatter scatter4 = tools.SetponintOneD(testXII,outputofGriewank, rgb4, alpha, 5);
Scatter scatter5 = tools.SetponintOneD(testXII,outputofRastrigin, rgb5, alpha, 5);
Scatter scatter6 = tools.SetponintOneD(testXII,outputofMean, rgb6, alpha, 5);
Chart chart = new Chart(Quality.Advanced, "awt");
// add scatters
// chart.getScene().add(scatter1);
// chart.getScene().add(scatter2);
// chart.getScene().add(scatter3);
// chart.getScene().add(scatter4);
// chart.getScene().add(scatter5);
chart.getScene().add(scatter6);
Settings.getInstance().setHardwareAccelerated(true);
Settings.getInstance().getGLCapabilities();
//open chart
ChartLauncher.openChart(chart, new Rectangle(0, 0, 600, 600), "Gfunction");
“https://www.researchgate.net/publication/317543380_Evolutionary_Multitasking_for_Multiobjective_Continuous_Optimization_Benchmark_Problems_Performance_Metrics_and_Baseline_Results
for (int i = 0; i < numberOfObjectives_; i++)
f[i] = 1 + g;
solution.setGFunValue(1 + g);