1
0
mirror of https://github.com/fazo96/AIrium.git synced 2025-01-09 09:29:53 +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.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<String,Float> readSettings(String fileContent) {
public static Map<String, Float> readSettings(String fileContent) {
int line = 0;
Map<String, Float> m = new HashMap<String, Float>();
for (String s : fileContent.split("\n")) {

View File

@ -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;
}
}

View File

@ -350,22 +350,18 @@
<Component id="currentMutationFactor" min="-2" max="-2" attributes="0"/>
</Group>
<Group type="102" attributes="0">
<Group type="103" groupAlignment="0" attributes="0">
<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" 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>
<Component id="drawViewCones" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="drawSightLines" min="-2" max="-2" attributes="0"/>
<EmptySpace min="0" pref="0" max="32767" attributes="0"/>
</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>
<EmptySpace max="-2" attributes="0"/>
</Group>

View File

@ -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;
}