diff --git a/core/src/com/mygdx/game/Serializer.java b/core/src/com/mygdx/game/Serializer.java index aad0aa7..15a3335 100644 --- a/core/src/com/mygdx/game/Serializer.java +++ b/core/src/com/mygdx/game/Serializer.java @@ -7,6 +7,7 @@ import java.io.IOException; import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; +import java.util.Random; /** * Hangles File I/O for AIrium components. @@ -15,6 +16,27 @@ import java.util.Map; */ 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) { PrintWriter fw = null; try { @@ -53,7 +75,7 @@ public class Serializer { return a; } - public static Map readSettings(String fileContent) { + public static Map readSettings(String fileContent) { int line = 0; Map m = new HashMap(); for (String s : fileContent.split("\n")) { diff --git a/core/src/logic/neural/Brain.java b/core/src/logic/neural/Brain.java index 0df0a4d..361d370 100644 --- a/core/src/logic/neural/Brain.java +++ b/core/src/logic/neural/Brain.java @@ -2,6 +2,7 @@ package logic.neural; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.mygdx.game.Log; +import com.mygdx.game.Serializer; /** * Represents a virtual brain @@ -12,6 +13,7 @@ public class Brain { public static final float bias = 0.5f; private Neuron[][] neurons; + private String name; /** * 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)); } } + 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. * @@ -273,4 +281,9 @@ public class Brain { public Neuron[][] getNeurons() { return neurons; } + + public String getName() { + return name; + } + } diff --git a/desktop/src/gui/GUI.form b/desktop/src/gui/GUI.form index 31c6757..c03186a 100644 --- a/desktop/src/gui/GUI.form +++ b/desktop/src/gui/GUI.form @@ -350,22 +350,18 @@ - - - - - - - - - - - - - - + + + + + + + + + + diff --git a/desktop/src/gui/GUI.java b/desktop/src/gui/GUI.java index 076f7a8..a4d7642 100644 --- a/desktop/src/gui/GUI.java +++ b/desktop/src/gui/GUI.java @@ -23,6 +23,7 @@ import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JFileChooser; import javax.swing.JOptionPane; +import javax.swing.filechooser.FileFilter; import logic.Creature; /** @@ -517,18 +518,16 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener { .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(currentMutationFactor)) .addGroup(settingsPanelLayout.createSequentialGroup() - .addGroup(settingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(settingsPanelLayout.createSequentialGroup() - .addComponent(jLabel12) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(nMutatedConnectionsSlider, javax.swing.GroupLayout.PREFERRED_SIZE, 490, javax.swing.GroupLayout.PREFERRED_SIZE) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(currentNMutatedConnections)) - .addGroup(settingsPanelLayout.createSequentialGroup() - .addComponent(drawViewCones) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(drawSightLines))) - .addGap(0, 0, Short.MAX_VALUE))) + .addComponent(drawViewCones) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(drawSightLines) + .addGap(0, 0, Short.MAX_VALUE)) + .addGroup(settingsPanelLayout.createSequentialGroup() + .addComponent(jLabel12) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(nMutatedConnectionsSlider, javax.swing.GroupLayout.PREFERRED_SIZE, 490, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(currentNMutatedConnections, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) .addContainerGap()) ); settingsPanelLayout.setVerticalGroup( @@ -844,7 +843,9 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener { if (i >= game.getWorld().getCreatures().size()) { 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()) { selected = i; } @@ -865,6 +866,10 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener { sightRangeSlider.setValue(100); hpDecaySlider.setValue(500); maxTicksSlider.setValue(0); + nMutatedBrainsSlider.setValue(50); + nMutatedNeuronsSlider.setValue(20); + nMutatedConnectionsSlider.setValue(50); + mutationFactorSlider.setValue(100); toggleFPSLimitCheckbox.setSelected(false); multithreadingCheckbox.setSelected(true); 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 updateSettings(); }//GEN-LAST:event_nMutatedConnectionsSliderStateChanged - private File saveDialog() { + return saveDialog(null); + } + + private File saveDialog(String defaultName) { JFileChooser fc = new JFileChooser(); File f = null; + if (defaultName != null) { + fc.setSelectedFile(new File(defaultName)); + } if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) { 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"); return; } - File f = saveDialog(); + File f = saveDialog(game.getWorld().getSelectedCreature().getBrain().getName()+".brain"); if (f == null) { return; } @@ -1099,7 +1110,7 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener { game.setPaused(true); } updateGUI(); - File f = saveDialog(); + File f = saveDialog("AIrium_settings.ini"); if (f == null) { return; }