Minor updates, merging MK branch revs. 511:515

This commit is contained in:
Marcel Kronfeld 2010-04-15 09:43:49 +00:00
parent b6e13e3b60
commit 372e0cf5e9
32 changed files with 709 additions and 196 deletions

View File

@ -320,9 +320,8 @@ public class GenericObjectEditor implements PropertyEditor {
*/
private void setObject(Object c) {
// This should really call equals() for comparison.
if (TRACE) System.out.println("setObject "+ c.getClass().getName());
if (TRACE) System.out.println("setObject "+ c.getClass().getName() + " " + c);
boolean trueChange = (c != getValue());
//System.err.println("Didn't even try to make a Object copy!! "+ "(using original)");
m_Backup = m_Object;
m_Object = c;

View File

@ -153,7 +153,6 @@ Serializable {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

View File

@ -33,6 +33,8 @@ import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Formatter;
import java.util.Locale;
import javax.imageio.ImageIO;
import javax.swing.JButton;
@ -260,22 +262,24 @@ public class Plot implements PlotInterface, Serializable {
*/
public void drawPopulation(String prefix, Population pop) {
for (int i = 0; i < pop.size(); i++) {
drawIndividual(1, 2, prefix, pop.getEAIndividual(i));
drawIndividual(0, 2, prefix, pop.getEAIndividual(i));
}
}
/**
* Draw an individual to the Plot instance. It is annotated with the given
* prefix and its fitness.
* Draw an individual to the Plot instance. It is annotated with the
* given prefix and its fitness with short scientific notation.
*
* @param prefix
* @param pop
* @see FunctionArea.drawIcon
*/
public void drawIndividual(int iconType, int graphID, String prefix,
AbstractEAIndividual indy) {
getFunctionArea().drawIcon(iconType, prefix + " " + indy.getFitness(0),
indy.getDoublePosition(), graphID);
public void drawIndividual(int iconType, int graphID, String prefix, AbstractEAIndividual indy) {
StringBuffer sb = new StringBuffer();
Formatter formatter = new Formatter(sb, Locale.US);
formatter.format("%s %.3e", prefix, indy.getFitness(0));
getFunctionArea().drawIcon(iconType, sb.toString(), indy.getDoublePosition(), graphID);
}
public void setPreferredSize(Dimension prefSize) {

View File

@ -912,7 +912,6 @@ public class PropertySheetPanel extends JPanel implements PropertyChangeListener
try {
val = getter.invoke(m_Target, (Object[])null);
} catch (Exception e) {
// TODO Auto-generated catch block
val = null;
e.printStackTrace();
}

View File

@ -0,0 +1,5 @@
package eva2.server.go.enums;
public enum MutateESCrossoverTypeEnum {
none, intermediate, discrete;
}

View File

@ -3,6 +3,7 @@ package eva2.server.go.individuals;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.Formatter;
import java.util.HashMap;
import java.util.List;
@ -805,7 +806,7 @@ public abstract class AbstractEAIndividual implements IndividualInterface, java.
public double getCrossoverProbability() {
return this.m_CrossoverProbability;
}
public String crossoverProbalilityTipText() {
public String crossoverProbabilityTipText() {
return "The chance that crossover occurs.";
}
@ -876,13 +877,13 @@ public abstract class AbstractEAIndividual implements IndividualInterface, java.
public static String getDefaultStringRepresentation(AbstractEAIndividual individual) {
// Note that changing this method might change the hashcode of an individual
// which might interfere with some functionality.
StringBuffer sb = new StringBuffer("Fit.: ");
StringBuffer sb = new StringBuffer("Fit.:\t");
sb.append(BeanInspector.toString(individual.getFitness()));
if (individual.isMarkedPenalized() || individual.violatesConstraint())
sb.append(", X");
sb.append(", ID: ");
sb.append(individual.getIndyID());
sb.append(", ");
sb.append(",\t");
sb.append(getDefaultDataString(individual));
if (individual.getParentIDs()!=null) {
@ -902,7 +903,7 @@ public abstract class AbstractEAIndividual implements IndividualInterface, java.
public static String getDefaultDataString(IndividualInterface individual) {
// Note that changing this method might change the hashcode of an individual
// which might interfere with some functionality.
return getDefaultDataString(individual, ", ");
return getDefaultDataString(individual, ",");
}
/**
@ -917,6 +918,7 @@ public abstract class AbstractEAIndividual implements IndividualInterface, java.
// which might interfere with some functionality.
if (individual == null) return "null";
StringBuffer sb = new StringBuffer("");
Formatter fm = new Formatter(sb);
char left = '{';
char right = '}';
sb.append(left);
@ -933,10 +935,12 @@ public abstract class AbstractEAIndividual implements IndividualInterface, java.
if ((i+1) < b.length) sb.append(separator);
}
} else if (individual instanceof InterfaceDataTypeDouble) {
double[] b = ((InterfaceDataTypeDouble)individual).getDoubleData();
for (int i = 0; i < b.length; i++) {
sb.append(b[i]);
if ((i+1) < b.length) sb.append(separator);
double[] d = ((InterfaceDataTypeDouble)individual).getDoubleData();
for (int i = 0; i < d.length; i++) {
// sb.append(d[i]);
// if ((i+1) < d.length) sb.append(separator);
fm.format("% .3f", d[i]);
if ((i+1) < d.length) sb.append(separator);
}
} else if (individual instanceof InterfaceDataTypePermutation) {
int[] b = ((InterfaceDataTypePermutation)individual).getPermutationData()[0];

View File

@ -1,6 +1,7 @@
package eva2.server.go.individuals;
import eva2.server.go.enums.MutateESCrossoverTypeEnum;
import eva2.server.go.operators.crossover.CrossoverESDefault;
import eva2.server.go.operators.mutation.InterfaceMutation;
import eva2.server.go.operators.mutation.MutateESGlobal;
@ -25,7 +26,7 @@ public class ESIndividualDoubleData extends AbstractEAIndividual implements Inte
public ESIndividualDoubleData() {
this.m_MutationProbability = 1.0;
this.m_MutationOperator = new MutateESGlobal();
this.m_MutationOperator = new MutateESGlobal(0.2, MutateESCrossoverTypeEnum.intermediate);
this.m_CrossoverProbability = 0.5;
this.m_CrossoverOperator = new CrossoverESDefault();
this.m_Genotype = new double[1];

View File

@ -0,0 +1,40 @@
package eva2.server.go.individuals;
import java.util.Comparator;
import eva2.server.go.operators.distancemetric.InterfaceDistanceMetric;
/**
* Compare two AbstractEAIndividuals by their distance to a reference individual.
* Usable to sort by a distance.
**/
public class IndividualDistanceComparator implements Comparator<Object> {
private AbstractEAIndividual refIndy=null;
private InterfaceDistanceMetric distMetric = null;
private boolean closerMeansLess = true;
/**
* Constructor with a reference individual to which the distance is measured and the corresponding desired metric.
* Using closerIsLess it can be switched between increasing and decreasing order.
*
* @param referenceIndy
* @param metric
* @param closerIsLess
*/
public IndividualDistanceComparator(AbstractEAIndividual referenceIndy, InterfaceDistanceMetric metric, boolean closerIsLess) {
refIndy = referenceIndy;
distMetric = metric;
closerMeansLess = closerIsLess;
}
public int compare(Object o1, Object o2) {
double d1 = distMetric.distance((AbstractEAIndividual)o1, refIndy);
double d2 = distMetric.distance((AbstractEAIndividual)o2, refIndy);
if (d1==d2) return 0;
if (closerMeansLess) return ((d1<d2) ? -1 : 1);
else return ((d1<d2) ? 1 : -1);
}
}

View File

@ -60,7 +60,6 @@ public class MOCCOProblemInitialization extends MOCCOPhase implements InterfaceP
try {
altern = ReflectPackage.getAssignableClassesInPackage("eva2.server.go.problems", Class.forName("eva2.server.go.problems.InterfaceMultiObjectiveDeNovoProblem"), true, true);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.m_ProblemChooser.setModel(new DefaultComboBoxModel(altern));

View File

@ -53,7 +53,7 @@ public class ArchivingMaxiMin implements InterfaceArchiving, java.io.Serializabl
Population tmpPop = new Population();
tmpPop.addPopulation((Population)pop.getClone());
tmpPop.addPopulation((Population)pop.getArchive().getClone());
tmpPop.removeDoubleInstancesUsingFitness();
tmpPop.removeRedundantIndiesUsingFitness();
// Now calculate the MaxiMin Criterium
this.m_MaxiMin.convertMultiObjective2SingleObjective(tmpPop);

View File

@ -65,7 +65,7 @@ public class ArchivingNSGAII extends ArchivingNSGA implements java.io.Serializab
Population tmpPop = new Population();
tmpPop.addPopulation((Population)pop.getClone());
tmpPop.addPopulation((Population)pop.getArchive().getClone());
tmpPop.removeDoubleInstancesUsingFitness();
tmpPop.removeRedundantIndiesUsingFitness();
// Now fetch the n pareto-fronts
Population[] fronts = this.getNonDomiatedSortedFronts(tmpPop);

View File

@ -63,7 +63,7 @@ public class ArchivingSPEAII extends AbstractArchiving implements java.io.Serial
Population tmpPop = new Population();
tmpPop.addPopulation((Population)pop.getClone());
tmpPop.addPopulation((Population)pop.getArchive().getClone());
tmpPop.removeDoubleInstancesUsingFitness();
tmpPop.removeRedundantIndiesUsingFitness();
// double[][] d = this.showMay(tmpPop);
double[] RawFitness = this.calculateRawFitness(tmpPop);

View File

@ -84,10 +84,6 @@ public class GenericConstraint extends AbstractConstraint implements InterfaceDo
return 0.;
}
// private double evalLinearConstr(AbstractEAIndividual indy) {
// // TODO Auto-generated method stub
// return null;
// }
public String getConstraintString() {
return constraintString;
}

View File

@ -48,6 +48,9 @@ public class CrossoverESDefault implements InterfaceCrossover, java.io.Serializa
}
//for (int i = 0; i < result.length; i++) System.out.println("Before Crossover: " +result[i].getSolutionRepresentationFor());
if (partners==null || (partners.size()==0)) {
throw new RuntimeException("Error, empty partner population for crossover!");
}
if ((indy1 instanceof InterfaceESIndividual) && (partners.get(0) instanceof InterfaceESIndividual)) {
int crossoverpoint = RNG.randomInt(0,((InterfaceESIndividual)indy1).getDGenotype().length-1);
boolean switcher = RNG.randomBoolean();
@ -75,7 +78,7 @@ public class CrossoverESDefault implements InterfaceCrossover, java.io.Serializa
// write the result back
for (int i = 0; i < result.length; i++) ((InterfaceESIndividual)result[i]).SetDGenotype(children[i]);
}
//in case the crossover was successfull lets give the mutation operators a chance to mate the strategy parameters
//in case the crossover was successful lets give the mutation operators a chance to mate the strategy parameters
for (int i = 0; i < result.length; i++) result[i].getMutationOperator().crossoverOnStrategyParameters(indy1, partners);
//for (int i = 0; i < result.length; i++) System.out.println("After Crossover: " +result[i].getSolutionRepresentationFor());
return result;

View File

@ -1,5 +1,7 @@
package eva2.server.go.operators.distancemetric;
import java.io.Serializable;
import eva2.server.go.individuals.AbstractEAIndividual;
/**
@ -10,7 +12,7 @@ import eva2.server.go.individuals.AbstractEAIndividual;
* @author mkron
*
*/
public class EuclideanMetric implements InterfaceDistanceMetric {
public class EuclideanMetric implements InterfaceDistanceMetric, Serializable {
public Object clone() {
return (Object) new EuclideanMetric(this);

View File

@ -24,7 +24,6 @@ public class IndividualDataMetric implements InterfaceDistanceMetric, Serializab
}
public IndividualDataMetric(IndividualDataMetric pBestMetric) {
// TODO Auto-generated constructor stub
}
/** This method allows you to make a deep clone of

View File

@ -390,12 +390,12 @@ public class MOClusteringSeparation implements InterfaceMigration, java.io.Seria
* @return The distance type to use.
*/
public boolean getReuseC() {
this.m_ReuseC = this.m_KMeans.m_ReuseC;
this.m_ReuseC = this.m_KMeans.getReuseC();
return this.m_ReuseC;
}
public void setReuseC(boolean m){
this.m_ReuseC = m;
this.m_KMeans.m_ReuseC = this.m_ReuseC;
this.m_KMeans.setReuseC(this.m_ReuseC);
}
public String reuseCTipText() {
return "Toggel reuse of previously found cluster centroids.";

View File

@ -108,7 +108,7 @@ public class MOSOMaxiMin implements InterfaceMOSOConverter, java.io.Serializable
tmpFit = indy.getFitness();
indy.putData("MOFitness", tmpFit);
System.out.println("The MaxiMin MOSO can not be applied to single individuals! I default to random criterion.");
System.err.println("The MaxiMin MOSO can not be applied to single individuals! I default to random criterion.");
resultFit[0] = tmpFit[RNG.randomInt(0, tmpFit.length)];
indy.SetFitness(resultFit);
}

View File

@ -2,6 +2,7 @@ package eva2.server.go.operators.mutation;
import java.util.ArrayList;
import eva2.server.go.enums.MutateESCrossoverTypeEnum;
import eva2.server.go.individuals.AbstractEAIndividual;
import eva2.server.go.individuals.InterfaceESIndividual;
import eva2.server.go.populations.Population;
@ -21,26 +22,35 @@ public class MutateESGlobal implements InterfaceMutation, java.io.Serializable {
protected double m_MutationStepSize = 0.2;
protected double m_Tau1 = 0.15;
protected double m_LowerLimitStepSize = 0.0000005;
protected SelectedTag m_CrossoverType;
protected MutateESCrossoverTypeEnum m_CrossoverType = MutateESCrossoverTypeEnum.none;
public MutateESGlobal() {
initTags();
}
/**
* Use given mutation step size and no crossover on strategy params.
*
* @param mutationStepSize
*/
public MutateESGlobal(double mutationStepSize) {
initTags();
this(mutationStepSize, MutateESCrossoverTypeEnum.none);
}
/**
* Use given mutation step size and given crossover type on strategy params.
*
* @param mutationStepSize
*/
public MutateESGlobal(double mutationStepSize, MutateESCrossoverTypeEnum coType) {
setMutationStepSize(mutationStepSize);
setCrossoverType(coType);
}
public MutateESGlobal(MutateESGlobal mutator) {
this.m_MutationStepSize = mutator.m_MutationStepSize;
this.m_Tau1 = mutator.m_Tau1;
this.m_LowerLimitStepSize = mutator.m_LowerLimitStepSize;
this.m_CrossoverType = (SelectedTag)mutator.m_CrossoverType.clone();
}
protected void initTags() {
this.m_CrossoverType = new SelectedTag(new String[]{"None", "Intermediate", "Discrete"});
this.m_CrossoverType = mutator.m_CrossoverType;
}
/** This method will enable you to clone a given mutation operator
@ -100,6 +110,7 @@ public class MutateESGlobal implements InterfaceMutation, java.io.Serializable {
* @param partners The original partners
*/
public void crossoverOnStrategyParameters(AbstractEAIndividual indy1, Population partners) {
if (m_CrossoverType!=MutateESCrossoverTypeEnum.none) {
ArrayList<Double> tmpList = new ArrayList<Double>();
if (indy1.getMutationOperator() instanceof MutateESGlobal) tmpList.add(new Double(((MutateESGlobal)indy1.getMutationOperator()).m_MutationStepSize));
for (int i = 0; i < partners.size(); i++) {
@ -108,19 +119,18 @@ public class MutateESGlobal implements InterfaceMutation, java.io.Serializable {
double[] list = new double[tmpList.size()];
for (int i = 0; i < tmpList.size(); i++) list[i] = ((Double)tmpList.get(i)).doubleValue();
if (list.length <= 1) return;
switch (this.m_CrossoverType.getSelectedTag().getID()) {
case 1 : {
switch (this.m_CrossoverType) {
case intermediate :
this.m_MutationStepSize = 0;
for (int i = 0; i < list.length; i++) this.m_MutationStepSize += list[i];
this.m_MutationStepSize = this.m_MutationStepSize/(double)list.length;
break;
}
case 2 : {
case discrete :
this.m_MutationStepSize = list[RNG.randomInt(0, list.length-1)];
break;
}
default : {
// do nothing
case none : // do nothing
break;
}
}
}
@ -195,10 +205,10 @@ public class MutateESGlobal implements InterfaceMutation, java.io.Serializable {
/** Set the value for tau1 with this method.
* @param d The mutation operator.
*/
public void setCrossoverType(SelectedTag d) {
public void setCrossoverType(MutateESCrossoverTypeEnum d) {
this.m_CrossoverType = d;
}
public SelectedTag getCrossoverType() {
public MutateESCrossoverTypeEnum getCrossoverType() {
return this.m_CrossoverType;
}
public String crossoverTypeTipText() {

View File

@ -41,7 +41,8 @@ public class SelectBestIndividuals implements InterfaceSelection, java.io.Serial
// nothing to prepare here
}
/** This method will select one Individual from the given
/**
* This method will select n best individuals from the given
* Population.
* @param population The source population where to select from
* @param size The number of Individuals to select

View File

@ -56,6 +56,7 @@ public class SelectBestSingle implements InterfaceSelection, java.io.Serializabl
int currentCriteria = 0, critSize;
double currentBestValue;
if (population.size()==0) return population; // trivial
critSize = ((AbstractEAIndividual)population.get(0)).getFitness().length;
result.setTargetSize(size);
if (this.m_ObeyDebsConstViolationPrinciple) {
@ -99,13 +100,21 @@ public class SelectBestSingle implements InterfaceSelection, java.io.Serializabl
/** This method allows you to select >size< partners for a given Individual
* @param dad The already selected parent
* @param avaiablePartners The mating pool.
* @param availablePartners The mating pool.
* @param size The number of partners needed.
* @return The selected partners.
*/
public Population findPartnerFor(AbstractEAIndividual dad, Population avaiablePartners, int size) {
if (excludeSelfAsPartner) return this.selectFrom(avaiablePartners.filter(new Population().addToPop(dad)), size);
else return this.selectFrom(avaiablePartners, size);
public Population findPartnerFor(AbstractEAIndividual dad, Population availablePartners, int size) {
if (excludeSelfAsPartner) {
Population newPartners = availablePartners.filter(new Population().addToPop(dad));
if (newPartners.size()==0) {
// no problem if crossover is deactivated
// EVAERROR.errorMsgOnce("Warning, no partners available when excluding self (SelectBestSingle.findPartnerFor)");
// EVAERROR.errorMsgOnce("Partners where: " + availablePartners.getStringRepresentation());
return newPartners;
}
return this.selectFrom(newPartners, size);
} else return this.selectFrom(availablePartners, size);
}
/**********************************************************************************************************************
* These are for GUI

View File

@ -73,6 +73,8 @@ public class HistoryConvergenceTerminator implements InterfaceTerminator, Serial
msg = "History did not improve" + (convergenceThreshold>0 ? (" by more than " + convergenceThreshold) : "") + " for " + haltingWindowLen + " iterations.";
}
}
} else {
if (haltingWindowLen > ((Population)pop).getMaxHistLength()) System.err.println("Warning, population history length not long enough for window length " + haltingWindowLen + " (HistoryConvergenceTerminator)");
}
return res;
}

View File

@ -2,8 +2,10 @@ package eva2.server.go.populations;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Set;
@ -22,6 +24,7 @@ import eva2.server.go.operators.postprocess.PostProcess;
import eva2.server.go.operators.selection.probability.AbstractSelProb;
import eva2.tools.EVAERROR;
import eva2.tools.Pair;
import eva2.tools.Serializer;
import eva2.tools.math.Mathematics;
import eva2.tools.math.RNG;
import eva2.tools.math.StatisticUtils;
@ -58,9 +61,10 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
protected int notifyEvalInterval = 0;
// additional data connected to the population
protected HashMap<String, Object> additionalPopData = null;
// historical best indidivuals may be traced
boolean useHistory = false;
private transient ArrayList<AbstractEAIndividual> m_History = new ArrayList<AbstractEAIndividual>();
// historical best indidivuals may be traced for a given number of generations. Set to -1 to trace all, set to 0 to not trace at all
int historyMaxLen = 0;
// boolean useHistory = false;
private transient LinkedList<AbstractEAIndividual> m_History = new LinkedList<AbstractEAIndividual>();
// remember when the last sorted queue was prepared
private int lastQModCount = -1;
@ -78,7 +82,7 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
}
/**
* Constructor setting initial capacity and population size to the given
* Constructor setting initial capacity and target population size to the given
* integer value.
*
* @param initialCapacity initial capacity and population size of the instance
@ -123,7 +127,7 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
public Population(InterfaceSolutionSet allSolutions) {
this(allSolutions.getCurrentPopulation().size()+allSolutions.getSolutions().size());
if (TRACE) System.err.println("TRACING POP");
addPopulation(allSolutions.getCurrentPopulation());
addPopulation(allSolutions.getCurrentPopulation(), false);
HashMap<Long, Integer> checkCols = new HashMap<Long, Integer>(size());
for (int i=0; i<size(); i++) {
checkCols.put(getEAIndividual(i).getIndyID(), 1);
@ -139,9 +143,13 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
setInitMethod(getInitMethod());
}
/**
* Make (shallow) copies of the given instance within the current instance.
* @param population
*/
public void copyHistAndArchive(Population population) {
if (population.m_Archive != null) this.m_Archive = (Population)population.m_Archive.clone();
if (population.m_History != null) this.m_History = (ArrayList<AbstractEAIndividual>)population.m_History.clone();
if (population.m_History != null) this.m_History = (LinkedList<AbstractEAIndividual>)population.m_History.clone();
if (population.additionalPopData!=null) {
this.additionalPopData = (HashMap<String, Object>)additionalPopData.clone();
if (population.additionalPopData.size()>0) {
@ -161,7 +169,7 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
this.m_Generation = population.m_Generation;
this.m_FunctionCalls = population.m_FunctionCalls;
this.m_TargetSize = population.m_TargetSize;
this.useHistory = population.useHistory;
this.historyMaxLen = population.historyMaxLen;
this.notifyEvalInterval = population.notifyEvalInterval;
this.initMethod = population.initMethod;
this.aroundDist = population.aroundDist;
@ -190,7 +198,7 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
if (this.m_Generation != opop.m_Generation) return false;
if (this.m_FunctionCalls != opop.m_FunctionCalls) return false;
if (this.m_TargetSize != opop.m_TargetSize) return false;
if (this.useHistory != opop.useHistory) return false;
if (this.historyMaxLen != opop.historyMaxLen) return false;
if (this.notifyEvalInterval != opop.notifyEvalInterval) return false;
if (this.initMethod != opop.initMethod) return false;
if (this.aroundDist != opop.aroundDist) return false;
@ -253,7 +261,7 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
* have been inited by a problem
*/
public void init() {
this.m_History = new ArrayList<AbstractEAIndividual>();
this.m_History = new LinkedList<AbstractEAIndividual>();
this.m_Generation = 0;
this.m_FunctionCalls = 0;
// evaluationTimeHashes = null;
@ -371,10 +379,19 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
* @param useHist
*/
public void setUseHistory(boolean useHist) {
useHistory = useHist;
if (useHist) setMaxHistoryLength(-1); // trace full history!
else setMaxHistoryLength(0); // dont trace at all!
// this.setUseHistory(useHist, (useHist ? (new AbstractEAIndividualComparator()) : null));
}
public boolean isUsingHistory() {
return historyMaxLen!=0;
}
public void setMaxHistoryLength(int len) {
historyMaxLen = len;
}
public int getMaxHistLength() {
return historyMaxLen;
}
// /**
// * Activate or deactivate the history tracking, which stores the best individual in every
// * generation in the incrGeneration() method.
@ -388,15 +405,15 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
// }
public int getHistoryLength() {
if (useHistory) return m_History.size();
if (historyMaxLen!=0) return m_History.size();
else return 0;
}
public ArrayList<AbstractEAIndividual> getHistory() {
public LinkedList<AbstractEAIndividual> getHistory() {
return m_History;
}
public void SetHistory(ArrayList<AbstractEAIndividual> theHist) {
public void SetHistory(LinkedList<AbstractEAIndividual> theHist) {
m_History = theHist;
}
@ -485,8 +502,12 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
* Stagnation measured etc. pp.
*/
public void incrGeneration() {
if (useHistory && (this.size() >= 1)) {
this.m_History.add((AbstractEAIndividual)this.getBestEAIndividual().clone());// TODO
if (isUsingHistory() && (this.size() >= 1)) {
if (historyMaxLen>0 && (m_History.size()>=historyMaxLen)) {
// oldest one must be replaced.. should be the one in front
m_History.removeFirst();
}
this.m_History.add((AbstractEAIndividual)this.getBestEAIndividual().clone());
}
for (int i=0; i<size(); i++) ((AbstractEAIndividual)get(i)).incrAge();
this.m_Generation++;
@ -527,17 +548,35 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
/** This method allows you to add a complete population to the current population.
* Note: After this operation the target population size may be exceeded.
* This method does not check for duplicate object pointers.
*
* @param pop The population that is to be added.
*/
public Population addPopulation(Population pop) {
return this.addPopulation(pop, false);
}
/**
* This method allows you to add a complete population to the current population.
* Note: After this operation the target population size may be exceeded.
* If indicated, this method checks for and avoids duplicate object pointers.
*
* @param pop The population that is to be added.
* @param avoidDuplicatePointers if true, duplicate object pointers are forbidden
*/
public Population addPopulation(Population pop, boolean avoidDuplicatePointers) {
if (pop != null) {
for (int i = 0; i < pop.size(); i++) {
AbstractEAIndividual indy = (AbstractEAIndividual)pop.get(i);
if (avoidDuplicatePointers && this.contains(indy)) {
System.err.println("Warning, duplicate indy avoided in Population.addPopulation! Index of " + this.indexOf(indy));
} else {
if (indy != null) {
this.add(indy);
}
}
}
}
return this;
}
@ -1058,10 +1097,10 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
* Sort the population returning a new ArrayList.
* The returned array should not be modified!
*
* @param comp A comparator by which sorting is performed
* @param comp A comparator by which sorting is performed - it should work on AbstractEAIndividual instances.
* @return
*/
public ArrayList<AbstractEAIndividual> getSorted(AbstractEAIndividualComparator comp) {
public ArrayList<AbstractEAIndividual> getSorted(Comparator<Object> comp) {
if (super.size()==0) return new ArrayList<AbstractEAIndividual>();
PriorityQueue<AbstractEAIndividual> sQueue = new PriorityQueue<AbstractEAIndividual>(super.size(), comp);
for (int i = 0; i < super.size(); i++) {
@ -1206,7 +1245,7 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
* This method relies on the implementation of the equals method
* in the individuals.
*/
public void removeDoubleInstances() {
public void removeRedundantIndies() {
for (int i = 0; i < this.size(); i++) {
for (int j = i+1; j < this.size(); j++) {
if (((AbstractEAIndividual)this.get(i)).equals(this.get(j))) {
@ -1217,9 +1256,34 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
}
}
public Population removeRedundantIndiesAsNew() {
Population pop = this.cloneShallowInds();
pop.removeRedundantIndies();
return pop;
}
/**
* Return the number of individuals within this instance that are
* seen as being equal by the equals relation of the individual.
*
* @return the number of equal individuals
*
*/
public int getRedundancyCount() {
int redund = 0;
for (int i=0; i<size()-1; i++) {
for (int j=i+1; j<size(); j++) {
if (getEAIndividual(i).equals(getEAIndividual(j))) {
redund++;
break; // jump because the i-th is redundant and we dont want to count all redundant pairs
}
}
}
return redund;
}
/** This method will remove instances with equal fitness from the population.
*/
public void removeDoubleInstancesUsingFitness() {
public void removeRedundantIndiesUsingFitness() {
for (int i = 0; i < this.size(); i++) {
for (int j = i+1; j < this.size(); j++) {
if (((AbstractEAIndividual)this.get(i)).equalFitness((AbstractEAIndividual)this.get(j))) {
@ -1477,9 +1541,17 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
return false;
}
public void removeMembers(Population tmpPop, boolean errorOnMissing) {
for (int i=0; i<tmpPop.size(); i++) {
if (!removeMember(tmpPop.getEAIndividual(i))) {
/**
* Remove a set of individuals from the instance. If indicated by the errorOnMissing-flag
* a RuntimeException is thrown as soon as one individual to be removed is not contained
* in the instance. Otherwise, this is ignored.
*
* @param tmpPop
* @param errorOnMissing
*/
public void removeMembers(Population popToRemove, boolean errorOnMissing) {
for (int i=0; i<popToRemove.size(); i++) {
if (!removeMember(popToRemove.getEAIndividual(i))) {
if (errorOnMissing) throw new RuntimeException("Error, member to be removed was missing (Population.removeMembers)!");
}
}
@ -1584,6 +1656,42 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
return res;
}
/**
* Return the minimal, maximal, average, median, and variance of correlations between any two solutions.
* @return
*/
public double[] getCorrelations() {
return Population.getCorrelations(this);
}
/**
* Return the minimal, maximal, average, median, and variance of correlations between any two solutions.
*
* @param pop the population instance to look at
* @return
*/
public static double[] getCorrelations(Population pop) {
double[] cors = new double[pop.size()*(pop.size()-1)/2];
if (pop.size()<2) {
return new double[]{1.,1.,1.,1.};
}
int index=0;
double corsSum=0, minCor = 10., maxCor=-10.;
for (int i = 0; i < pop.size()-1; i++) {
for (int j = i+1; j < pop.size(); j++) {
double cor = StatisticUtils.correlation(pop.getEAIndividual(i).getDoublePosition(), pop.getEAIndividual(j).getDoublePosition());
cors[index++] = cor;
corsSum += cor;
if (cor>maxCor) maxCor=cor;
if (cor<minCor) minCor=cor;
}
}
double var = StatisticUtils.variance(cors, true);
double[] res = new double[] {minCor, maxCor, corsSum/cors.length, Mathematics.median(cors, false), var};
return res;
}
/**
* Returns the average, minimal and maximal individual fitness and std dev. for the population in the given criterion.
*
@ -1673,6 +1781,7 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
double dist = -1.;
int sel=-1;
for (int i = 0; i < pop.size(); i++) {
if (pop.getEAIndividual(i)!=null) {
double curDist = metric.distance(refIndy, pop.getEAIndividual(i));
if ((dist<0) || (!closestOrFarthest && (dist < curDist))
|| (closestOrFarthest && (dist > curDist))) {
@ -1680,6 +1789,7 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
sel = i;
}
}
}
return new Pair<Integer,Double>(sel,dist);
}
@ -1693,7 +1803,14 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
*/
public boolean isWithinPopDist(AbstractEAIndividual indy, double d, InterfaceDistanceMetric metric) {
Pair<Integer,Double> closest = Population.getClosestFarthestIndy(indy, this, metric, true);
return (closest.tail()<=d);
if (closest.tail()<=d) {
return true;
} else {
// if (size()==1) {
// System.out.println("dist is " + metric.distance(getEAIndividual(0), indy));
// }
return false;
}
}
/**
@ -1711,13 +1828,29 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
return centerPos;
}
/**
* Return an individual which is located at the center of the population.
* The fitness will be invalid.
*
* @return
*/
public IndividualInterface getCenterIndy() {
AbstractEAIndividual indy = (AbstractEAIndividual)getEAIndividual(0).clone();
double[] center = getCenter();
indy.setDoublePosition(indy, center);
indy.SetFitness(null);
return indy;
}
/**
* Calculate the weighted center position of the population. Weights must add up to one!
*
* @return the average position of the population
*/
public double[] getCenterWeighted(double[] weights) {
if (size()==0 || (weights.length > size())) EVAERROR.errorMsgOnce("Invalid pop size in DistractingPopulation:getCenterWeighted!");
if (size()==0 || (weights.length > size()) || (weights.length==0)) {
EVAERROR.errorMsgOnce("Invalid pop size in DistractingPopulation:getCenterWeighted!");
}
double[] centerPos = AbstractEAIndividual.getDoublePosition(getEAIndividual(0));
Mathematics.svMult(weights[0], centerPos, centerPos);
for (int i=1; i<weights.length; i++) {
@ -2005,6 +2138,56 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
}
}
/**
* Return true if the current instance is a subset of the given population, otherwise false.
*
* @param offspring
* @return
*/
public boolean isSubSet(Population set) {
Population filtered = this.filter(set); // if this is a subset of s, filtered must be empty.
return (filtered.size()==0);
}
/**
* Return the set of individuals which are members of both this and the other population.
* Uses the individuals equals predicate.
*
* @param other
* @return
*/
public Population setCut(Population other) {
Population cut = new Population();
for (int i=0; i<size(); i++) {
if (other.indexOf(getEAIndividual(i))>=0) cut.add(getEAIndividual(i));
}
return cut;
}
/**
* Copy all additional population data of the given instance
* into the current instance (by references only).
*
* @param pop
*/
public void copyHashData(Population pop) {
if (pop!=null && (pop.additionalPopData!=null)) {
for (String key : pop.additionalPopData.keySet()) {
Object origData = pop.getData(key);
Object maybeClone = Serializer.deepClone(origData);
if (maybeClone!=null) putData(key, maybeClone);
else {
System.err.println("Warning, additional pop data could not be cloned!");
putData(key, origData);
}
}
}
}
public void clearHistory() {
m_History.clear();
}
// /**
// * Mark the population at the current state as evaluated. Changes to the modCount or hashes of individuals
// * will invalidate the mark.

View File

@ -240,7 +240,7 @@ implements InterfaceOptimizationProblem /*, InterfaceParamControllable*/, Serial
* @return String
*/
public String[] getAdditionalFileStringHeader(PopulationInterface pop) {
if (this instanceof InterfaceInterestingHistogram) return new String[]{"Solution","Histogram(c0)","Score"};
if (this instanceof InterfaceInterestingHistogram) return new String[]{"Solution","Histogram","Score"};
else return new String[]{"Solution"};
}

View File

@ -494,7 +494,7 @@ public abstract class AbstractProblemDouble extends AbstractOptimizationProblem
@Override
public String[] getAdditionalFileStringHeader(PopulationInterface pop) {
String[] superHeader = super.getAdditionalFileStringHeader(pop);
if (isWithConstraints()) return ToolBox.appendArrays(superHeader, new String[]{"RawFit.","Num.Viol.","Sum.Viol."});
if (isWithConstraints()) return ToolBox.appendArrays(superHeader, new String[]{"rawFit","numViol","sumViol"});
else return superHeader;
}

View File

@ -2,6 +2,8 @@ package eva2.server.go.strategies;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.PriorityQueue;
import eva2.gui.BeanInspector;
@ -17,7 +19,6 @@ import eva2.server.go.individuals.InterfaceDataTypeDouble;
import eva2.server.go.operators.cluster.ClusteringDensityBased;
import eva2.server.go.operators.cluster.InterfaceClustering;
import eva2.server.go.operators.distancemetric.ObjectiveSpaceMetric;
//import eva2.server.go.populations.Distraction;
import eva2.server.go.populations.Population;
import eva2.server.go.populations.SolutionSet;
import eva2.server.go.problems.B1Problem;
@ -327,7 +328,7 @@ public class ClusterBasedNichingEA implements InterfacePopulationChangedEventLis
* @return True if converged.
*/
private boolean testSpeciesForConvergence(Population pop) {
ArrayList<AbstractEAIndividual> speciesHistory = pop.getHistory();
List<AbstractEAIndividual> speciesHistory = pop.getHistory();
int histLen = speciesHistory.size();
if (histLen <= haltingWindow) {
@ -780,10 +781,10 @@ public class ClusterBasedNichingEA implements InterfacePopulationChangedEventLis
newSp.setUseHistory(true);
if (startAtP1Gen) { // start explicitely as a child population of p1
newSp.setGenerationTo(parentSp.getGeneration());
newSp.SetHistory((ArrayList<AbstractEAIndividual>) parentSp.getHistory().clone());
newSp.SetHistory((LinkedList<AbstractEAIndividual>) parentSp.getHistory().clone());
} else { // start anew (from undiff)
newSp.setGenerationTo(0);
newSp.SetHistory(new ArrayList<AbstractEAIndividual>());
newSp.SetHistory(new LinkedList<AbstractEAIndividual>());
}
if (m_Optimizer instanceof InterfaceSpeciesAware) ((InterfaceSpeciesAware)m_Optimizer).splitFromFirst(parentSp, newSp);

View File

@ -310,7 +310,7 @@ public class Processor extends Thread implements InterfaceProcessor, InterfacePo
else if (terminator instanceof EvaluationTerminator)
args = new Object[] {optimizer, optimizer.getPopulation(), optimizer.getPopulation().getFunctionCalls(), ((EvaluationTerminator)terminator).getFitnessCalls()};
// ((InterfaceParameterControl)paramCtrl).updateParameters(optimizer, optimizer.getPopulation().getFunctionCalls(), ((EvaluationTerminator)terminator).getFitnessCalls());
else args = null;//new Object[]{optimizer, optimizer.getPopulation()};
else args = new Object[]{optimizer, optimizer.getPopulation()};
// ((InterfaceParameterControl)paramCtrl).updateParameters(optimizer);
if (args != null) { // only if iteration counting is available

View File

@ -305,6 +305,9 @@ public abstract class AbstractStatistics implements InterfaceTextListener, Inter
} else {
if (printRunStoppedVerbosity()) printToTextListener(" NO feasible individual found.\n");
}
if (printRunStoppedVerbosity()) {
printToTextListener(" Solution correlations (min,max,avg,med,var): " + BeanInspector.toString(((Population)lastSols).getCorrelations((Population)lastSols)) + "\n");
}
if (bestOfRunFeasibleIndy != null) {
runBestFeasibleList.add(bestOfRunFeasibleIndy);
// if (meanBestFeasibleFit==null) {
@ -666,8 +669,8 @@ public abstract class AbstractStatistics implements InterfaceTextListener, Inter
case meanFit: ret[i] = (currentMeanFit==null) ? Double.NaN : currentMeanFit[defaultFitCriterion]; break;
case currentWorst: ret[i] = (currentWorstFit==null) ? Double.NaN : currentWorstFit[defaultFitCriterion]; break;
case runBest: ret[i] = bestOfRunIndy.getFitness()[defaultFitCriterion]; break;
case currentBestFeasible: ret[i] = currentBestFeasibleFit[defaultFitCriterion]; break;
case runBestFeasible: ret[i] = bestOfRunFeasibleIndy.getFitness()[defaultFitCriterion]; break;
case currentBestFeasible: ret[i] = (currentBestFeasibleFit==null) ? Double.NaN : currentBestFeasibleFit[defaultFitCriterion]; break;
case runBestFeasible: ret[i] = (bestOfRunFeasibleIndy==null) ? Double.NaN : bestOfRunFeasibleIndy.getFitness()[defaultFitCriterion]; break;
case avgPopDistance: ret[i] = currentAvgPopDist; break;
case maxPopDistance: ret[i] = currentMaxPopDist; break;
}

View File

@ -241,7 +241,10 @@ public class StatisticsWithGUI extends AbstractStatistics implements Serializabl
if (lastIsShowFull) colIndex = 1+graphDesc.get(i).tail;
// plot the column as indicated by the graph description
if (currentStatDoubleData[colIndex]!=null) plotFitnessPoint(0, subGraph++, functionCalls, currentStatDoubleData[colIndex]);
else EVAERROR.errorMsgOnce("Error, data field " + graphDesc.get(i).head + " does not contain primitive data and cannot be plotted.");
else {
EVAERROR.errorMsgOnce("Error, data field " + graphDesc.get(i).head + " does not contain primitive data and cannot be plotted.");
subGraph++; // increase index anyways or the name assignment gets inconsistent
}
}
// }
}

View File

@ -12,6 +12,8 @@ package eva2.tools;
/*==========================================================================*
* IMPORTS
*==========================================================================*/
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
@ -68,6 +70,35 @@ public class Serializer {
return ret;
}
/**
* Returns a copy of the object, or null if the object cannot
* be serialized.
*/
public static Object deepClone(Object orig) {
Object obj = null;
try {
// Write the object out to a byte array
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bos);
out.writeObject(orig);
out.flush();
out.close();
// Make an input stream from the byte array and read
// a copy of the object back in.
ObjectInputStream in = new ObjectInputStream(
new ByteArrayInputStream(bos.toByteArray()));
obj = in.readObject();
}
catch(IOException e) {
e.printStackTrace();
}
catch(ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
return obj;
}
/**
* Use object serialization to make a "deep clone" of the object o.
@ -77,35 +108,35 @@ public class Serializer {
* usually implemented to produce a "shallow" clone that copies references
* to other objects, instead of copying all referenced objects.
**/
static public Object deepclone(final Serializable o) throws IOException, ClassNotFoundException {
// Create a connected pair of "piped" streams.
// We'll write bytes to one, and them from the other one.
final PipedOutputStream pipeout = new PipedOutputStream();
PipedInputStream pipein = new PipedInputStream(pipeout);
// Now define an independent thread to serialize the object and write
// its bytes to the PipedOutputStream
Thread writer = new Thread() {
public void run() {
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(pipeout);
out.writeObject(o); }
catch(IOException e) {
System.out.println("ERROR in Serialization1"+ e.getMessage());
}
finally {
try { out.close(); } catch (Exception e) {
System.out.println("ERROR in Serialization2"+ e.getMessage());
}
}
}
};
writer.start();
// Meanwhile, in this thread, read and deserialize from the piped
// input stream. The resulting object is a deep clone of the original.
ObjectInputStream in = new ObjectInputStream(pipein);
return in.readObject();
}
// static public Object deepclone(final Serializable o) throws IOException, ClassNotFoundException {
// // Create a connected pair of "piped" streams.
// // We'll write bytes to one, and them from the other one.
// final PipedOutputStream pipeout = new PipedOutputStream();
// PipedInputStream pipein = new PipedInputStream(pipeout);
// // Now define an independent thread to serialize the object and write
// // its bytes to the PipedOutputStream
// Thread writer = new Thread() {
// public void run() {
// ObjectOutputStream out = null;
// try {
// out = new ObjectOutputStream(pipeout);
// out.writeObject(o); }
// catch(IOException e) {
// System.out.println("ERROR in Serialization1"+ e.getMessage());
// }
// finally {
// try { out.close(); } catch (Exception e) {
// System.out.println("ERROR in Serialization2"+ e.getMessage());
// }
// }
// }
// };
// writer.start();
// // Meanwhile, in this thread, read and deserialize from the piped
// // input stream. The resulting object is a deep clone of the original.
// ObjectInputStream in = new ObjectInputStream(pipein);
// return in.readObject();
// }
/**
* This is a simple serializable data structure that we use below for
* testing the methods above
@ -123,8 +154,6 @@ public class Serializer {
}
}
/** This class defines a main() method for testing */
public static class Test {
public static void main(String[] args) throws IOException, ClassNotFoundException {
// Create a simple object graph
DataStructure ds = new DataStructure();
@ -144,11 +173,11 @@ public class Serializer {
System.out.println("Read from the file: " + ds);
// Create a deep clone and display that. After making the copy
// modify the original to prove that the clone is "deep".
DataStructure ds2 = (DataStructure) Serializer.deepclone(ds);
DataStructure ds2 = (DataStructure) Serializer.deepClone(ds);
ds.other.message = null; ds.other.data = null; // Change original
System.out.println("Deep clone: " + ds2);
}
}
/**
* Serialize the string s and
* store its serialized state in File with name Filename.

View File

@ -485,7 +485,7 @@ public class Mathematics {
* @return the median
*/
public static double[] median(List<double[]> dblArrList, boolean interpolate) {
java.util.Collections.sort(dblArrList, new DoubleArrayComparator());
java.util.Collections.sort(dblArrList, new DoubleArrayComparator()); // by default, the comparator uses pareto dominance
int len = dblArrList.size();
if (len % 2 != 0) return dblArrList.get((len-1) / 2);
@ -1081,6 +1081,21 @@ public class Mathematics {
vvAddOffs(v1, 0, v2, 0, res, 0, v1.length);
}
/**
* Calculate r=1/2 * sqrt(sum(sqr(upperBound_i - lowerBound_i))).
*
* @param range
* @return the average length of the range intervals
*/
public static double getAvgRangeL2(double[][] range) {
double sum = 0.;
for (int i=0; i<range.length; i++) {
double d=(range[i][1]-range[i][0]);
sum+=(d*d);
}
return Math.sqrt(sum)/2.;
}
/**
* Add vectors in place setting with an offset within the target
* vector, meaning that res[resOffs+i]=v1[v1Offs+i]+v2[v2Offs+i] for i in length.
@ -1159,4 +1174,17 @@ public class Mathematics {
Arrays.fill(result, 0, result.length-1, 0.);
return result;
}
/**
* Create a double vector of length dim filled with value d.
*
* @param d
* @param dim
* @return a double vector of length dim filled with value d
*/
public static double[] makeVector(double d, int dim) {
double[] ret = new double[dim];
Arrays.fill(ret, d);
return ret;
}
}

View File

@ -17,6 +17,8 @@ package eva2.tools.math;
import java.util.ArrayList;
import java.util.Collections;
import eva2.gui.BeanInspector;
import eva2.server.go.problems.AbstractProblemDouble;
import eva2.tools.math.Jama.Matrix;
/**
@ -38,10 +40,12 @@ public class StatisticUtils
* @param n the length of two double vectors
* @return the correlation coefficient
*/
public final static double correlation(double y1[],double y2[],int n) {
public final static double correlation(double y1[],double y2[]) {
int i;
double av1 = 0.0, av2 = 0.0, y11 = 0.0, y22 = 0.0, y12 = 0.0, c;
int n=y1.length;
if (n!=y2.length) throw new RuntimeException("Error, mismatching vectors for correlation calculation in StatisticUtils.correlation(double[], double[])");
if (n <= 1) {
return 1.0;
@ -230,21 +234,24 @@ public class StatisticUtils
* Computes the variance for an array of doubles.
*
* @param vector the array
* @param finiteSet if true, the vector is interpreted as complete data set, otherwise a set of samples of a larger set.
* @return the variance
*/
public static double variance(double[] vector) {
public static double variance(double[] vector, boolean finiteSet) {
double sum = 0, sumSquared = 0;
if (vector.length <= 1) {
int n=vector.length;
if (n <= 1) {
return 0;
}
for (int i = 0; i < vector.length; i++) {
for (int i = 0; i < n; i++) {
sum += vector[i];
sumSquared += (vector[i] * vector[i]);
}
double result = (sumSquared - (sum * sum / (double) vector.length)) /
(double) (vector.length - 1);
double denom;
if (finiteSet) denom=n;
else denom=(n-1);
double result = (sumSquared - (sum * sum / (double) n)) / denom;
// We don't like negative variance
if (result < 0) {
@ -371,8 +378,7 @@ public class StatisticUtils
*
* @author Joerg K. Wegner
*/
public static double getCorrelationCoefficient(double array1[], double array2[])
{
public static double getCorrelationCoefficient(double array1[], double array2[]) {
if ((array1 == null) || (array2 == null)) { return -2.0; }
double sumA = 0;
@ -406,48 +412,236 @@ public class StatisticUtils
* Main method for testing this class.
*/
public static void main(String[] ops) {
double[][] vs = {{ 356.873,-498.563, 130.923,-223.684, 494.296, 380.739, 481.353, 344.571, 501.105,-166.109},
{ 199.116,-423.448, 487.898, 344.579, 140.709, 89.007,-259.310, 512.000, 152.069,-440.778},
{ 439.128, 112.370,-422.820,-43.119,-297.609,-438.940, 488.914,-512.000,-407.847, 386.611},
{ 395.772, 191.634,-511.999,-93.078,-282.853,-444.621, 491.291,-512.000,-407.620, 386.493}};
// System.out.println("test (0.5, 100): " +
// StatisticUtils.test(100));
vs[1]=Mathematics.rotate(vs[0], AbstractProblemDouble.initDefaultRotationMatrix(10, 10));
for (int i=0; i<vs.length; i++) {
for (int j=0; j<vs.length; j++) {
System.out.print("\t" + correlation(vs[i], vs[j]));
}
System.out.println();
}
// vs[1]=Mathematics.rotate(vs[0], AbstractProblemDouble.initDefaultRotationMatrix(30, 10));
// vs[2]=Mathematics.rotate(vs[0], AbstractProblemDouble.initDefaultRotationMatrix(50, 10));
// vs[3]=Mathematics.rotate(vs[0], AbstractProblemDouble.initDefaultRotationMatrix(70, 10));
vs = new double[][]{{0.1,0.9},
{0.8,0.2},
// {0.8,0.2},
{0.0,1.0}};
System.out.println("---");
// for (int i=0; i<vs.length; i++) {
// Mathematics.svAdd(512, vs[i], vs[i]);
// Mathematics.normVect(vs[i], vs[i]);
// System.out.println(BeanInspector.toString(vs[i]));
// }
// System.out.println("entropy cols: " + entropyOverColumns(vs));
// System.out.println("entropy rows: " + entropyOverRows(vs));
}
// The following methods got mysteriously lost maybe during cvs-svn refactoring.
// For the time being I add method thunks which give a warning when called. (mkron)
public static double quadratic_entropy(double[] ds) {
// TODO Auto-generated method stub
System.err.println("warning, not implemented!");
/**
* Computes the entropy of the given array.
*
* @param array an array of double data
* @return the entropy
*/
public static double entropy(double[] array) {
double returnValue = 0;
double sum = 0;
for (int i = 0; i < array.length; i++) {
returnValue -= lnFunc(array[i]);
sum += array[i];
}
if (StatisticUtils.eq(sum, 0)) {
return 0;
} else {
return (returnValue + lnFunc(sum)) / (sum * log2);
}
}
public static double mutual_information(double[] ds, double[] ds2, int nbins) {
// TODO Auto-generated method stub
System.err.println("warning, not implemented!");
// these came from ContingencyTables.java in the wsi package (mkron)
// /**
// * Computes conditional entropy of the rows given
// * the columns.
// *
// * @param matrix the contingency table
// * @return the conditional entropy of the rows given the columns
// */
// public static double entropyConditionedOnColumns(double[][] matrix) {
// double ret = 0;
// double colSum;
// double total = 0;
//
// for (int j = 0; j < matrix[0].length; j++) {
// colSum = 0;
//
// for (int i = 0; i < matrix.length; i++) {
// ret = ret + lnFunc(matrix[i][j]);
// colSum += matrix[i][j];
// }
//
// ret = ret - lnFunc(colSum);
// total += colSum;
// }
//
// if (StatisticUtils.eq(total, 0)) {
// return 0;
// }
//
// return -ret / (total * log2);
// }
// /**
// * Computes conditional entropy of the columns given
// * the rows.
// *
// * @param matrix the contingency table
// * @return the conditional entropy of the columns given the rows
// */
// public static double entropyConditionedOnRows(double[][] matrix) {
// double returnValue = 0;
// double sumForRow;
// double total = 0;
//
// for (int i = 0; i < matrix.length; i++) {
// sumForRow = 0;
//
// for (int j = 0; j < matrix[0].length; j++) {
// returnValue = returnValue + lnFunc(matrix[i][j]);
// sumForRow += matrix[i][j];
// }
// returnValue = returnValue - lnFunc(sumForRow);
// total += sumForRow;
// }
//
// if (StatisticUtils.eq(total, 0)) {
// return 0;
// }
//
// return -returnValue / (total * log2);
// }
// /**
// * Computes the columns' entropy for the given contingency table.
// *
// * @param matrix the contingency table
// * @return the columns' entropy
// */
// public static double entropyOverColumns(double[][] matrix)
// {
// double returnValue = 0;
// double sumForColumn;
// double total = 0;
//
// for (int j = 0; j < matrix[0].length; j++)
// {
// sumForColumn = 0;
//
// for (int i = 0; i < matrix.length; i++)
// {
// sumForColumn += matrix[i][j];
// }
//
// returnValue = returnValue - lnFunc(sumForColumn);
// total += sumForColumn;
// }
//
// if (StatisticUtils.eq(total, 0))
// {
// return 0;
// }
//
// return (returnValue + lnFunc(total)) / (total * log2);
// }
// /**
// * Computes the rows' entropy for the given contingency table.
// *
// * @param matrix the contingency table
// * @return the rows' entropy
// */
// public static double entropyOverRows(double[][] matrix)
// {
// double returnValue = 0;
// double sumForRow;
// double total = 0;
//
// for (int i = 0; i < matrix.length; i++)
// {
// sumForRow = 0;
//
// for (int j = 0; j < matrix[0].length; j++)
// {
// sumForRow += matrix[i][j];
// }
//
// returnValue = returnValue - lnFunc(sumForRow);
// total += sumForRow;
// }
//
// if (StatisticUtils.eq(total, 0))
// {
// return 0;
// }
//
// return (returnValue + lnFunc(total)) / (total * log2);
// }
private static double lnFunc(double num) {
// hard coded for efficiency reasons
if (num < 1e-7) {
return 0;
} else {
return num * Math.log(num);
}
}
public static double quadratic_mutinf(double[] feature, double[] labels) {
// TODO Auto-generated method stub
System.err.println("warning, not implemented!");
return 0;
}
public static double quadratic_mutinf(double[] feature, double[] labels, int[] classes) {
// TODO Auto-generated method stub
System.err.println("warning, not implemented!");
return 0;
}
public static double SUquadratic(double[] feature, double[] labels) {
// TODO Auto-generated method stub
System.err.println("warning, not implemented!");
return 0;
}
public static double SUquadratic(double[] feature, double[] labels, int[] classes) {
// TODO Auto-generated method stub
System.err.println("warning, not implemented!");
return 0;
}
// // The following methods got mysteriously lost maybe during cvs-svn refactoring.
// // For the time being I add method thunks which give a warning when called. (mkron)
// public static double quadratic_entropy(double[] ds) {
// // TODO Auto-generated method stub
// System.err.println("warning, not implemented!");
// return 0;
// }
//
// public static double mutual_information(double[] ds, double[] ds2, int nbins) {
// // TODO Auto-generated method stub
// System.err.println("warning, not implemented!");
// return 0;
// }
//
// public static double quadratic_mutinf(double[] feature, double[] labels) {
// // TODO Auto-generated method stub
// System.err.println("warning, not implemented!");
// return 0;
// }
//
// public static double quadratic_mutinf(double[] feature, double[] labels, int[] classes) {
// // TODO Auto-generated method stub
// System.err.println("warning, not implemented!");
// return 0;
// }
//
// public static double SUquadratic(double[] feature, double[] labels) {
// // TODO Auto-generated method stub
// System.err.println("warning, not implemented!");
// return 0;
// }
//
// public static double SUquadratic(double[] feature, double[] labels, int[] classes) {
// // TODO Auto-generated method stub
// System.err.println("warning, not implemented!");
// return 0;
// }
/**
* Random Latin Hypercube Sampling within a given double range.