Tuning of the Serializer - should load faster now.

This commit is contained in:
Marcel Kronfeld 2010-06-02 13:47:45 +00:00
parent 513bf3df7d
commit 1430b33335
7 changed files with 63 additions and 30 deletions

View File

@ -75,7 +75,13 @@ public class GOEPanel extends JPanel implements ItemListener {
m_goe = goe; m_goe = goe;
//System.out.println("GOEPanel.Constructor !!"); //System.out.println("GOEPanel.Constructor !!");
try {
if (!(Proxy.isProxyClass(m_Object.getClass()))) m_Backup = copyObject(m_Object); if (!(Proxy.isProxyClass(m_Object.getClass()))) m_Backup = copyObject(m_Object);
} catch(OutOfMemoryError err) {
m_Backup=null;
System.gc();
System.err.println("Could not create backup object: not enough memory (GOEPanel backup of " + m_Object + ")");
}
m_ObjectNames = new DefaultComboBoxModel(new String [0]); m_ObjectNames = new DefaultComboBoxModel(new String [0]);
m_ObjectChooser = new JComboBox(m_ObjectNames); m_ObjectChooser = new JComboBox(m_ObjectNames);
m_ObjectChooser.setEditable(false); m_ObjectChooser.setEditable(false);
@ -211,6 +217,7 @@ public class GOEPanel extends JPanel implements ItemListener {
protected Object copyObject(Object source) { protected Object copyObject(Object source) {
Object result = null; Object result = null;
try { try {
// System.out.println("Copying " + BeanInspector.toString(source));
SerializedObject so = new SerializedObject(source); SerializedObject so = new SerializedObject(source);
result = so.getObject(); result = so.getObject();
} catch (Exception ex) { } catch (Exception ex) {

View File

@ -12,8 +12,21 @@ package eva2.gui;
/*==========================================================================* /*==========================================================================*
* IMPORTS * IMPORTS
*==========================================================================*/ *==========================================================================*/
import java.io.*; import java.io.BufferedInputStream;
import java.util.zip.*; import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import eva2.server.modules.GOParameters;
import eva2.tools.Serializer;
/** /**
* This class stores an object serialized in memory. It allows compression, * This class stores an object serialized in memory. It allows compression,
* to be used to conserve memory (for example, when storing large strings * to be used to conserve memory (for example, when storing large strings
@ -29,9 +42,10 @@ public class SerializedObject implements Serializable {
* Serializes the supplied object into a byte array without compression. * Serializes the supplied object into a byte array without compression.
* *
* @param obj the Object to serialize. * @param obj the Object to serialize.
* @throws IOException
* @exception Exception if the object is not Serializable. * @exception Exception if the object is not Serializable.
*/ */
public SerializedObject(Object obj) throws Exception { public SerializedObject(Object obj) throws IOException {
this(obj, false); this(obj, false);
} }
/** /**
@ -39,9 +53,10 @@ public class SerializedObject implements Serializable {
* *
* @param obj the Object to serialize. * @param obj the Object to serialize.
* @param compress true if the object should be stored compressed. * @param compress true if the object should be stored compressed.
* @throws IOException
* @exception Exception if the object is not Serializable. * @exception Exception if the object is not Serializable.
*/ */
public SerializedObject(Object obj, boolean compress) throws Exception { public SerializedObject(Object obj, boolean compress) throws IOException {
//System.err.print("."); System.err.flush(); //System.err.print("."); System.err.flush();
m_Compressed = compress; m_Compressed = compress;
m_Serialized = toByteArray(obj, m_Compressed); m_Serialized = toByteArray(obj, m_Compressed);
@ -52,9 +67,10 @@ public class SerializedObject implements Serializable {
* @param obj the Object to serialize * @param obj the Object to serialize
* @param compress true if the object should be compressed. * @param compress true if the object should be compressed.
* @return the byte array containing the serialized object. * @return the byte array containing the serialized object.
* @throws IOException
* @exception Exception if the object is not Serializable. * @exception Exception if the object is not Serializable.
*/ */
protected static byte [] toByteArray(Object obj, boolean compress) throws Exception { protected static byte [] toByteArray(Object obj, boolean compress) throws IOException {
ByteArrayOutputStream bo = new ByteArrayOutputStream(); ByteArrayOutputStream bo = new ByteArrayOutputStream();
OutputStream os = bo; OutputStream os = bo;
if (compress) if (compress)

View File

@ -59,7 +59,7 @@ class treeElement implements java.io.Serializable {
*/ */
public class SelectXProbRouletteWheel implements InterfaceSelection, java.io.Serializable { public class SelectXProbRouletteWheel implements InterfaceSelection, java.io.Serializable {
private treeElement[] m_TreeRoot; private transient treeElement[] m_TreeRoot = null;
private InterfaceSelectionProbability m_SelProbCalculator = new SelProbStandard(); private InterfaceSelectionProbability m_SelProbCalculator = new SelProbStandard();
private boolean m_ObeyDebsConstViolationPrinciple = true; private boolean m_ObeyDebsConstViolationPrinciple = true;
@ -86,8 +86,9 @@ public class SelectXProbRouletteWheel implements InterfaceSelection, java.io.Ser
this.m_TreeRoot = this.buildSelectionTree(population); this.m_TreeRoot = this.buildSelectionTree(population);
} }
/** This method will select a pool of individuals from the given /**
* Population in respect to the selection propability of the * This method will select a pool of individuals from the given
* Population in respect to the selection probability of the
* individuals. * individuals.
* @param population The source population where to select from * @param population The source population where to select from
* @param size The number of Individuals to select * @param size The number of Individuals to select
@ -97,21 +98,15 @@ public class SelectXProbRouletteWheel implements InterfaceSelection, java.io.Ser
Population result = new Population(); Population result = new Population();
result.setTargetSize(size); result.setTargetSize(size);
if (true) { for (int i = 0; i < size; i++) {
//this.m_TreeRoot = this.buildSelectionTree(population); result.add(this.selectTree(population));
for (int i = 0; i < size; i++) {
result.add(this.selectTree(population));
}
} else {
for (int i = 0; i < size; i++) {
result.add(this.selectStandard(population));
}
} }
return result; return result;
} }
/** This method will build a selection tree /**
* This method will build a selection tree.
* @param p The population * @param p The population
*/ */
private treeElement[] buildSelectionTree(Population p) { private treeElement[] buildSelectionTree(Population p) {
@ -155,6 +150,7 @@ public class SelectXProbRouletteWheel implements InterfaceSelection, java.io.Ser
} }
private AbstractEAIndividual selectStandard(Population population) { private AbstractEAIndividual selectStandard(Population population) {
// old version
double sum = 1, random, tmpD; double sum = 1, random, tmpD;
int currentCriteria = 0, critSize; int currentCriteria = 0, critSize;

View File

@ -1677,8 +1677,8 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
return new double[]{1.,1.,1.,1.}; return new double[]{1.,1.,1.,1.};
} }
if (!(pop.getEAIndividual(0) instanceof InterfaceDataTypeDouble)) { if (!(pop.getEAIndividual(0) instanceof InterfaceDataTypeDouble)) {
EVAERROR.errorMsgOnce("Warning: population correlations can only be calculated for double valued data!"); // EVAERROR.errorMsgOnce("Warning: population correlations can only be calculated for double valued data!");
return new double[]{1.,1.,1.,1.}; return new double[]{Double.NaN,Double.NaN,Double.NaN,Double.NaN};
} }
int index=0; int index=0;
double corsSum=0, minCor = 10., maxCor=-10.; double corsSum=0, minCor = 10., maxCor=-10.;

View File

@ -68,6 +68,7 @@ public class GeneticAlgorithm implements InterfaceOptimizer, java.io.Serializabl
public void initByPopulation(Population pop, boolean reset) { public void initByPopulation(Population pop, boolean reset) {
this.m_Population = (Population)pop.clone(); this.m_Population = (Population)pop.clone();
if (reset) { if (reset) {
this.m_Problem.initPopulation(m_Population);
this.m_Population.init(); this.m_Population.init();
this.evaluatePopulation(this.m_Population); this.evaluatePopulation(this.m_Population);
this.firePropertyChangedEvent(Population.nextGenerationPerformed); this.firePropertyChangedEvent(Population.nextGenerationPerformed);

View File

@ -3,7 +3,6 @@ package eva2.server.modules;
import java.io.Serializable; import java.io.Serializable;
import eva2.server.go.InterfaceGOParameters; import eva2.server.go.InterfaceGOParameters;
import eva2.server.go.individuals.GAIndividualDoubleData;
import eva2.server.go.operators.terminators.EvaluationTerminator; import eva2.server.go.operators.terminators.EvaluationTerminator;
import eva2.server.go.problems.F1Problem; import eva2.server.go.problems.F1Problem;
import eva2.server.go.strategies.GeneticAlgorithm; import eva2.server.go.strategies.GeneticAlgorithm;

View File

@ -21,9 +21,11 @@ import java.io.IOException;
import java.io.InvalidClassException; import java.io.InvalidClassException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.Serializable; import java.io.Serializable;
import eva2.gui.SerializedObject;
import eva2.server.go.problems.PSymbolicRegression;
import eva2.server.modules.GOParameters;
/*==========================================================================* /*==========================================================================*
* CLASS DECLARATION * CLASS DECLARATION
*==========================================================================*/ *==========================================================================*/
@ -33,13 +35,23 @@ import java.io.Serializable;
public class Serializer { public class Serializer {
/** /**
* Serialize the object o (and any Serializable objects it refers to) and * Serialize the object o (and any Serializable objects it refers to) and
* store its serialized state in File f. * store its serialized state in File f. If serializeInMem is true, the object
* is wrapped in a SerializedObject first, which seems to be more efficient than
* writing a nested object directly to a file.
*
* @param o the object to write
* @param f the file to write to
* @param serializeInMem flag whether to wrap the object in a SerializedObject
* @throws IOException
**/ **/
static public void store(Serializable o, File f) throws IOException { static public void store(Serializable o, File f, boolean serializeInMem) throws IOException {
FileOutputStream file = new FileOutputStream(f); FileOutputStream file = new FileOutputStream(f);
ObjectOutputStream out = new ObjectOutputStream(file); ObjectOutputStream out = new ObjectOutputStream(file);
try { try {
out.writeObject(o); Object objToStore = o;
if (serializeInMem) objToStore = new SerializedObject((Object)o);
// System.out.println("Writing " + o.getClass());
out.writeObject(objToStore);
} catch (java.io.NotSerializableException e) { } catch (java.io.NotSerializableException e) {
System.err.println("Error: Object " + o.getClass() + " is not serializable - run settings cannot be stored."); System.err.println("Error: Object " + o.getClass() + " is not serializable - run settings cannot be stored.");
e.printStackTrace(); e.printStackTrace();
@ -59,12 +71,14 @@ public class Serializer {
// } // }
/** /**
* Deserialize the contents of File f and return the resulting object * Deserialize the contents of File f and return the resulting object.
* A SerializedObject is unwrapped once.
**/ **/
static public Object load(File f) throws IOException, ClassNotFoundException { static public Object load(File f) throws IOException, ClassNotFoundException {
FileInputStream file = new FileInputStream(f); FileInputStream file = new FileInputStream(f);
ObjectInputStream in = new ObjectInputStream(file); ObjectInputStream in = new ObjectInputStream(file);
Object ret = in.readObject(); Object ret = in.readObject();
if (ret instanceof SerializedObject) ret = ((SerializedObject)ret).getObject();
in.close(); in.close();
file.close(); file.close();
return ret; return ret;
@ -167,7 +181,7 @@ public class Serializer {
// Output it to a file // Output it to a file
File f = new File("datastructure.ser"); File f = new File("datastructure.ser");
System.out.println("Storing to a file..."); System.out.println("Storing to a file...");
Serializer.store(ds, f); Serializer.store(ds, f, true);
// Read it back from the file, and display it again // Read it back from the file, and display it again
ds = (DataStructure) Serializer.load(f); ds = (DataStructure) Serializer.load(f);
System.out.println("Read from the file: " + ds); System.out.println("Read from the file: " + ds);
@ -184,7 +198,7 @@ public class Serializer {
**/ **/
public static void storeString (String Filename,String s) { public static void storeString (String Filename,String s) {
try { try {
store(s, new File(Filename)); store(s, new File(Filename), false);
} catch (Exception e) { } catch (Exception e) {
System.out.println("ERROR writing string File "+Filename+ " String "+s); System.out.println("ERROR writing string File "+Filename+ " String "+s);
} }
@ -209,7 +223,7 @@ public class Serializer {
public static File storeObject (String Filename,Serializable s) { public static File storeObject (String Filename,Serializable s) {
File ret = new File(Filename); File ret = new File(Filename);
try { try {
store(s, ret); store(s, ret, true);
} catch (Exception e) { } catch (Exception e) {
System.err.println("ERROR writing Object File "+Filename+ " String "+s); System.err.println("ERROR writing Object File "+Filename+ " String "+s);
System.err.println(e.getMessage()); System.err.println(e.getMessage());