1
0
mirror of https://github.com/fazo96/AIrium.git synced 2025-01-25 11:54:20 +01:00

added unique creature names. Fix #12

This commit is contained in:
Enrico Fasoli 2015-08-06 19:50:51 +02:00
parent 9cbbc7c9b2
commit e18cddebc7
4 changed files with 73 additions and 31 deletions

View File

@ -7,6 +7,7 @@ import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Random;
/** /**
* Hangles File I/O for AIrium components. * Hangles File I/O for AIrium components.
@ -15,6 +16,27 @@ import java.util.Map;
*/ */
public class Serializer { public class Serializer {
private static final String[] sillabe = {"ba", "de", "ka", "mo", "shi", "du", "ro", "te", "mi", "lo", "pa"};
public static String nameBrain(float[][][] brainMap) {
// Compute a unique representation of the brainmap
long a = 0;
for (int i = 0; i < brainMap.length; i++) {
for (int j = 0; j < brainMap[i].length; j++) {
for (int z = 0; z < brainMap[i][j].length; z++) {
a += brainMap[i][j][z] * i * j * z;
}
}
}
Random gen = new Random(a);
String name = "";
int length = Math.abs(gen.nextInt()) % 5 + 2;
for (int i = 0; i < length; i++){
name += sillabe[Math.abs(gen.nextInt()) % sillabe.length];
}
return name;
}
public static void saveToFile(File f, String content) { public static void saveToFile(File f, String content) {
PrintWriter fw = null; PrintWriter fw = null;
try { try {
@ -53,7 +75,7 @@ public class Serializer {
return a; return a;
} }
public static Map<String,Float> readSettings(String fileContent) { public static Map<String, Float> readSettings(String fileContent) {
int line = 0; int line = 0;
Map<String, Float> m = new HashMap<String, Float>(); Map<String, Float> m = new HashMap<String, Float>();
for (String s : fileContent.split("\n")) { for (String s : fileContent.split("\n")) {

View File

@ -2,6 +2,7 @@ package logic.neural;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.mygdx.game.Log; import com.mygdx.game.Log;
import com.mygdx.game.Serializer;
/** /**
* Represents a virtual brain * Represents a virtual brain
@ -12,6 +13,7 @@ public class Brain {
public static final float bias = 0.5f; public static final float bias = 0.5f;
private Neuron[][] neurons; private Neuron[][] neurons;
private String name;
/** /**
* Create a new brain with a random map (mind) with given number of neurons * Create a new brain with a random map (mind) with given number of neurons
@ -50,6 +52,7 @@ public class Brain {
} }
} }
} }
recomputeName();
} }
/** /**
@ -65,6 +68,7 @@ public class Brain {
Log.log(Log.DEBUG, "Adding Layer " + (i + 1) + " Neuron " + (j + 1)); Log.log(Log.DEBUG, "Adding Layer " + (i + 1) + " Neuron " + (j + 1));
} }
} }
recomputeName();
} }
/** /**
@ -265,6 +269,10 @@ public class Brain {
} }
} }
private void recomputeName(){
name = Serializer.nameBrain(getMap());
}
/** /**
* Returns an array with pointers to all this brain's neurons. * Returns an array with pointers to all this brain's neurons.
* *
@ -273,4 +281,9 @@ public class Brain {
public Neuron[][] getNeurons() { public Neuron[][] getNeurons() {
return neurons; return neurons;
} }
public String getName() {
return name;
}
} }

View File

@ -350,22 +350,18 @@
<Component id="currentMutationFactor" min="-2" max="-2" attributes="0"/> <Component id="currentMutationFactor" min="-2" max="-2" attributes="0"/>
</Group> </Group>
<Group type="102" attributes="0"> <Group type="102" attributes="0">
<Group type="103" groupAlignment="0" attributes="0"> <Component id="drawViewCones" min="-2" max="-2" attributes="0"/>
<Group type="102" alignment="0" attributes="0"> <EmptySpace max="-2" attributes="0"/>
<Component id="jLabel12" min="-2" max="-2" attributes="0"/> <Component id="drawSightLines" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="nMutatedConnectionsSlider" min="-2" pref="490" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="currentNMutatedConnections" min="-2" max="-2" attributes="0"/>
</Group>
<Group type="102" alignment="0" attributes="0">
<Component id="drawViewCones" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="drawSightLines" min="-2" max="-2" attributes="0"/>
</Group>
</Group>
<EmptySpace min="0" pref="0" max="32767" attributes="0"/> <EmptySpace min="0" pref="0" max="32767" attributes="0"/>
</Group> </Group>
<Group type="102" alignment="0" attributes="0">
<Component id="jLabel12" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="nMutatedConnectionsSlider" min="-2" pref="490" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="currentNMutatedConnections" max="32767" attributes="0"/>
</Group>
</Group> </Group>
<EmptySpace max="-2" attributes="0"/> <EmptySpace max="-2" attributes="0"/>
</Group> </Group>

View File

@ -23,6 +23,7 @@ import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.swing.JFileChooser; import javax.swing.JFileChooser;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import logic.Creature; import logic.Creature;
/** /**
@ -517,18 +518,16 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener {
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(currentMutationFactor)) .addComponent(currentMutationFactor))
.addGroup(settingsPanelLayout.createSequentialGroup() .addGroup(settingsPanelLayout.createSequentialGroup()
.addGroup(settingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(drawViewCones)
.addGroup(settingsPanelLayout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel12) .addComponent(drawSightLines)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGap(0, 0, Short.MAX_VALUE))
.addComponent(nMutatedConnectionsSlider, javax.swing.GroupLayout.PREFERRED_SIZE, 490, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(settingsPanelLayout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel12)
.addComponent(currentNMutatedConnections)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(settingsPanelLayout.createSequentialGroup() .addComponent(nMutatedConnectionsSlider, javax.swing.GroupLayout.PREFERRED_SIZE, 490, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(drawViewCones) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(currentNMutatedConnections, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addComponent(drawSightLines)))
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap()) .addContainerGap())
); );
settingsPanelLayout.setVerticalGroup( settingsPanelLayout.setVerticalGroup(
@ -844,7 +843,9 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener {
if (i >= game.getWorld().getCreatures().size()) { if (i >= game.getWorld().getCreatures().size()) {
return; return;
} }
list[i] = "Fitness: " + game.getWorld().getCreatures().get(i).getFitness(); list[i] = game.getWorld().getCreatures().get(i).getBrain().getName()
+ " - Fitness: "
+ game.getWorld().getCreatures().get(i).getFitness();
if (game.getWorld().getCreatures().get(i) == game.getWorld().getSelectedCreature()) { if (game.getWorld().getCreatures().get(i) == game.getWorld().getSelectedCreature()) {
selected = i; selected = i;
} }
@ -865,6 +866,10 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener {
sightRangeSlider.setValue(100); sightRangeSlider.setValue(100);
hpDecaySlider.setValue(500); hpDecaySlider.setValue(500);
maxTicksSlider.setValue(0); maxTicksSlider.setValue(0);
nMutatedBrainsSlider.setValue(50);
nMutatedNeuronsSlider.setValue(20);
nMutatedConnectionsSlider.setValue(50);
mutationFactorSlider.setValue(100);
toggleFPSLimitCheckbox.setSelected(false); toggleFPSLimitCheckbox.setSelected(false);
multithreadingCheckbox.setSelected(true); multithreadingCheckbox.setSelected(true);
enableCorpsesCheckbox.setSelected(false); enableCorpsesCheckbox.setSelected(false);
@ -1029,10 +1034,16 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener {
private void nMutatedConnectionsSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_nMutatedConnectionsSliderStateChanged private void nMutatedConnectionsSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_nMutatedConnectionsSliderStateChanged
updateSettings(); updateSettings();
}//GEN-LAST:event_nMutatedConnectionsSliderStateChanged }//GEN-LAST:event_nMutatedConnectionsSliderStateChanged
private File saveDialog() { private File saveDialog() {
return saveDialog(null);
}
private File saveDialog(String defaultName) {
JFileChooser fc = new JFileChooser(); JFileChooser fc = new JFileChooser();
File f = null; File f = null;
if (defaultName != null) {
fc.setSelectedFile(new File(defaultName));
}
if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) { if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
f = fc.getSelectedFile(); f = fc.getSelectedFile();
} }
@ -1071,7 +1082,7 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener {
JOptionPane.showMessageDialog(this, "Please select a creature first"); JOptionPane.showMessageDialog(this, "Please select a creature first");
return; return;
} }
File f = saveDialog(); File f = saveDialog(game.getWorld().getSelectedCreature().getBrain().getName()+".brain");
if (f == null) { if (f == null) {
return; return;
} }
@ -1099,7 +1110,7 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener {
game.setPaused(true); game.setPaused(true);
} }
updateGUI(); updateGUI();
File f = saveDialog(); File f = saveDialog("AIrium_settings.ini");
if (f == null) { if (f == null) {
return; return;
} }