Work on first step

pull/1/head
Yong Zhu 7 years ago
parent 218ff93449
commit 4a1c0fd630

@ -1,11 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/test/java" output="target/test-classes" including="**/*.java"/>
<classpathentry kind="src" path="src/test/resources" output="target/test-classes" excluding="**/*.java"/>
<classpathentry kind="src" path="src/main/java" including="**/*.java"/>
<classpathentry kind="src" path="src/main/resources" excluding="**/*.java"/>
<classpathentry kind="output" path="target/classes"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="var" path="M2_REPO/junit/junit/4.11/junit-4.11.jar"/>
<classpathentry kind="var" path="M2_REPO/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar"/>
</classpath>

@ -1,14 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>frog</name>
<comment>An artificial life test project. NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse.</comment>
<projects/>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

@ -1,9 +0,0 @@
#Tue Feb 19 07:51:10 MST 2019
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
eclipse.preferences.version=1
encoding/src/main/java=UTF-8
org.eclipse.jdt.core.compiler.source=1.8
encoding/src/test/resources=UTF-8
encoding/src/main/resources=UTF-8
encoding/src/test/java=UTF-8
org.eclipse.jdt.core.compiler.compliance=1.8

Before

Width:  |  Height:  |  Size: 930 B

After

Width:  |  Height:  |  Size: 930 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.3 KiB

@ -1,32 +1,23 @@
package com.github.drinkjava2.env;
import java.util.Random;
import javax.swing.JFrame;
import com.github.drinkjava2.frog.Frog;
/**
* Application will build env, frogs and let them run
*/
public class Application {
public static void main(String[] args) {
public static void main(String[] args) throws InterruptedException {
JFrame frame = new JFrame();
frame.setSize(Env.XSIZE + 20, Env.YSIZE + 40); // 窗口大小
frame.setLayout(null);
frame.setSize(Env.XSIZE + 220, Env.YSIZE + 250); // 窗口大小
frame.setTitle("Stage#1: First Artifical Life"); // 标题
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 关闭时退出程序
Env env = new Env();
frame.add(env);
frame.setVisible(true);
do {
env.repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
} while (true);
env.run();
}
}

@ -1,11 +1,12 @@
package com.github.drinkjava2.env;
import java.awt.Graphics;
import java.awt.Image;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JPanel;
import javax.swing.JButton;
import com.github.drinkjava2.frog.Frog;
@ -13,23 +14,23 @@ import com.github.drinkjava2.frog.Frog;
* Env is the living space of frog. Use Java Swing to drawing the environment.
*/
@SuppressWarnings("serial")
public class Env extends JPanel {
public class Env extends JButton {
/** Metric width is 500 pixels */
public static final int XSIZE = 500;
public static final int XSIZE = 300;
/** Metric height is 500 pixels */
public static final int YSIZE = 500;
public static final int YSIZE = 300;
public int frogAmount = 10;
public int foodAmount = 10;
public int foodAmount = 500;
public List<Frog> frogs = new ArrayList<Frog>();
public List<Food> foods = new ArrayList<Food>();
public Env() {
super();
this.setLayout(null);// 空布局
this.setBounds(100, 100, XSIZE, YSIZE);
Random rand = new Random();
for (int i = 0; i < frogAmount; i++)
frogs.add(new Frog(rand.nextInt(Env.XSIZE - 3), rand.nextInt(Env.YSIZE - 3)));
@ -37,13 +38,43 @@ public class Env extends JPanel {
foods.add(new Food(rand.nextInt(Env.XSIZE - 3), rand.nextInt(Env.YSIZE - 3)));
}
@Override
public void paint(Graphics g) {
super.paint(g);
for (Frog frog : frogs)
frog.show(g);
for (Food food : foods)
food.show(g);
public void eatFoods() {
eating = true;
for (Frog frog : frogs) {
int j = foods.size() - 1;
for (int i = j; i >= 0; i--) {
Food food = foods.get(i);
if (frog.x == food.x && frog.y == food.y)
foods.remove(i);
}
}
eating = false;
}
static boolean eating = true;
public void run() throws InterruptedException {
Random r = new Random();
int count = 0;
do {
count++;
for (Frog frog : frogs) {
frog.x += r.nextInt(3) - 1;
frog.y += r.nextInt(3) - 1;
}
eatFoods();
if (count % 50 != 0)
continue;
Image buffImg = createImage(this.getWidth(), this.getHeight());
Graphics g = buffImg.getGraphics();
for (Frog frog : frogs)
frog.show(g);
for (Food food : foods)
food.show(g);
Graphics g2 = this.getGraphics();
g2.drawImage(buffImg, 0, 0, this);
Thread.sleep(10);
} while (true);
}
}

@ -19,7 +19,6 @@ import java.util.Random;
import javax.imageio.ImageIO;
import com.github.drinkjava2.frog.brain.Brain;
import com.github.drinkjava2.frog.brain.Eye;
/**
* Frog = brain + body(mouth, eye, leg)
@ -34,25 +33,25 @@ public class Frog {
public int move = 0; // 0: stop 1:up 2:right 3:down 4:left 5:right turn 6 left turn
public long energy = 10000;
static Image frog_up;
static Image frogImg;
static {
try {
frog_up = ImageIO.read(new FileInputStream(new File("").getAbsolutePath() + "/frog_up.png"));
frogImg = ImageIO.read(new FileInputStream(new File("").getAbsolutePath() + "/frog.png"));
} catch (Exception e) {
e.printStackTrace();
}
}
Brain brain;
Eye eye;
public Frog(int x, int y) {
this.x = x;
this.y = y;
}
public void show(Graphics g) {
g.drawImage(frog_up, x - 16, y - 16, 32, 32, null);
public void show(Graphics g) {
g.drawLine(x, y-2, x, y+2);
g.drawImage(frogImg, x - 16, y - 16, 32, 32, null);
}
}

@ -1,69 +0,0 @@
/*
* Copyright 2018 the original author or authors.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
* applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
* OF ANY KIND, either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/
package com.github.drinkjava2.frog;
/**
* Leg of frog, frog only have 1 leg, Leg is a output device of frog , Leg's
* drive cells are located inside of brain
*
* Leg used to move frog, has 4 directions, i.e., have 4 output cells in brain
*
* @author Yong Zhu
* @since 1.0.0
*/
public class Leg {
int up;
int down;
int left;
int right;
int stop;
public int getUp() {
return up;
}
public void setUp(int up) {
this.up = up;
}
public int getDown() {
return down;
}
public void setDown(int down) {
this.down = down;
}
public int getLeft() {
return left;
}
public void setLeft(int left) {
this.left = left;
}
public int getRight() {
return right;
}
public void setRight(int right) {
this.right = right;
}
public int getStop() {
return stop;
}
public void setStop(int stop) {
this.stop = stop;
}
}

@ -1,27 +0,0 @@
/*
* Copyright 2018 the original author or authors.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
* applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
* OF ANY KIND, either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/
package com.github.drinkjava2.frog;
/**
* Mouth of frog, Mouth is a output device of frog , mouth's input cells are
* located inside of brain
*
* @author Yong Zhu
* @since 1.0.0
*/
public class Mouth {
/**
* when eat, cost 1 energy, if eat success, come cells in brain will active
*/
public void eat() {
}
}

@ -10,29 +10,22 @@
*/
package com.github.drinkjava2.frog.brain;
import java.util.List;
/**
* Cell represents a nerve cell, this is the basic unit of brain
* Cell represents a brain nerve cell, this is the basic unit of brain
*
* @author Yong Zhu
* @since 1.0.0
*/
public class Cell {
/** x location in brain */
public int x = 0;
/** y location in brain */
public int y = 0;
/** z location in brain */
public int z = 0;
public int active = 0;
// cell type, 0:normal 1:xxx 2:xxx 3:xxx ...
public int type = 0;
/** Connected other cells */
List<Cell> connects;
public int x;
public int y;
public int active;
public int drop;
public int inX;
public int inY;
public int inSize;
public int outX;
public int outY;
public int outSize;
public int fat;
}

@ -1,24 +0,0 @@
/*
* Copyright 2018 the original author or authors.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
* applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
* OF ANY KIND, either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/
package com.github.drinkjava2.frog.brain;
/**
* Eye of frog, each frog has 1 eye, eye is the input device of frog, eye's feel
* cells are located inside of brain
*
* @author Yong Zhu
* @since 1.0.0
*/
public class Eye {
private int xSize = 30;
private int ySize = 30;
}

@ -1,24 +0,0 @@
/*
* Copyright 2018 the original author or authors.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
* applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
* OF ANY KIND, either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/
package com.github.drinkjava2.frog.brain;
/**
* Gene is not a real thing, it represents the structure of brain, gene can be
* stored to file or database, used to build next generation.
*
* Gene is consisted by Zones.
*
* @author Yong Zhu
* @since 1.0.0
*/
public class Gene {
}

@ -1,22 +0,0 @@
/*
* Copyright 2018 the original author or authors.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
* applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
* OF ANY KIND, either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/
package com.github.drinkjava2.frog.brain;
/**
* Zone is not a real thing, it represents a kind of function zone in brain, may
* be consisted with same or bound of different cells
*
* @author Yong Zhu
* @since 1.0.0
*/
public class Zone {
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.2 KiB

@ -0,0 +1,17 @@
Cell 视觉 extends Cell
int x 但是 x, y 总是出现在固定的区
int y 这个区各个神经元由外界的图形点亮
int active
int last
int inputX
int inputY
int inSize 运动 extends Cell
int outputX 但是 x, y 总是出现在固定的区
int outputY 这个区激活会使frog运动
int outSize
int fat
奖励
不是一个神经元,而是一个规则
最近激活过的神经元fat值增加
Loading…
Cancel
Save