Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplified wildcard capture in EvolutionObserver.populationUpdate(). Added PopulationData.getPopulation(). #9

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 9 additions & 14 deletions examples/nb-configuration.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ That way multiple projects can share the same settings (useful for formatting ru
Any value defined here will override the pom.xml file value but is only applicable to the current project.
-->
<org-netbeans-modules-editor-indent.CodeStyle.usedProfile>project</org-netbeans-modules-editor-indent.CodeStyle.usedProfile>
<org-netbeans-modules-editor-indent.CodeStyle.project.spaces-per-tab>2</org-netbeans-modules-editor-indent.CodeStyle.project.spaces-per-tab>
<org-netbeans-modules-editor-indent.CodeStyle.project.tab-size>2</org-netbeans-modules-editor-indent.CodeStyle.project.tab-size>
<org-netbeans-modules-editor-indent.CodeStyle.project.indent-shift-width>2</org-netbeans-modules-editor-indent.CodeStyle.project.indent-shift-width>
<org-netbeans-modules-editor-indent.CodeStyle.project.expand-tabs>false</org-netbeans-modules-editor-indent.CodeStyle.project.expand-tabs>
<org-netbeans-modules-editor-indent.CodeStyle.project.spaces-per-tab>4</org-netbeans-modules-editor-indent.CodeStyle.project.spaces-per-tab>
<org-netbeans-modules-editor-indent.CodeStyle.project.tab-size>4</org-netbeans-modules-editor-indent.CodeStyle.project.tab-size>
<org-netbeans-modules-editor-indent.CodeStyle.project.indent-shift-width>4</org-netbeans-modules-editor-indent.CodeStyle.project.indent-shift-width>
<org-netbeans-modules-editor-indent.CodeStyle.project.expand-tabs>true</org-netbeans-modules-editor-indent.CodeStyle.project.expand-tabs>
<org-netbeans-modules-editor-indent.CodeStyle.project.text-limit-width>100</org-netbeans-modules-editor-indent.CodeStyle.project.text-limit-width>
<org-netbeans-modules-editor-indent.CodeStyle.project.text-line-wrap>words</org-netbeans-modules-editor-indent.CodeStyle.project.text-line-wrap>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.blankLinesAfterClassHeader>0</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.blankLinesAfterClassHeader>
Expand All @@ -29,33 +29,28 @@ Any value defined here will override the pom.xml file value but is only applicab
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapMethodParams>WRAP_IF_LONG</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapMethodParams>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapTernaryOps>WRAP_IF_LONG</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapTernaryOps>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapThrowsKeyword>WRAP_IF_LONG</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapThrowsKeyword>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.spaces-per-tab>2</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.spaces-per-tab>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapAssignOps>WRAP_IF_LONG</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapAssignOps>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.placeElseOnNewLine>true</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.placeElseOnNewLine>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapAssignOps>WRAP_IF_LONG</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapAssignOps>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapEnumConstants>WRAP_IF_LONG</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapEnumConstants>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.placeFinallyOnNewLine>true</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.placeFinallyOnNewLine>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.placeCatchOnNewLine>true</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.placeCatchOnNewLine>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.placeFinallyOnNewLine>true</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.placeFinallyOnNewLine>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.classDeclBracePlacement>NEW_LINE</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.classDeclBracePlacement>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapMethodCallArgs>WRAP_IF_LONG</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapMethodCallArgs>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapDisjunctiveCatchTypes>WRAP_IF_LONG</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapDisjunctiveCatchTypes>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapMethodCallArgs>WRAP_IF_LONG</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapMethodCallArgs>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapChainedMethodCalls>WRAP_IF_LONG</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapChainedMethodCalls>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapFor>WRAP_IF_LONG</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapFor>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapArrayInit>WRAP_IF_LONG</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapArrayInit>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapAssert>WRAP_IF_LONG</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapAssert>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapBinaryOps>WRAP_IF_LONG</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapBinaryOps>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapAnnotationArgs>WRAP_IF_LONG</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapAnnotationArgs>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.indent-shift-width>2</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.indent-shift-width>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.placeWhileOnNewLine>true</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.placeWhileOnNewLine>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.text-limit-width>100</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.text-limit-width>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapExtendsImplementsList>WRAP_IF_LONG</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapExtendsImplementsList>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.generateParagraphTagOnBlankLines>true</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.generateParagraphTagOnBlankLines>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.redundantWhileBraces>LEAVE_ALONE</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.redundantWhileBraces>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.tab-size>2</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.tab-size>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.continuationIndentSize>2</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.continuationIndentSize>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.expand-tabs>false</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.expand-tabs>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.continuationIndentSize>4</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.continuationIndentSize>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.otherBracePlacement>NEW_LINE</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.otherBracePlacement>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.methodDeclBracePlacement>NEW_LINE</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.methodDeclBracePlacement>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.redundantDoWhileBraces>LEAVE_ALONE</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.redundantDoWhileBraces>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapThrowsList>WRAP_IF_LONG</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapThrowsList>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.blankLinesBeforeMethods>2</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.blankLinesBeforeMethods>
</properties>
</project-shared-configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,15 @@

import java.awt.Container;
import java.lang.reflect.InvocationTargetException;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.*;

/**
* Base class for examples that run as applets.
* <p/>
* @author Daniel Dyer
*/
public abstract class AbstractExampleApplet extends JApplet
{
/**
* {@inheritDoc}
*/
@Override
public void init()
{
Expand Down Expand Up @@ -74,7 +68,8 @@ public void run()
catch (InvocationTargetException ex)
{
ex.getCause().printStackTrace();
JOptionPane.showMessageDialog(container, ex.getCause(), "Error Occurred", JOptionPane.ERROR_MESSAGE);
JOptionPane.showMessageDialog(container, ex.getCause(), "Error Occurred",
JOptionPane.ERROR_MESSAGE);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,22 @@
import org.uncommons.watchmaker.framework.islands.IslandEvolutionObserver;

/**
* Trivial evolution observer for displaying information at the end
* of each generation.
* Trivial evolution observer for displaying information at the end of each generation.
* <p/>
* @param <T> The type of entity being evolved.
* @author Daniel Dyer
*/
public class EvolutionLogger<T> implements IslandEvolutionObserver<T>
{
public void populationUpdate(PopulationData<? extends T> data)
public <S extends T> void populationUpdate(PopulationData<S> data)
{
System.out.println("Generation " + data.getGenerationNumber() + ": " + data.getBestCandidateFitness());
System.out.println("Generation " + data.getGenerationNumber() + ": " + data.
getBestCandidateFitness());
}


public void islandPopulationUpdate(int islandIndex, PopulationData<? extends T> populationData)
public <S extends T> void islandPopulationUpdate(int islandIndex,
PopulationData<S> populationData)
{
// Do nothing.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,13 @@
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.concurrent.TimeUnit;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.SpringLayout;
import javax.swing.SwingUtilities;
import javax.swing.*;
import org.uncommons.maths.random.MersenneTwisterRNG;
import org.uncommons.maths.random.Probability;
import org.uncommons.swing.SpringUtilities;
import org.uncommons.swing.SwingBackgroundTask;
import org.uncommons.watchmaker.examples.AbstractExampleApplet;
import org.uncommons.watchmaker.framework.EvolutionEngine;
import org.uncommons.watchmaker.framework.EvolutionObserver;
import org.uncommons.watchmaker.framework.EvolutionaryOperator;
import org.uncommons.watchmaker.framework.GenerationalEvolutionEngine;
import org.uncommons.watchmaker.framework.PopulationData;
import org.uncommons.watchmaker.framework.*;
import org.uncommons.watchmaker.framework.interactive.InteractiveSelection;
import org.uncommons.watchmaker.framework.interactive.Renderer;
import org.uncommons.watchmaker.framework.termination.GenerationCount;
Expand All @@ -54,6 +39,7 @@

/**
* Watchmaker Framework implementation of Dawkin's biomorph program.
* <p/>
* @author Daniel Dyer
*/
public class BiomorphApplet extends AbstractExampleApplet
Expand Down Expand Up @@ -98,31 +84,35 @@ protected void prepareGUI(Container container)
* the GUI when it is done.
*/
private SwingBackgroundTask<Biomorph> createTask(final int populationSize,
final int generationCount,
final boolean random)
final int generationCount,
final boolean random)
{
return new SwingBackgroundTask<Biomorph>()
{
@Override
protected Biomorph performTask()
{
EvolutionaryOperator<Biomorph> mutation = random ? new RandomBiomorphMutation(new Probability(0.12d))
: new DawkinsBiomorphMutation();
InteractiveSelection<Biomorph> selection = new InteractiveSelection<Biomorph>(console,
renderer,
populationSize,
1);
EvolutionEngine<Biomorph> engine = new GenerationalEvolutionEngine<Biomorph>(new BiomorphFactory(),
mutation,
selection,
new MersenneTwisterRNG());
EvolutionaryOperator<Biomorph> mutation = random
? new RandomBiomorphMutation(new Probability(0.12d))
: new DawkinsBiomorphMutation();
InteractiveSelection<Biomorph> selection = new InteractiveSelection<Biomorph>(
console,
renderer,
populationSize,
1);
EvolutionEngine<Biomorph> engine =
new GenerationalEvolutionEngine<Biomorph>(new BiomorphFactory(),
mutation,
selection,
new MersenneTwisterRNG());
engine.addEvolutionObserver(new SwingEvolutionObserver(
new GenerationTracker(), 300, TimeUnit.MILLISECONDS));
new GenerationTracker(), 300, TimeUnit.MILLISECONDS));
return engine.evolve(populationSize,
0,
new GenerationCount(generationCount));
0,
new GenerationCount(generationCount));
}


@Override
protected void postProcessing(Biomorph result)
{
Expand All @@ -144,27 +134,25 @@ public static void main(String[] args)
new BiomorphApplet().displayInFrame("Watchmaker Framework - Biomporphs Example");
}


/**
* Simple observer to update the dialog title every time the evolution advances
* to a new generation.
*/
private final class GenerationTracker implements EvolutionObserver<Biomorph>
{
public void populationUpdate(final PopulationData<? extends Biomorph> populationData)
public <S extends Biomorph> void populationUpdate(final PopulationData<S> populationData)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
selectionDialog.setTitle("Biomorph Selection - Generation "
+ (populationData.getGenerationNumber() + 1));
+ (populationData.getGenerationNumber() + 1));
}
});
}
}


/**
* Panel for controlling the evolutionary algorithm parameters.
*/
Expand All @@ -174,6 +162,7 @@ private final class ControlPanel extends JPanel
private JSpinner generationsSpinner;
private JComboBox mutationCombo;


ControlPanel()
{
super(new BorderLayout());
Expand All @@ -198,7 +187,10 @@ private JComponent createInputPanel()
inputPanel.add(generationsLabel);
inputPanel.add(generationsSpinner);
JLabel mutationLabel = new JLabel("Mutation Type: ");
mutationCombo = new JComboBox(new String[]{"Dawkins (Non-random)", "Random"});
mutationCombo = new JComboBox(new String[]
{
"Dawkins (Non-random)", "Random"
});
mutationCombo.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent itemEvent)
Expand Down Expand Up @@ -232,8 +224,8 @@ private JComponent createButtonPanel()
public void actionPerformed(ActionEvent actionEvent)
{
createTask((Integer) populationSpinner.getValue(),
(Integer) generationsSpinner.getValue(),
mutationCombo.getSelectedIndex() == 1).execute();
(Integer) generationsSpinner.getValue(),
mutationCombo.getSelectedIndex() == 1).execute();
selectionDialog.setVisible(true);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@
import org.uncommons.watchmaker.framework.EvolutionaryOperator;

/**
* Non-random mutation of a population of biomorphs. This ensures that each selected candidate
* is mutated differently. This is the mutation used by Dawkins in his original experiment.
* Non-random mutation of a population of biomorphs. This ensures that each selected candidate is
* mutated differently. This is the mutation used by Dawkins in his original experiment.
* <p/>
* @author Daniel Dyer
*/
public class DawkinsBiomorphMutation implements EvolutionaryOperator<Biomorph>
{
/**
* Mutate a population of biomorphs non-randomly, ensuring that each selected
* candidate is mutated differently.
* Mutate a population of biomorphs non-randomly, ensuring that each selected candidate is
* mutated differently.
* <p/>
* @param selectedCandidates {@inheritDoc}
* @param rng A source of randomness (not used since this mutation is non-random).
* @return {@inheritDoc}
Expand All @@ -39,7 +41,7 @@ public List<Biomorph> apply(List<Biomorph> selectedCandidates, Random rng)
List<Biomorph> mutatedPopulation = new ArrayList<Biomorph>(selectedCandidates.size());
int mutatedGene = 0;
int mutation = 1;
for (Biomorph b : selectedCandidates)
for (Biomorph b: selectedCandidates)
{
int[] genes = b.getGenotype();

Expand All @@ -49,8 +51,10 @@ public List<Biomorph> apply(List<Biomorph> selectedCandidates, Random rng)
mutatedGene = (mutatedGene + 1) % Biomorph.GENE_COUNT;
}
genes[mutatedGene] += mutation;
int min = mutatedGene == Biomorph.LENGTH_GENE_INDEX ? Biomorph.LENGTH_GENE_MIN : Biomorph.GENE_MIN;
int max = mutatedGene == Biomorph.LENGTH_GENE_INDEX ? Biomorph.LENGTH_GENE_MAX : Biomorph.GENE_MAX;
int min = mutatedGene == Biomorph.LENGTH_GENE_INDEX ? Biomorph.LENGTH_GENE_MIN
: Biomorph.GENE_MIN;
int max = mutatedGene == Biomorph.LENGTH_GENE_INDEX ? Biomorph.LENGTH_GENE_MAX
: Biomorph.GENE_MAX;
if (genes[mutatedGene] > max)
{
genes[mutatedGene] = min;
Expand Down
Loading