Cosmetics (Moving F21Problem to F15)

This commit is contained in:
Marcel Kronfeld 2010-02-25 10:23:09 +00:00
parent ba2cd9c8ca
commit a37f030756
5 changed files with 279 additions and 242 deletions

View File

@ -98,22 +98,8 @@ public class Plot implements PlotInterface, Serializable {
m_PlotName = PlotName; m_PlotName = PlotName;
init(); init();
} }
/**
*
*/
public void init() {
m_Frame = new JEFrame("Plot: "+m_PlotName);
BasicResourceLoader loader = BasicResourceLoader.instance();
byte[] bytes = loader.getBytesFromResourceLocation(EvAInfo.iconLocation);
try {
m_Frame.setIconImage(Toolkit.getDefaultToolkit().createImage(bytes));
} catch (java.lang.NullPointerException e) {
System.err.println("Could not find EvA2 icon, please move resources folder to working directory!");
}
m_ButtonPanel = new JPanel(); protected void installButtons(JPanel buttonPan) {
m_PlotArea = new FunctionArea(m_xname,m_yname);
m_ButtonPanel.setLayout( new FlowLayout(FlowLayout.LEFT, 10,10));
JButton ClearButton = new JButton ("Clear"); JButton ClearButton = new JButton ("Clear");
ClearButton.addActionListener(new ActionListener() { ClearButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
@ -142,8 +128,8 @@ public class Plot implements PlotInterface, Serializable {
} }
}); });
JButton SaveJPGButton = new JButton ("Save as PNG..."); JButton saveImageButton = new JButton ("Save as PNG...");
SaveJPGButton.addActionListener(new ActionListener() { saveImageButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
try { try {
Robot robot = new Robot(); Robot robot = new Robot();
@ -152,31 +138,31 @@ public class Plot implements PlotInterface, Serializable {
BufferedImage bufferedImage = robot.createScreenCapture(area); BufferedImage bufferedImage = robot.createScreenCapture(area);
JFileChooser fc = new JFileChooser(); JFileChooser fc = new JFileChooser();
if (fc.showSaveDialog(m_Frame) != JFileChooser.APPROVE_OPTION) return; if (fc.showSaveDialog(m_Frame) != JFileChooser.APPROVE_OPTION) return;
// System.out.println("Name " + outfile); // System.out.println("Name " + outfile);
try { try {
/* Old version /* Old version
FileOutputStream fos = new FileOutputStream(fc.getSelectedFile().getAbsolutePath()+".jpeg"); FileOutputStream fos = new FileOutputStream(fc.getSelectedFile().getAbsolutePath()+".jpeg");
BufferedOutputStream bos = new BufferedOutputStream(fos); BufferedOutputStream bos = new BufferedOutputStream(fos);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
encoder.encode(bufferedImage); encoder.encode(bufferedImage);
bos.close();*/ bos.close();*/
File file = new File(fc.getSelectedFile().getAbsolutePath()+".png"); File file = new File(fc.getSelectedFile().getAbsolutePath()+".png");
ImageIO.write(bufferedImage, "png", file); ImageIO.write(bufferedImage, "png", file);
/* JPEG version with javax.imageio /* JPEG version with javax.imageio
float compression = 0.8f; float compression = 0.8f;
FileImageOutputStream out = new FileImageOutputStream(new File(fc.getSelectedFile().getAbsolutePath()+".jpeg")); FileImageOutputStream out = new FileImageOutputStream(new File(fc.getSelectedFile().getAbsolutePath()+".jpeg"));
ImageWriter encoder = (ImageWriter)ImageIO.getImageWritersByFormatName("JPEG").next(); ImageWriter encoder = (ImageWriter)ImageIO.getImageWritersByFormatName("JPEG").next();
JPEGImageWriteParam param = new JPEGImageWriteParam(null); JPEGImageWriteParam param = new JPEGImageWriteParam(null);
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(compression); param.setCompressionQuality(compression);
encoder.setOutput(out); encoder.setOutput(out);
encoder.write((IIOMetadata) null, new IIOImage(bufferedImage,null,null), param); encoder.write((IIOMetadata) null, new IIOImage(bufferedImage,null,null), param);
out.close();
*/
out.close();
*/
} catch (Exception eee) { } catch (Exception eee) {
System.err.println("Error on exporting PNG: " + eee.getMessage()); System.err.println("Error on exporting PNG: " + eee.getMessage());
} }
@ -187,14 +173,34 @@ public class Plot implements PlotInterface, Serializable {
} }
}); });
m_ButtonPanel.add(ClearButton); buttonPan.add(ClearButton);
m_ButtonPanel.add(LOGButton); buttonPan.add(LOGButton);
m_ButtonPanel.add(DumpButton); buttonPan.add(DumpButton);
m_ButtonPanel.add(ExportButton); buttonPan.add(ExportButton);
// m_ButtonPanel.add(PrintButton); // m_ButtonPanel.add(PrintButton);
// m_ButtonPanel.add(OpenButton); // m_ButtonPanel.add(OpenButton);
// m_ButtonPanel.add(SaveButton); // m_ButtonPanel.add(SaveButton);
m_ButtonPanel.add(SaveJPGButton); buttonPan.add(saveImageButton);
}
/**
*
*/
public void init() {
m_Frame = new JEFrame("Plot: "+m_PlotName);
BasicResourceLoader loader = BasicResourceLoader.instance();
byte[] bytes = loader.getBytesFromResourceLocation(EvAInfo.iconLocation);
try {
m_Frame.setIconImage(Toolkit.getDefaultToolkit().createImage(bytes));
} catch (java.lang.NullPointerException e) {
System.err.println("Could not find EvA2 icon, please move resources folder to working directory!");
}
m_ButtonPanel = new JPanel();
m_PlotArea = new FunctionArea(m_xname,m_yname);
m_ButtonPanel.setLayout( new FlowLayout(FlowLayout.LEFT, 10,10));
installButtons(m_ButtonPanel);
// getContentPane().smultetLayout( new GridLayout(1, 4) ); // getContentPane().smultetLayout( new GridLayout(1, 4) );
m_Frame.getContentPane().add(m_ButtonPanel,"South"); m_Frame.getContentPane().add(m_ButtonPanel,"South");
m_Frame.getContentPane().add(m_PlotArea,"Center"); // north was not so nice m_Frame.getContentPane().add(m_PlotArea,"Center"); // north was not so nice
@ -216,12 +222,12 @@ public class Plot implements PlotInterface, Serializable {
* @param prefix * @param prefix
* @param pop * @param pop
*/ */
public void drawPopulation(String prefix, Population pop) { public void drawPopulation(String prefix, Population pop) {
for (int i=0; i<pop.size(); i++) { for (int i=0; i<pop.size(); i++) {
drawIndividual(1, 2, prefix, pop.getEAIndividual(i)); drawIndividual(1, 2, prefix, pop.getEAIndividual(i));
} }
} }
/** /**
* Draw an individual to the Plot instance. It is annotated with the * Draw an individual to the Plot instance. It is annotated with the
* given prefix and its fitness. * given prefix and its fitness.
@ -230,10 +236,10 @@ public class Plot implements PlotInterface, Serializable {
* @param pop * @param pop
* @see FunctionArea.drawIcon * @see FunctionArea.drawIcon
*/ */
public void drawIndividual(int iconType, int graphID, String prefix, AbstractEAIndividual indy) { public void drawIndividual(int iconType, int graphID, String prefix, AbstractEAIndividual indy) {
getFunctionArea().drawIcon(iconType, prefix+" "+indy.getFitness(0), indy.getDoublePosition(), graphID); getFunctionArea().drawIcon(iconType, prefix+" "+indy.getFitness(0), indy.getDoublePosition(), graphID);
} }
public void setPreferredSize(Dimension prefSize) { public void setPreferredSize(Dimension prefSize) {
if (m_Frame != null) { if (m_Frame != null) {
m_Frame.setPreferredSize(prefSize); m_Frame.setPreferredSize(prefSize);
@ -401,7 +407,7 @@ public class Plot implements PlotInterface, Serializable {
public String getName() { public String getName() {
return this.m_PlotName; return this.m_PlotName;
} }
/** /**
* *
*/ */
@ -427,23 +433,23 @@ public class Plot implements PlotInterface, Serializable {
setUnconnectedPoint(range[0][1], range[1][1], graphLabel); setUnconnectedPoint(range[0][1], range[1][1], graphLabel);
} }
// /** // /**
// * Just for testing the Plot class. // * Just for testing the Plot class.
// */ // */
// public static void main( String[] args ){ // public static void main( String[] args ){
// Plot plot = new Plot("Plot-Test","x-value","y-value"); // Plot plot = new Plot("Plot-Test","x-value","y-value");
// plot.init(); // plot.init();
// double x; // double x;
// for (x= 0; x <6000; x++) { // for (x= 0; x <6000; x++) {
// //double y = SpecialFunction.getnormcdf(x); // //double y = SpecialFunction.getnormcdf(x);
// // double yy = 0.5*SpecialFunction.getnormpdf(x); // // double yy = 0.5*SpecialFunction.getnormpdf(x);
// double n = Math.sin(((double)x/1000*Math.PI)); // double n = Math.sin(((double)x/1000*Math.PI));
// //plot.setConnectedPoint(x,Math.sin(x),0); // //plot.setConnectedPoint(x,Math.sin(x),0);
// //plot.setConnectedPoint(x,Math.cos(x),1); // //plot.setConnectedPoint(x,Math.cos(x),1);
// //plot.setConnectedPoint(x,y,0); // //plot.setConnectedPoint(x,y,0);
// plot.setConnectedPoint(x,n,1); // plot.setConnectedPoint(x,n,1);
// } // }
// //plot.addGraph(1,2); // //plot.addGraph(1,2);
// } // }
} }

View File

@ -14,6 +14,8 @@ package eva2.gui;
*==========================================================================*/ *==========================================================================*/
import java.awt.Color; import java.awt.Color;
import javax.swing.JPanel;
import eva2.server.go.problems.Interface2DBorderProblem; import eva2.server.go.problems.Interface2DBorderProblem;
import eva2.server.go.problems.InterfaceFirstOrderDerivableProblem; import eva2.server.go.problems.InterfaceFirstOrderDerivableProblem;
import eva2.tools.chart2d.DRectangle; import eva2.tools.chart2d.DRectangle;
@ -28,7 +30,9 @@ import eva2.tools.math.Mathematics;
* *
*/ */
public class TopoPlot extends Plot { public class TopoPlot extends Plot {
Interface2DBorderProblem prob=null;
double[][] range=null;
boolean withGrads=false;
private int gridx = 50; private int gridx = 50;
private int gridy = 50; private int gridy = 50;
int colorScale = ColorBarCalculator.BLUE_TO_RED; int colorScale = ColorBarCalculator.BLUE_TO_RED;
@ -60,7 +64,24 @@ public class TopoPlot extends Plot {
colorScale = color_scale; colorScale = color_scale;
} }
/**
@Override
protected void installButtons(JPanel buttonPan) {
super.installButtons(buttonPan);
// TODO this actually works, but it is horribly slow
// JButton refineButton = new JButton ("Refine");
// refineButton.setToolTipText("Refine the graph resolution");
// refineButton.addActionListener(new ActionListener() {
// public void actionPerformed(ActionEvent e) {
// gridx=(int)(Math.sqrt(2.)*gridx);
// gridy=(int)(Math.sqrt(2.)*gridy);
// setTopology(prob, range, withGrads);
// }
// });
// buttonPan.add(refineButton);
}
/**
* Defines parameters used for drawing the topology. * Defines parameters used for drawing the topology.
* @param gridX the x-resolution of the topology, higher value means higher resolution * @param gridX the x-resolution of the topology, higher value means higher resolution
* @param gridY the y-resolution of the topology, higher value means higher resolution * @param gridY the y-resolution of the topology, higher value means higher resolution
@ -79,6 +100,9 @@ public class TopoPlot extends Plot {
* Defines the topology (by setting a specific problem) and draws the topology * Defines the topology (by setting a specific problem) and draws the topology
*/ */
public void setTopology(Interface2DBorderProblem problem, double[][] border, boolean withGradientsIfAvailable) { public void setTopology(Interface2DBorderProblem problem, double[][] border, boolean withGradientsIfAvailable) {
prob=problem;
range=border;
withGrads=withGradientsIfAvailable;
double[] sizeXY=Mathematics.getAbsRange(border); double[] sizeXY=Mathematics.getAbsRange(border);
double deltaX = sizeXY[0]/gridx; double deltaX = sizeXY[0]/gridx;
double deltaY = sizeXY[1]/gridy; double deltaY = sizeXY[1]/gridy;
@ -133,9 +157,5 @@ public class TopoPlot extends Plot {
} // for x } // for x
} }
m_Frame.setVisible(true); m_Frame.setVisible(true);
} // setTopology } // setTopology
} // class } // class

View File

@ -1,102 +1,77 @@
package eva2.server.go.problems; package eva2.server.go.problems;
import static java.lang.Math.PI; import java.io.Serializable;
import static java.lang.Math.sin;
public class F15Problem extends AbstractProblemDoubleOffset { import eva2.server.go.operators.postprocess.SolutionHistogram;
int iterations = 100;
/**
* The Levy-function, from Levy, A., and Montalvo, A. (1985). Also described in
* "A Trust-Region Algorithm for Global Optimization", Bernardetta Addisy and Sven Leyfferz, 2004/2006.
*/
public class F15Problem extends AbstractProblemDouble implements Serializable, InterfaceInterestingHistogram {
private int dim=10;
public F15Problem() {
super();
super.SetDefaultAccuracy(0.00001);
setDefaultRange(10);
}
public F15Problem(F15Problem f15Problem) { public F15Problem(int d) {
iterations = f15Problem.iterations; this();
setProblemDimension(d);
} }
public F15Problem(F15Problem o) {
super(o);
dim=o.getProblemDimension();
setDefaultRange(o.getDefaultRange());
}
@Override
public double[] eval(double[] x) {
x = rotateMaybe(x);
double t=0, s=0, sum=0;
public Object clone() { for (int i=0; i<x.length-2; i++) {
return (Object) new F15Problem(this); s=(x[i]-1.);
} t=Math.sin(Math.PI*x[i+1]);
sum += (s*s)*(1+10.*t*t);
/** This method allows you to evaluate a double[] to determine the fitness }
* @param x The n-dimensional input vector
* @return The m-dimensional output vector.
*/
public double[] eval(double[] x) {
x = rotateMaybe(x);
double[] c = new double[2];
// c[0]= (x[0])*getXOffSet();
// c[1]= (x[1])*Math.sin((Math.PI/2)*getYOffSet());
c[0] = x[0]*x[2];
c[1] = x[1] * sin( PI / 2.0 * x[3]);
// c[0]= (x[0]+(x[2]/10))*getXOffSet(); s=Math.sin(Math.PI*x[0]);
// c[1]= (x[1]+(x[3]/10))*Math.sin(Math.PI/2*getYOffSet()); double[] y = new double[1];
// c[0]= (x[0]*(1-x[2]/10)); y[0] = 10.*s*s+sum+dim*(x[dim-1]-1)*(x[dim-1]-1);
// c[1]= (x[1]*(1-x[3]/10)); return y;
double[] result = new double[1];
result[0] = flatten(evalRec(x, c, iterations));
return result;
}
private double[] evalRec(double[] x, double[] c, int n) {
if (n==0) return x;
else return evalRec(addComplex(squareComplex(x),c), c, n-1);
}
private double[] squareComplex(double[] x) {
double[] result = new double[2];
result[0] = (x[0]*x[0])-(x[1]*x[1]);
result[1] = (2*x[0]*x[1]);
return result;
}
private double[] addComplex(double[] x, double[] y) {
double[] result = new double[2];
result[0] = x[0] + y[0];
result[1] = x[1] + y[1];
return result;
}
private double flatten(double[] x) {
double len = Math.sqrt((x[0]*x[0])+(x[1]*x[1]));
double ang = Math.atan2(x[1],x[0]);
if (Double.isNaN(len) || (len > 1000.)) len = 1000.;
return len;
// return 0.5+0.5*t*(2*b*a*u(x/a));
// return 1/(2*Math.PI*ang);
// return +Math.abs(x[0])+Math.abs(x[1]);
}
public int getProblemDimension() {
return 4;
}
public String getName() {
return "F15-Problem";
}
public double getRangeLowerBound(int dim) {
// return -1;
if (dim == 0) return -2.5;
else return -1.5;
}
public double getRangeUpperBound(int dim) {
// return 1;
return 1.5;
}
/**
* @return the iterations
*/
public int getIterations() {
return iterations;
} }
/** @Override
* @param iterations the iterations to set public int getProblemDimension() {
*/ return dim;
public void setIterations(int iterations) {
this.iterations = iterations;
} }
public void setProblemDimension(int d) {
dim=d;
}
@Override
public Object clone() {
return new F15Problem(this);
}
public SolutionHistogram getHistogram() {
if (getProblemDimension()<15) return new SolutionHistogram(0, 2, 16);
else if (getProblemDimension()<25) return new SolutionHistogram(0, 4, 16);
else return new SolutionHistogram(0, 8, 16);
}
public String getName() {
return "F15-Problem";
}
public String globalInfo() {
return "The Levy-function, from Levy, A., and Montalvo, A. (1985)";
}
} }

View File

@ -1,77 +0,0 @@
package eva2.server.go.problems;
import java.io.Serializable;
import eva2.server.go.operators.postprocess.SolutionHistogram;
/**
* The Levy-function, from Levy, A., and Montalvo, A. (1985). Also described in
* "A Trust-Region Algorithm for Global Optimization", Bernardetta Addisy and Sven Leyfferz, 2004/2006.
*/
public class F21Problem extends AbstractProblemDouble implements Serializable, InterfaceInterestingHistogram {
private int dim=10;
public F21Problem() {
super();
super.SetDefaultAccuracy(0.00001);
setDefaultRange(10);
}
public F21Problem(int d) {
this();
setProblemDimension(d);
}
public F21Problem(F21Problem f21Problem) {
super(f21Problem);
dim=f21Problem.getProblemDimension();
setDefaultRange(f21Problem.getDefaultRange());
}
@Override
public double[] eval(double[] x) {
x = rotateMaybe(x);
double t=0, s=0, sum=0;
for (int i=0; i<x.length-2; i++) {
s=(x[i]-1.);
t=Math.sin(Math.PI*x[i+1]);
sum += (s*s)*(1+10.*t*t);
}
s=Math.sin(Math.PI*x[0]);
double[] y = new double[1];
y[0] = 10.*s*s+sum+dim*(x[dim-1]-1)*(x[dim-1]-1);
return y;
}
@Override
public int getProblemDimension() {
return dim;
}
public void setProblemDimension(int d) {
dim=d;
}
@Override
public Object clone() {
return new F21Problem(this);
}
public SolutionHistogram getHistogram() {
if (getProblemDimension()<15) return new SolutionHistogram(0, 2, 16);
else if (getProblemDimension()<25) return new SolutionHistogram(0, 4, 16);
else return new SolutionHistogram(0, 8, 16);
}
public String getName() {
return "F21-Problem";
}
public String globalInfo() {
return "The Levy-function, from Levy, A., and Montalvo, A. (1985)";
}
}

View File

@ -0,0 +1,113 @@
package eva2.server.go.problems;
import static java.lang.Math.PI;
import static java.lang.Math.sin;
/**
* This is just an example function with no real use except that it has a
* nice plot to it. It doesnt make much sense for optimization, so Ill
* hide it for now.
* If you wonder what this function actually represents, you may want to
* try to display a cut with x[2]=1; x[3]=1.
*
* @author mkron
*
*/
public class FunnyProblem extends AbstractProblemDoubleOffset {
int iterations = 100;
// public static final boolean hideFromGOE = true;
public FunnyProblem() {}
public FunnyProblem(FunnyProblem o) {
iterations = o.iterations;
}
public Object clone() {
return (Object) new FunnyProblem(this);
}
/** This method allows you to evaluate a double[] to determine the fitness
* @param x The n-dimensional input vector
* @return The m-dimensional output vector.
*/
public double[] eval(double[] x) {
x = rotateMaybe(x);
double[] c = new double[2];
// c[0]= (x[0])*getXOffSet();
// c[1]= (x[1])*Math.sin((Math.PI/2)*getYOffSet());
c[0] = x[0]*x[2];
c[1] = x[1] * sin( PI / 2.0 * x[3]);
// c[0]= (x[0]+(x[2]/10))*getXOffSet();
// c[1]= (x[1]+(x[3]/10))*Math.sin(Math.PI/2*getYOffSet());
// c[0]= (x[0]*(1-x[2]/10));
// c[1]= (x[1]*(1-x[3]/10));
double[] result = new double[1];
result[0] = flatten(evalRec(x, c, iterations));
return result;
}
private double[] evalRec(double[] x, double[] c, int n) {
if (n==0) return x;
else return evalRec(addComplex(squareComplex(x),c), c, n-1);
}
private double[] squareComplex(double[] x) {
double[] result = new double[2];
result[0] = (x[0]*x[0])-(x[1]*x[1]);
result[1] = (2*x[0]*x[1]);
return result;
}
private double[] addComplex(double[] x, double[] y) {
double[] result = new double[2];
result[0] = x[0] + y[0];
result[1] = x[1] + y[1];
return result;
}
private double flatten(double[] x) {
double len = Math.sqrt((x[0]*x[0])+(x[1]*x[1]));
double ang = Math.atan2(x[1],x[0]);
if (Double.isNaN(len) || (len > 1000.)) len = 1000.;
return len;
// return 0.5+0.5*t*(2*b*a*u(x/a));
// return 1/(2*Math.PI*ang);
// return +Math.abs(x[0])+Math.abs(x[1]);
}
public int getProblemDimension() {
return 4;
}
public String getName() {
return "FunnyProblem";
}
public double getRangeLowerBound(int dim) {
if (dim == 0) return -2.5;
else return -1.5;
}
public double getRangeUpperBound(int dim) {
return 1.5;
}
/**
* @return the iterations
*/
public int getIterations() {
return iterations;
}
/**
* @param iterations the iterations to set
*/
public void setIterations(int iterations) {
this.iterations = iterations;
}
}