Another small cleanup.

This commit is contained in:
Marcel Kronfeld 2008-03-20 12:44:58 +00:00
parent b472f17428
commit cbc6992ac5
7 changed files with 104 additions and 119 deletions

View File

@ -90,7 +90,8 @@ public class TopoPlot extends Plot {
ry = problem.get2DBorder()[1][0]+y*rh;
pos[0] = rx; pos[1] = ry;
DRectangle rect = new DRectangle(rx,ry,rw,rh);
Color color = new Color(colorBar.getRGB((float)((problem.functionValue(pos)-min)/fitRange))); // Color color = new Color(255,(int)(problem.doEvaluation(pos)[0]/fitRange*255),(int)(problem.doEvaluation(pos)[0]/fitRange*255));
Color color = new Color(colorBar.getRGB((float)((problem.functionValue(pos)-min)/fitRange)));
// Color color = new Color(255,(int)(problem.doEvaluation(pos)[0]/fitRange*255),(int)(problem.doEvaluation(pos)[0]/fitRange*255));
// Color color = new Color(colorBar.getRGB((float)(problem.functionValue(pos)/fitRange))); // Color color = new Color(255,(int)(problem.doEvaluation(pos)[0]/fitRange*255),(int)(problem.doEvaluation(pos)[0]/fitRange*255));
rect.setColor(color);
rect.setFillColor(color);

View File

@ -15,6 +15,9 @@ public class CombinedTerminator implements InterfaceTerminator, Serializable {
private InterfaceTerminator t2 = new EvaluationTerminator();
private SelectedTag andOrTag = new SelectedTag("OR", "AND");
public static final boolean AND = true;
public static final boolean OR = false;
/**
*
*/

View File

@ -105,12 +105,12 @@ public abstract class AbstractOptimizationProblem implements InterfaceOptimizati
// public String getSolutionDataFor(IndividualInterface individual) {
// }
/** This method returns a string describing the optimization problem.
* @return The description.
*/
public String getStringRepresentation() {
return "AbstractOptimizationProblem: programmer failed to give further details";
}
// /** This method returns a string describing the optimization problem.
// * @return The description.
// */
// public String getStringRepresentationF() {
// return "AbstractOptimizationProblem: programmer failed to give further details";
// }
/** This method returns a double value that will be displayed in a fitness
* plot. A fitness that is to be minimized with a global min of zero

View File

@ -225,7 +225,7 @@ public abstract class AbstractProblemDouble extends AbstractOptimizationProblem
double x[] = new double[getProblemDimension()];
for (int i=0; i<point.length; i++) x[i]=point[i];
for (int i=point.length; i<x.length; i++) x[i] = 0;
return eval(x)[0];
return Math.sqrt(eval(x)[0]);
}
/**********************************************************************************************************************
* These are for GUI
@ -254,7 +254,9 @@ public abstract class AbstractProblemDouble extends AbstractOptimizationProblem
*/
public String getStringRepresentationForProblem(InterfaceOptimizer opt) {
StringBuffer sb = new StringBuffer(200);
sb.append("A double valued problem:\n");
sb.append("A double valued problem: ");
sb.append(this.getName());
sb.append("\n");
sb.append(globalInfo());
sb.append("Dimension : ");
sb.append(this.getProblemDimension());

View File

@ -2,25 +2,25 @@ package javaeva.server.go.problems;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import javaeva.gui.BeanInspector;
import javaeva.server.go.individuals.AbstractEAIndividual;
import javaeva.server.go.individuals.ESIndividualDoubleData;
import javaeva.server.go.individuals.InterfaceDataTypeDouble;
import javaeva.server.go.populations.Population;
import javaeva.server.go.strategies.InterfaceOptimizer;
import javaeva.server.go.tools.RandomNumberGenerator;
public class ExternalRuntimeProblem extends AbstractOptimizationProblem {
public class ExternalRuntimeProblem extends AbstractOptimizationProblem implements Interface2DBorderProblem {
protected AbstractEAIndividual m_OverallBest = null;
protected int m_ProblemDimension = 10;
protected boolean m_UseTestConstraint = false;
// protected boolean m_UseTestConstraint = false;
protected String m_Command = "";
protected double defaultRange = 1;
protected double m_upperBound = 10;
protected double m_lowerBound = 0;
public ExternalRuntimeProblem() {
@ -36,7 +36,11 @@ public class ExternalRuntimeProblem extends AbstractOptimizationProblem {
if (b.m_OverallBest != null)
this.m_OverallBest = (AbstractEAIndividual)((AbstractEAIndividual)b.m_OverallBest).clone();
this.m_ProblemDimension = b.m_ProblemDimension;
this.m_UseTestConstraint = b.m_UseTestConstraint;
// this.m_UseTestConstraint = b.m_UseTestConstraint;
m_Command = b.m_Command;
m_lowerBound = b.m_lowerBound;
m_upperBound = b.m_upperBound;
}
/** This method returns a deep clone of the problem.
@ -85,45 +89,44 @@ public class ExternalRuntimeProblem extends AbstractOptimizationProblem {
}
protected double getRangeLowerBound(int dim) {
return -defaultRange;
return m_lowerBound;
}
protected double getRangeUpperBound(int dim) {
return defaultRange;
return m_upperBound;
}
protected double[][] getDoubleRange() {
return ((InterfaceDataTypeDouble)this.m_Template).getDoubleRange();
}
/** This method evaluates a given population and set the fitness values
* accordingly
* @param population The population that is to be evaluated.
*/
public void evaluate(Population population) {
AbstractEAIndividual tmpIndy;
//System.out.println("Population size: " + population.size());
for (int i = 0; i < population.size(); i++) {
tmpIndy = (AbstractEAIndividual) population.get(i);
tmpIndy.resetConstraintViolation();
this.evaluate(tmpIndy);
population.incrFunctionCalls();
}
}
/** This method evaluate a single individual and sets the fitness values
* @param individual The individual that is to be evalutated
*/
public void evaluate(AbstractEAIndividual individual) {
double[] x;
double[] fitness;
// double[] fitness;
x = new double[((InterfaceDataTypeDouble) individual).getDoubleData().length];
System.arraycopy(((InterfaceDataTypeDouble) individual).getDoubleData(), 0, x, 0, x.length);
//TODO call external runtime
double[] fit = eval(x);
individual.SetFitness(fit);
// if (this.m_UseTestConstraint) {
// if (x[0] < 1) individual.addConstraintViolation(1-x[0]);
// }
if ((this.m_OverallBest == null) || (this.m_OverallBest.getFitness(0) > individual.getFitness(0))) {
this.m_OverallBest = (AbstractEAIndividual)individual.clone();
}
}
protected double[] eval(double[] x) {
Process process;
ProcessBuilder pb;
ArrayList<Double> fitList = new ArrayList<Double>();
try {
List<String> parameters=new ArrayList<String>();
parameters.add(this.m_Command);
@ -132,39 +135,31 @@ public class ExternalRuntimeProblem extends AbstractOptimizationProblem {
}
pb = new ProcessBuilder(parameters);
process=pb.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
int count=0;
while ((line = br.readLine()) != null) {
individual.SetFitness(count,new Double(line));
count++;
line = line.trim();
if (line.contains(" ")) {
String[] parts = line.split(" ");
for (String str : parts) {
fitList.add(new Double(str));
}
} else {
fitList.add(new Double(line));
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
System.err.println("IO Error in ExternalRuntimeProblem!");
e.printStackTrace();
} catch (NumberFormatException e) {
System.err.println("Error: " + m_Command + " delivered malformatted output for " + BeanInspector.toString(x));
e.printStackTrace();
}
if (this.m_UseTestConstraint) {
if (x[0] < 1) individual.addConstraintViolation(1-x[0]);
double[] fit = new double[fitList.size()];
for (int i=0; i<fit.length; i++) {
fit[i] = fitList.get(i);
}
if ((this.m_OverallBest == null) || (this.m_OverallBest.getFitness(0) > individual.getFitness(0))) {
this.m_OverallBest = (AbstractEAIndividual)individual.clone();
}
}
/** Ths method allows you to evaluate a simple bit string to determine the fitness
* @param x The n-dimensional input vector
* @return The m-dimensional output vector.
*/
public double[] doEvaluation(double[] x) {
double[] result = new double[1];
result[0] = 0;
for (int i = 0; i < x.length; i++) {
result[0] += Math.pow(x[i], 2);
}
return result;
return fit;
}
/** This method returns a string describing the optimization problem.
@ -226,18 +221,18 @@ public class ExternalRuntimeProblem extends AbstractOptimizationProblem {
return "Command";
}
/** This method allows you to toggle the application of a simple test constraint.
* @param b The mode for the test constraint
*/
public void setUseTestConstraint(boolean b) {
this.m_UseTestConstraint = b;
}
public boolean getUseTestConstraint() {
return this.m_UseTestConstraint;
}
public String useTestConstraintTipText() {
return "Just a simple test constraint of x[0] >= 1.";
}
// /** This method allows you to toggle the application of a simple test constraint.
// * @param b The mode for the test constraint
// */
// public void setUseTestConstraint(boolean b) {
// this.m_UseTestConstraint = b;
// }
// public boolean getUseTestConstraint() {
// return this.m_UseTestConstraint;
// }
// public String useTestConstraintTipText() {
// return "Just a simple test constraint of x[0] >= 1.";
// }
/** This method allows you to choose the EA individual
* @param indy The EAIndividual type
@ -252,36 +247,43 @@ public class ExternalRuntimeProblem extends AbstractOptimizationProblem {
double x[] = new double[m_ProblemDimension];
for (int i=0; i<point.length; i++) x[i]=point[i];
for (int i=point.length; i<m_ProblemDimension; i++) x[i] = 0;
return Math.sqrt(doEvaluation(x)[0]);
return eval(x)[0];
}
public double[][] get2DBorder() {
return getDoubleRange();
}
/**
* A (symmetric) absolute range limit.
*
* @return value of the absolute range limit
* @return the m_upperBound
*/
public double getDefaultRange() {
return defaultRange;
public double getRangeUpperBound() {
return m_upperBound;
}
/**
* Set a (symmetric) absolute range limit.
*
* @param defaultRange
* @param bound the m_upperBound to set
*/
public void setDefaultRange(double defaultRange) {
this.defaultRange = defaultRange;
if (((InterfaceDataTypeDouble)this.m_Template).getDoubleData().length != m_ProblemDimension) {
((InterfaceDataTypeDouble)this.m_Template).setDoubleDataLength(m_ProblemDimension);
}
((InterfaceDataTypeDouble)this.m_Template).SetDoubleRange(makeRange());
public void setRangeUpperBound(double bound) {
m_upperBound = bound;
}
public String defaultRangeTipText() {
return "Absolute limit for the symmetric range in any dimension (not used for all f-problems)";
public String rangeUpperBoundTipText() {
return "Upper bound of the search space in any dimension.";
}
/**
* @return the m_lowerBound
*/
public double getRangeLowerBound() {
return m_lowerBound;
}
/**
* @param bound the m_lowerBound to set
*/
public void setRangeLowerBound(double bound) {
m_lowerBound = bound;
}
public String rangeLowerBoundTipText() {
return "Lower bound of the search space in any dimension.";
}
}

View File

@ -1,14 +1,9 @@
package javaeva.server.go.problems;
import java.io.Serializable;
import java.util.List;
import javaeva.server.go.PopulationInterface;
import javaeva.server.go.individuals.AbstractEAIndividual;
import javaeva.server.go.individuals.ESIndividualDoubleData;
import javaeva.server.go.individuals.InterfaceDataTypeDouble;
import javaeva.server.go.operators.postprocess.PostProcess;
import javaeva.server.go.populations.Population;
import javaeva.server.go.strategies.InterfaceOptimizer;
/**
* Created by IntelliJ IDEA.
@ -17,7 +12,7 @@ import javaeva.server.go.populations.Population;
* Time: 11:10:43
* To change this template use Options | File Templates.
*/
public class FM0Problem extends AbstractMultiModalProblemKnown implements Interface2DBorderProblem, InterfaceMultimodalProblemKnown, Serializable {
public class FM0Problem extends AbstractMultiModalProblemKnown implements InterfaceOptimizationProblem, Interface2DBorderProblem, InterfaceMultimodalProblemKnown, Serializable {
public FM0Problem() {
this.m_ProblemDimension = 2;
@ -62,22 +57,6 @@ public class FM0Problem extends AbstractMultiModalProblemKnown implements Interf
return result;
}
/** This method returns a string describing the optimization problem.
* @return The description.
*/
public String getStringRepresentation() {
String result = "";
result += "M0 function:\n";
result += "This problem has one global and one local optimum.\n";
result += "Parameters:\n";
result += "Dimension : " + this.m_ProblemDimension +"\n";
result += "Noise level : " + this.getNoise() + "\n";
result += "Solution representation:\n";
//result += this.m_Template.getSolutionRepresentationFor();
return result;
}
/** This method will prepare the problem to return a list of all optima
* if possible and to return quality measures like NumberOfOptimaFound and
* the MaximumPeakRatio. This method should be called by the user.
@ -107,6 +86,6 @@ public class FM0Problem extends AbstractMultiModalProblemKnown implements Interf
* @return description
*/
public String globalInfo() {
return "M0(x) = sin(2*x - 0.5*PI) + 1 + 2*cos(y) + 0.5*x is to be maximized.";
return "M0(x) = sin(2*x - 0.5*PI) + 1 + 2*cos(y) + 0.5*x is to be maximized, two optima.";
}
}

View File

@ -237,9 +237,7 @@ public class PSymbolicRegression extends AbstractOptimizationProblem implements
* @return The description.
*/
public String getStringRepresentationForProblem(InterfaceOptimizer opt) {
String result = "";
result += "Symbolic Regression Problem:\n";
String result = "Symbolic Regression Problem";
return result;
}