1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
| package com.company.creature;
import com.company.neuronalnetwork.*;
import java.util.ArrayList;
public class C1 implements Comparable<C1>{
private double x,y;
public boolean isDead = false;
private int Strength, Speed;
private double dist;
private InputNeuron distX,distY;
private ArrayList<Neuron> InList = new ArrayList<>();
private ArrayList<Neuron> OutList = new ArrayList<>();
private OutputNeuron moveX = new OutputNeuron(),moveY = new OutputNeuron();
private ArrayList<MiddleNeuron> tabMid;
private ArrayList<Synapse> tabSyn = new ArrayList<Synapse>();
private Wall Wall1 = new Wall(10,10,20,11);
private Wall Wall2 = new Wall(10,10,11,20);
private Wall Wall3 = new Wall(19,10,20,20);
private ArrayList<Wall> Walls = new ArrayList<Wall>();
public C1 (int FoodX, int FoodY,ArrayList<Wall> walls){
Walls = walls;
x= 100; y= 100;InList.add(new InputNeuron(FoodX-x)); InList.add(new InputNeuron(FoodY-y));
for(int i = 0;i!=32;i++)InList.add(new InputNeuron(0));
this.see();
OutList.add(new OutputNeuron()); OutList.add(new OutputNeuron());
for (int z = 0;z!=InList.size();z++) {
for(int y = 0;y!= OutList.size();y++){
double T = Math.random()*100;
if ((int)T < 10)tabSyn.add(new Synapse(Math.random()*0.25-0.125,z,y));
}
}
dist = Math.sqrt((InList.get(0).getValue())*(InList.get(0).getValue())+(InList.get(1).getValue())*(InList.get(1).getValue()));
}
public void move(){
for (int z =0;z!=InList.size();z++) {
for (Synapse s: tabSyn) {
if (s.getInput()==z){
OutList.get(s.getOutput()).setValue(InList.get(z).getValue()*s.getValue());
}
}
}
InList.get(0).setValue(20-x); InList.get(1).setValue(20-y); dist = Math.sqrt((InList.get(0).getValue())*(InList.get(0).getValue())+(InList.get(1).getValue())*(InList.get(1).getValue()));
this.see();
this.x = x+OutList.get(0).getValue();this.y=y+OutList.get(1).getValue();
for (Wall w:Walls) {
if (this.x >= w.getX1() && this.x <= w.getX2() && this.y >= w.getY1() && this.y <= w.getY2()) {
this.isDead = true;
break;
}
}
}
public double getX(){
return x;
}
public double getY(){
return y;
}
public ArrayList<Synapse> getTabSyn(){
return tabSyn;
}
@Override
public int compareTo(C1 c1) {
return (int)(this.dist - c1.dist);
}
private void see(){
for(int z=0;z!=32;z++){
double X = 20*Math.sin(z*2*Math.PI/32);
double Y = 20*Math.cos(z*2*Math.PI/32);
boolean hasHit = false;
if(X>0&&Y>0){
for (Wall w:Walls) {
if (this.x<w.getX2()&&this.y<w.getY2()){
if((this.x+X>w.getX1()&&this.y+Y>w.getY1()&&this.y+Y<w.getY2())||(this.x+X>w.getX1()&&this.x+X<w.getX2()&&this.y+Y>w.getY1())){
InList.get(z+2).setValue(-1); hasHit = true;
}
}
}
}
if(X<0&&Y>0){
for (Wall w:Walls) {
if (this.x>w.getX1()&&this.y<w.getY2()){
if((this.x+X<w.getX2()&&this.y+Y>w.getY1()&&this.y+Y<w.getY2())||(this.x+X>w.getX1()&&this.x+X<w.getX2()&&this.y+Y>w.getY1())){
InList.get(z+2).setValue(-1); hasHit = true;
}
}
}
}
if(X>0&&Y<0){
for (Wall w:Walls) {
if (this.x<w.getX2()&&this.y>w.getY1()){
if((this.x+X>w.getX1()&&this.y+Y>w.getY1()&&this.y+Y<w.getY2())||(this.x+X>w.getX1()&&this.x+X<w.getX2()&&this.y+Y<w.getY2())){
InList.get(z+2).setValue(-1); hasHit = true; }
}
}
}
if(X<0&&Y<0){
for (Wall w:Walls) {
if (this.x>w.getX1()&&this.y>w.getY1()){
if((this.x+X<w.getX2()&&this.y+Y>w.getY1()&&this.y+Y<w.getY2())||(this.x+X>w.getX1()&&this.x+X<w.getX2()&&this.y+Y<w.getY2())){
InList.get(z+2).setValue(-1); hasHit = true; }
}
}
}
if(!hasHit)InList.get(z+2).setValue(1);
}
}
public void mutate(){
ArrayList<Synapse> buffer = tabSyn;
ArrayList<Neuron> IN = InList,OUT = OutList;
int a=0;
for (Neuron in:InList) {
int b=0;
for (Neuron out:OutList) {
for (Synapse s:buffer) {
if(s.getInput()!=a&&s.getOutput()!=b){
if(Math.random()>0.9){
tabSyn.add(new Synapse(Math.random()*0.25-0.125,a,b));
}else if(Math.random()>0.99){
this.tabMid.add(new MiddleNeuron());
OUT.add(tabMid.get(tabMid.size()-1));
IN.add(tabMid.get(tabMid.size()-1));
}
}else{
if (Math.random()>0.95){
s.setValue(s.getValue() + (Math.random()*0.25-0.125));
}
else if (Math.random()>0.99){
tabSyn.remove(s);
}
}
}
}
}
this.InList = IN;
this.OutList = OUT;
this.isDead = false;
}
} |
Partager