1
0
mirror of https://github.com/fazo96/AIrium.git synced 2025-01-10 09:34:20 +01:00

Fixed brain renderer and implemented creature list in GUI

This commit is contained in:
Enrico Fasoli 2015-07-06 17:40:18 +02:00
parent 124087d381
commit f60469bbdf
7 changed files with 334 additions and 233 deletions

View File

@ -96,20 +96,20 @@ public class Game extends ApplicationAdapter {
}
} catch (ConcurrentModificationException ex) {
}
renderer.setColor(0.3f, 0.3f, 0.3f, 1);
// draw borders
renderer.rect(0, 0, world.getWidth(), world.getHeight());
if (world.getSelectedCreature() != null) {
// There is a selection
Creature c = world.getSelectedCreature();
renderer.setColor(1, 1, 1, 1);
// Draw selection rectangle
renderer.rect(c.getX() - c.getSize() / 2, c.getY() - c.getSize() / 2, c.getX() + c.getSize() / 2, c.getY() + c.getSize() / 2);
renderer.rect(c.getX() - c.getSize() / 2, c.getY() - c.getSize() / 2, c.getSize(), c.getSize());
// Draw brain
overlayRenderer.begin();
c.getBrain().render(overlayRenderer);
overlayRenderer.end();
}
renderer.setColor(0.3f, 0.3f, 0.3f, 1);
// draw borders
renderer.rect(0, 0, world.getWidth(), world.getHeight());
renderer.end();
}

View File

@ -0,0 +1,17 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.mygdx.game;
/**
*
* @author Fazo
*/
public interface Listener {
public static int FPS_CHANGED = 0, CREATURE_LIST_CHANGED = 1;
public void on(int event);
}

View File

@ -6,6 +6,7 @@
package logic;
import com.mygdx.game.Game;
import com.mygdx.game.Listener;
import com.mygdx.game.Log;
import java.util.ArrayList;
import java.util.Comparator;
@ -30,7 +31,7 @@ public class World implements Runnable {
private final ArrayList<Creature> graveyard;
private final ArrayList<Vegetable> plants;
private final ArrayList<Vegetable> deadPlants;
private final ArrayList<FpsListener> fpsListeners;
private final ArrayList<Listener> listeners;
public World(int width, int height) {
this.width = width;
@ -43,7 +44,7 @@ public class World implements Runnable {
plants = new ArrayList();
deadPlants = new ArrayList();
graveyard = new ArrayList();
fpsListeners = new ArrayList();
listeners = new ArrayList();
selected = null;
newGen(true);
}
@ -62,9 +63,7 @@ public class World implements Runnable {
if (now.getTime() - timekeeper.getTime() > 1000) {
fps = frames;
frames = 0;
for (FpsListener f : fpsListeners) {
f.fpsChanged(fps);
}
fire(Listener.FPS_CHANGED);
timekeeper = new Date();
}
if (fpsLimit > 0) {
@ -106,7 +105,9 @@ public class World implements Runnable {
elements.removeAll(deadPlants);
plants.removeAll(deadPlants);
deadPlants.clear();
creatures.removeAll(graveyard);
if (creatures.removeAll(graveyard)) {
fire(Listener.CREATURE_LIST_CHANGED);
}
if (creatures.isEmpty()) {
// All dead, next gen
newGen(false);
@ -176,6 +177,7 @@ public class World implements Runnable {
ne.getBrain().mutate(0.05f); // mutate children
}
graveyard.clear();
fire(Listener.CREATURE_LIST_CHANGED);
generation++;
}
}
@ -218,11 +220,6 @@ public class World implements Runnable {
}
}
public interface FpsListener {
public abstract void fpsChanged(int newValue);
}
public void selectCreatureAt(int x, int y) {
selected = null; // Clear selection
try {
@ -236,6 +233,12 @@ public class World implements Runnable {
}
}
public void fire(int eventCode) {
for (Listener f : listeners) {
f.on(eventCode);
}
}
private void spawnVegetable() {
spawn(false, null);
}
@ -260,8 +263,8 @@ public class World implements Runnable {
return generation;
}
public void addFpsListener(FpsListener f) {
fpsListeners.add(f);
public void addListener(Listener f) {
listeners.add(f);
}
public void add(Element e) {

View File

@ -88,6 +88,7 @@ public class Brain {
* @param s the ShapeRenderer to use for the drawing
*/
public void render(ShapeRenderer s) {
int sepX = 100, sepY = 50, offset = 100;
s.set(ShapeRenderer.ShapeType.Filled);
int neuronHeight = 0;
for (Neuron[] ns : neurons) {
@ -95,22 +96,20 @@ public class Brain {
neuronHeight = ns.length;
}
}
s.rect(0, 0, neurons.length * 50, neuronHeight * 30);
for (int i = 0; i < neurons.length; i++) {
//s.set(ShapeRenderer.ShapeType.Line);
for (int j = 0; j < neurons[i].length; j++) {
// get neuron result first so cache system can kick in and save some calculations
float nr = neurons[i][j].compute();
// Draw neuron links
float[] links = neurons[i][j].getInputs();
float[] links = neurons[i][j].getWeights();
for (int f = 0; f < links.length; f++) {
s.setColor(links[f], links[f], links[f], 1);
s.line(i * 50, j * 30, (i - 1) * 50, f * 30);
s.setColor(links[f] < 0 ? links[f]/2 * -1 : 0, links[f] > 0 ? links[f]/2 : 0, 0, 1);
s.line(i * sepX + offset, j * sepY + offset, (i - 1) * sepX + offset, f * sepY + offset);
}
// Draw neuron
s.setColor(1 - nr, nr, 0, 1);
s.set(ShapeRenderer.ShapeType.Filled);
s.circle(i * 50, j * 30, 15);
s.circle(i * sepX + offset, j * sepY + offset, 15);
}
}
}
@ -130,7 +129,6 @@ public class Brain {
for (int i = 0; i < values.length; i++) {
neurons[0][i].setOutput(values[i]);
}
clearCache();
}
/**
@ -140,7 +138,7 @@ public class Brain {
* result
*/
public float[] compute() {
//clearCache(); // unnecessary if already called when changing inputs
clearCache(); // unnecessary if already called when changing inputs
float[] res = new float[neurons[neurons.length - 1].length];
for (int i = 0; i < neurons[neurons.length - 1].length; i++) {
res[i] = neurons[neurons.length - 1][i].compute();

View File

@ -55,7 +55,9 @@ public class Neuron {
if (isInputNeuron) {
return output;
}
if(cache.hasCachedOutput()) return cache.getCachedOutput();
if (cache.hasCachedOutput()) {
return cache.getCachedOutput();
}
float a = bias * -1; // activation
for (int i = 0; i < weights.length; i++) {
if (cache.has(i)) {
@ -83,6 +85,11 @@ public class Neuron {
return mutatedWeights;
}
/**
* Broken, doesn't work for some reason.
*
* @return
*/
public float[] getInputs() {
float inputs[] = new float[weights.length];
for (int i = 0; i < inputs.length; i++) {

View File

@ -169,6 +169,38 @@
</Component>
</SubComponents>
</Container>
<Container class="javax.swing.JScrollPane" name="jScrollPane2">
<AuxValues>
<AuxValue name="autoScrollPane" type="java.lang.Boolean" value="true"/>
</AuxValues>
<Constraints>
<Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.support.JTabbedPaneSupportLayout" value="org.netbeans.modules.form.compat2.layouts.support.JTabbedPaneSupportLayout$JTabbedPaneConstraintsDescription">
<JTabbedPaneConstraints tabName="Creatures">
<Property name="tabTitle" type="java.lang.String" value="Creatures"/>
</JTabbedPaneConstraints>
</Constraint>
</Constraints>
<Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/>
<SubComponents>
<Component class="javax.swing.JList" name="creatureList">
<Properties>
<Property name="model" type="javax.swing.ListModel" editor="org.netbeans.modules.form.editors2.ListModelEditor">
<StringArray count="5">
<StringItem index="0" value="Item 1"/>
<StringItem index="1" value="Item 2"/>
<StringItem index="2" value="Item 3"/>
<StringItem index="3" value="Item 4"/>
<StringItem index="4" value="Item 5"/>
</StringArray>
</Property>
</Properties>
<Events>
<EventHandler event="valueChanged" listener="javax.swing.event.ListSelectionListener" parameters="javax.swing.event.ListSelectionEvent" handler="creatureListValueChanged"/>
</Events>
</Component>
</SubComponents>
</Container>
</SubComponents>
</Container>
<Component class="javax.swing.JLabel" name="status">

View File

@ -8,15 +8,17 @@ package gui;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.mygdx.game.Game;
import com.mygdx.game.Listener;
import com.mygdx.game.Log;
import com.mygdx.game.Log.LogListener;
import javax.swing.JOptionPane;
import logic.World;
/**
*
* @author fazo
*/
public class GUI extends javax.swing.JFrame implements LogListener,World.FpsListener {
public class GUI extends javax.swing.JFrame implements LogListener, Listener {
private Game game;
private LwjglApplication app;
@ -47,6 +49,8 @@ public class GUI extends javax.swing.JFrame implements LogListener,World.FpsList
logTextArea = new javax.swing.JTextArea();
jLabel1 = new javax.swing.JLabel();
logLevelBox = new javax.swing.JComboBox();
jScrollPane2 = new javax.swing.JScrollPane();
creatureList = new javax.swing.JList();
status = new javax.swing.JLabel();
menuBar = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
@ -99,6 +103,20 @@ public class GUI extends javax.swing.JFrame implements LogListener,World.FpsList
tabs.addTab("Log", logPane);
creatureList.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
creatureList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
creatureListValueChanged(evt);
}
});
jScrollPane2.setViewportView(creatureList);
tabs.addTab("Creatures", jScrollPane2);
status.setText("Simulation stopped");
javax.swing.GroupLayout containerLayout = new javax.swing.GroupLayout(container);
@ -168,7 +186,8 @@ public class GUI extends javax.swing.JFrame implements LogListener,World.FpsList
config.width = 800;
config.resizable = false;
app = new LwjglApplication(game = new Game(), config);
game.getWorld().addFpsListener(this);
game.getWorld().addListener(this);
setCreatureList();
}//GEN-LAST:event_startButtonActionPerformed
@Override
public void onLog(int level, String msg) {
@ -176,6 +195,23 @@ public class GUI extends javax.swing.JFrame implements LogListener,World.FpsList
setScrollBarToTheBottom();
}
@Override
public void on(int event) {
if (event == Listener.FPS_CHANGED) {
status.setText("Generation: " + game.getWorld().getGeneration() + " FPS: " + game.getWorld().getFps());
} else if (event == Listener.CREATURE_LIST_CHANGED) {
setCreatureList();
}
}
private void setCreatureList() {
String list[] = new String[game.getWorld().getCreatures().size()];
for (int i = 0; i < list.length; i++) {
list[i] = "Fitness: " + game.getWorld().getCreatures().get(i).getFitness();
}
creatureList.setListData(list);
}
public void setScrollBarToTheBottom() {
jScrollPane1.getVerticalScrollBar().setValue(jScrollPane1.getVerticalScrollBar().getMaximum());
}
@ -184,12 +220,24 @@ public class GUI extends javax.swing.JFrame implements LogListener,World.FpsList
Log.setLogLevel(logLevelBox.getSelectedIndex());
}//GEN-LAST:event_logLevelBoxItemStateChanged
private void creatureListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_creatureListValueChanged
try {
if (creatureList.getSelectedIndex() >= 0) {
Game.get().getWorld().selectCreature(Game.get().getWorld().getCreatures().get(creatureList.getSelectedIndex()));
}
} catch (IndexOutOfBoundsException ex) {
JOptionPane.showMessageDialog(this, "This creature is not available");
}
}//GEN-LAST:event_creatureListValueChanged
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel container;
private javax.swing.JList creatureList;
private javax.swing.JMenuItem exitButton;
private javax.swing.JLabel jLabel1;
private javax.swing.JMenu jMenu1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JComboBox logLevelBox;
private javax.swing.JPanel logPane;
private javax.swing.JTextArea logTextArea;
@ -199,8 +247,4 @@ public class GUI extends javax.swing.JFrame implements LogListener,World.FpsList
private javax.swing.JTabbedPane tabs;
// End of variables declaration//GEN-END:variables
@Override
public void fpsChanged(int fps) {
status.setText("Generation: "+game.getWorld().getGeneration()+" FPS: "+fps);
}
}