diff --git a/README.md b/README.md index c408551..4066f40 100644 --- a/README.md +++ b/README.md @@ -77,11 +77,12 @@ Frog: 这是人工生命的主体,目前起名叫青蛙(Frog),其实叫什 2019-08-05 有了分组测试功能后,顺手加上了一个青蛙走跷跷板自动平衡的演示,它每次只出场一个青蛙, 每轮包括100场测试,大约跑90多轮半个小时(电脑慢)后,出现了下面的画面: ![result5](https://gitee.com/drinkjava2/frog/raw/master/result5_seesaw.gif) 这个版本的目的是为了增加一点趣味性,显得青蛙还是有点"用处"的,省得让人以为这个项目不务正业,青蛙只会找食。这个版本青蛙的脑结构和找食版的青蛙基本相同,区别只是在于环境不同,也就是说它的表现随着环境而变化,这符合"通用人工智能"的概念,即信号感受器官是统一的(通常是眼睛),但能根据不同的环境完成不同的任务。走跷跷板演示是最后一个2维脑的版本,今后这个项目将沉寂一段较长时间,我将致力于将青蛙脑重构为3D金字塔形脑结构(见上文),因为这个项目的缺点已经很明显,它不具备对2维图像的模式识别能力,用随机试错的方式只能处理非常简单的、信号在视网膜的固定区域出现的图像信号。 -青蛙的找食效率以及走跷跷板平衡的能力都没有优化到顶点,一些构想中的复杂的器官如“与门”、“或门”(不要怀疑大自然能否进化出这些复杂器官)等都没加上,器官的用进废退、奖励信号的利用都没反映,但我认为这些还不关键,目前最急迫的任务应该是先进行3D脑结构建模,让青蛙能具备2维图形的模式识别(和回忆)功能,这个大的架构重构是它能处理复杂图像信息的立足之本,它的图像识别能力和通常的用上千张图片来训练识别一个图片这种工作模式完全不同,它是一种通用的,可自动分类识别所有图像的模式,更符合动物脑的工作模式,记住并回忆出某个图像(或任意输入信号场景的组合),可能只需要这种场景重复出现过几次即可,它是一种无外界信号判定,自动分类的识别模式。 - +青蛙的找食效率以及走跷跷板平衡的能力都没有优化到顶点,一些构想中的复杂的器官如“与门”、“或门”(不要怀疑大自然能否进化出这些复杂器官)等都没加上,器官的用进废退、奖励信号的利用都没反映,但我认为这些还不关键,目前最急迫的任务应该是先进行3D脑结构建模,让青蛙能具备2维图形的模式识别(和回忆)功能,这个大的架构重构是它能处理复杂图像信息的立足之本,它的图像识别能力和通常的用上千张图片来训练识别一个图片这种工作模式完全不同,它是一种通用的,可自动分类识别所有图像的模式,更符合动物脑的工作模式,记住并回忆出某个图像(或任意输入信号场景的组合),可能只需要这种场景重复出现过几次即可,它是一种无外界信号判定,自动分类的识别模式。 如果想要运行这个项目的以前版本,可以结合gitk命令和参考"版本提交记录.md"的介绍,用git reset命令回复到以前任一个版本,例如用: git reset --hard ae34b07e 可以转回到上一个分组测试的找食版本。 -2019-09-09 开始3D脑的构建,任务又回到原点:找食,从静止的青蛙要能进化到吃光所有食物。目前只是搭建空的开发环境,还未涉及3D脑模型编程。新的工作存放在core3d目录,原有的旧core目录保留,相应地批处理文件也分为普通版run.bat和3d版run3d.bat,蛋文件也分为普通版eggs.ser和3d版eggs3d.ser。 +2019-09-09 开始3D脑的构建,任务又回到原点:找食,从静止的青蛙要能进化到吃光所有食物。目前只是搭建空的3D框架,还未涉及3D脑模型编程。新的工作存放在core3d目录,原有的旧core目录保留,相应地批处理文件也分为普通版run.bat和3d版run3d.bat,蛋文件也分为普通版eggs.ser和3d版eggs3d.ser。 +脑的3D版要引入模式识别功能,第一个编程任务是要用这个3D脑模拟出体全息存贮现象,也就等价于模式识别功能,用字母的点阵图像激活它的视觉区,并同时激活一个随意指定的脑区作为字母识别区,然后只激活视觉区的图像,再检查这个随意指定的字母区是否会被激活,而其它字母区基本不激活,例如同时训练字母A、B、C、D一段时间,然后只激活字母A的图像区(即在视网膜区产生光子),其它三个BCD字母区应该没有A字母区活跃(即其它字母区收到的光子数或总能量少于A字母区),这是从从二维图像识别出对应的文字,比较好检测并利用检测结果来进行生存竟争,从大量随机生成的脑细胞播种器官中挑选出有用的器官,注意在同一个脑空间(cube),可以同时存在多个不同器官播种出来的脑细胞(cell)。体全息存贮在物理上很难实现,因为光和材料受物理特性制约,但在虚拟脑中不存在这个物理限制,让虚拟的光子反射、拐弯、增强、拆分都可轻易模拟出。 +反之,如果对应A字母的脑区兴奋,也要能激活一个模糊的A的像素点阵图像脑内某个区(与视网膜重合或下层),这不好检测,但可以变通一下,比如同时训练C和O两个字母,然后只激活C字母区,并检测O字母区是否有较强的信号,也就是说C字母区会在脑内成像区生成一个C的像素点阵,然后这个像素点阵又会倒过来激活与C图像相似的O字母区(以及所有类似C图像的其它脑区),这是第二步的编程任务,它反映了脑的联想机制。如果说第一步是信息的存贮,是体全息存贮原理的简单实现,这第二步就是信息的检索和联想,相比与普通的体全息存贮,它多了一个联想功能,这个联想功能以后会用到,由痛苦、愉快等奖惩机制来调节这个联想过程一直永无止歇地进行下去。 ## 重要参数 | Parameters 在Env.java类中以下有以下可调整参数,请手工修改这些参数进行不同的测试,前5个参数很重要: diff --git a/core3d/src/main/java/com/github/drinkjava2/frog/Application.java b/core3d/src/main/java/com/github/drinkjava2/frog/Application.java index 76d44f0..e27fb30 100644 --- a/core3d/src/main/java/com/github/drinkjava2/frog/Application.java +++ b/core3d/src/main/java/com/github/drinkjava2/frog/Application.java @@ -27,7 +27,7 @@ public class Application { } public static JFrame mainFrame = new JFrame(); public static Env env = new Env(); - public static BrainPicture brainPic = new BrainPicture(Env.ENV_WIDTH + 5, 0, Env.FROG_BRAIN_RADIUS, + public static BrainPicture brainPic = new BrainPicture(Env.ENV_WIDTH + 5, 0, Env.FROG_BRAIN_XSIZE, Env.FROG_BRAIN_DISP_WIDTH); public static void main(String[] args) throws InterruptedException { diff --git a/core3d/src/main/java/com/github/drinkjava2/frog/Env.java b/core3d/src/main/java/com/github/drinkjava2/frog/Env.java index 8b36184..3c08ec9 100644 --- a/core3d/src/main/java/com/github/drinkjava2/frog/Env.java +++ b/core3d/src/main/java/com/github/drinkjava2/frog/Env.java @@ -11,10 +11,8 @@ import javax.swing.JPanel; import com.github.drinkjava2.frog.egg.Egg; import com.github.drinkjava2.frog.egg.EggTool; -import com.github.drinkjava2.frog.objects.Food; import com.github.drinkjava2.frog.objects.Material; import com.github.drinkjava2.frog.objects.Object; -import com.github.drinkjava2.frog.objects.Trap; import com.github.drinkjava2.frog.util.RandomUtils; /** @@ -26,7 +24,7 @@ import com.github.drinkjava2.frog.util.RandomUtils; @SuppressWarnings("all") public class Env extends JPanel { /** Speed of test */ - public static final int SHOW_SPEED = 3; // 测试速度,-1000~1000,可调, 数值越小,速度越慢 + public static final int SHOW_SPEED = 10; // 测试速度,-1000~1000,可调, 数值越小,速度越慢 /** Delete eggs at beginning of each run */ public static final boolean DELETE_EGGS = true;// 每次运行是否先删除保存的蛋 @@ -54,8 +52,10 @@ public class Env extends JPanel { /** Steps of one test round */ public static final int STEPS_PER_ROUND = 50000;// 每轮测试步数,可调 - /** Frog's brain radius */ - public static final float FROG_BRAIN_RADIUS = 20; // frog的脑半径先固定为20个单元 + /** Frog's x radius, brain volume = XSIZE * YSIZE * ZSIZE */ + public static final int FROG_BRAIN_XSIZE = 50; // frog的脑在X方向长度 + public static final int FROG_BRAIN_YSIZE = 25; // frog的脑在Y方向长度 + public static final int FROG_BRAIN_ZSIZE = 15; // frog的脑在Z方向长度 public static final int FOOD_QTY = 100; // 食物数量, 可调 @@ -67,7 +67,7 @@ public class Env extends JPanel { public static List eggs = new ArrayList<>(); // 这里存放从磁盘载入或上轮下的蛋,每个蛋可能生成1~n个青蛙, - public static Object[] things = new Object[] { }; + public static Object[] things = new Object[] {}; static { System.out.println("唵缚悉波罗摩尼莎诃!"); // 杀生前先打印往生咒,见码云issue#IW4H8 @@ -234,7 +234,7 @@ public class Env extends JPanel { } for (int j = 0; j < FROG_PER_SCREEN; j++) { Frog f = frogs.get(screen * FROG_PER_SCREEN + j); - f.cells.clear(); + f.cubes = new Object[1][1][1]; } Application.brainPic.drawBrainPicture(firstFrog); Application.mainFrame.setTitle(new StringBuilder("Round: ").append(round).append(", screen:") diff --git a/core3d/src/main/java/com/github/drinkjava2/frog/Frog.java b/core3d/src/main/java/com/github/drinkjava2/frog/Frog.java index 0172422..0afa4c6 100644 --- a/core3d/src/main/java/com/github/drinkjava2/frog/Frog.java +++ b/core3d/src/main/java/com/github/drinkjava2/frog/Frog.java @@ -18,7 +18,6 @@ import java.util.List; import javax.imageio.ImageIO; -import com.github.drinkjava2.frog.brain.Cell; import com.github.drinkjava2.frog.brain.Organ; import com.github.drinkjava2.frog.egg.Egg; import com.github.drinkjava2.frog.objects.Material; @@ -34,7 +33,7 @@ import com.github.drinkjava2.frog.objects.Material; public class Frog { /** brain cells */ - public List cells = new ArrayList<>(); + public Object[][][] cubes; /** organs */ public List organs = new ArrayList<>(); @@ -63,7 +62,7 @@ public class Frog { public void initOrgans() { for (Organ org : organs) - org.initFrog(this);// 每个新器官初始化,如果是Group类,它们会生成许多脑细胞 + org.init(this);// 每个新器官初始化,如果是Group类,它们会生成许多脑细胞 } public boolean active(Env v) { diff --git a/core3d/src/main/java/com/github/drinkjava2/frog/brain/BrainPicture.java b/core3d/src/main/java/com/github/drinkjava2/frog/brain/BrainPicture.java index 0c20e0a..a88a4b9 100644 --- a/core3d/src/main/java/com/github/drinkjava2/frog/brain/BrainPicture.java +++ b/core3d/src/main/java/com/github/drinkjava2/frog/brain/BrainPicture.java @@ -23,6 +23,8 @@ import com.github.drinkjava2.frog.Frog; /** * BrainPicture show first frog's brain structure, for debug purpose only * + * 这个类用来画出脑图,这不是一个关键类,对脑的运行逻辑无影响,但有了脑图后可以直观地看出脑的3维结构,进行有针对性的改进 + * * @author Yong Zhu * @since 1.0 */ @@ -33,18 +35,15 @@ public class BrainPicture extends JPanel { float scale; // brain scale int xOffset = 0; // brain display x offset compare to screen int yOffset = 0; // brain display y offset compare to screen - // float xAngle = (float) (Math.PI / 4); // brain rotate on x axis - // float yAngle = (float) (Math.PI / 4); // brain rotate on y axis - // float zAngle = (float) (Math.PI / 4);// brain rotate on z axis - float xAngle = 0; // brain rotate on x axis - float yAngle = 0; // brain rotate on y axis + float xAngle = (float) (Math.PI / 2.5); // brain rotate on x axis + float yAngle = -(float) (Math.PI / 8); // brain rotate on y axis float zAngle = 0;// brain rotate on z axis public BrainPicture(int x, int y, float brainWidth, int brainDispWidth) { super(); this.setLayout(null);// 空布局 this.brainDispWidth = brainDispWidth; - scale = 0.6f * brainDispWidth / brainWidth; + scale = 0.7f * brainDispWidth / brainWidth; this.setBounds(x, y, brainDispWidth + 1, brainDispWidth + 1); MouseAction act = new MouseAction(this); this.addMouseListener(act); @@ -56,30 +55,24 @@ public class BrainPicture extends JPanel { float x = c.x; float y = c.y; float z = c.z; - float xr = c.xr; - float yr = c.yr; - float zr = c.zr; - - setColor(Color.BLUE); - drawTopViewLine(x - xr, y - yr, z - zr, x - xr, y + yr, z - zr);// 画立方体的下面边 - drawTopViewLine(x - xr, y + yr, z - zr, x + xr, y + yr, z - zr); - drawTopViewLine(x + xr, y + yr, z - zr, x + xr, y - yr, z - zr); - drawTopViewLine(x + xr, y - yr, z - zr, x - xr, y - yr, z - zr); - - setColor(Color.green); - drawTopViewLine(x - xr, y - yr, z + zr, x - xr, y - yr, z - zr);// 画立方体的中间边 - setColor(Color.YELLOW); - drawTopViewLine(x + xr, y - yr, z + zr, x + xr, y - yr, z - zr); - setColor(Color.YELLOW); - drawTopViewLine(x + xr, y + yr, z + zr, x + xr, y + yr, z - zr); - setColor(Color.green); - drawTopViewLine(x - xr, y + yr, z + zr, x - xr, y + yr, z - zr); - - setColor(Color.red); - drawTopViewLine(x - xr, y - yr, z + zr, x - xr, y + yr, z + zr); // 画立方体的上面边 - drawTopViewLine(x - xr, y + yr, z + zr, x + xr, y + yr, z + zr); - drawTopViewLine(x + xr, y + yr, z + zr, x + xr, y - yr, z + zr); - drawTopViewLine(x + xr, y - yr, z + zr, x - xr, y - yr, z + zr); + float xe = c.xe; + float ye = c.ye; + float ze = c.ze; + + drawTopViewLine(x, y, z, x + xe, y, z);// 画立方体的下面边 + drawTopViewLine(x + xe, y, z, x + xe, y + ye, z); + drawTopViewLine(x + xe, y + ye, z, x, y + ye, z); + drawTopViewLine(x, y + ye, z, x, y, z); + + drawTopViewLine(x, y, z, x, y, z + ze);// 画立方体的中间边 + drawTopViewLine(x + xe, y, z, x + xe, y, z + ze); + drawTopViewLine(x + xe, y + ye, z, x + xe, y + ye, z + ze); + drawTopViewLine(x, y + ye, z, x, y + ye, z + ze); + + drawTopViewLine(x, y, z + ze, x + xe, y, z + ze);// 画立方体的上面边 + drawTopViewLine(x + xe, y, z + ze, x + xe, y + ye, z + ze); + drawTopViewLine(x + xe, y + ye, z + ze, x, y + ye, z + ze); + drawTopViewLine(x, y + ye, z + ze, x, y, z + ze); } /*- @@ -94,8 +87,18 @@ public class BrainPicture extends JPanel { x.cosθ-y.sinθ, x.sinθ+y.consθ, z -*/ private void drawTopViewLine(float px1, float py1, float pz1, float px2, float py2, float pz2) { - double x1 = px1 * scale, y1 = py1 * scale, z1 = pz1 * scale, x2 = px2 * scale, y2 = py2 * scale, - z2 = pz2 * scale; + double x1 = px1 - Env.FROG_BRAIN_XSIZE / 2; + double y1 = -py1 + Env.FROG_BRAIN_YSIZE / 2;// 屏幕的y坐标是反的,显示时要正过来 + double z1 = pz1 - Env.FROG_BRAIN_ZSIZE / 2; + double x2 = px2 - Env.FROG_BRAIN_XSIZE / 2; + double y2 = -py2 + Env.FROG_BRAIN_YSIZE / 2;// 屏幕的y坐标是反的,显示时要正过来 + double z2 = pz2 - Env.FROG_BRAIN_ZSIZE / 2; + x1 = x1 * scale; + y1 = y1 * scale; + z1 = z1 * scale; + x2 = x2 * scale; + y2 = y2 * scale; + z2 = z2 * scale; double x, y, z; y = y1 * cos(xAngle) - z1 * sin(xAngle);// 绕x轴转 z = y1 * sin(xAngle) + z1 * cos(xAngle); @@ -148,15 +151,16 @@ public class BrainPicture extends JPanel { if (!Application.SHOW_FIRST_FROG_BRAIN) return; Graphics g = this.getGraphics(); - g.setColor(Color.WHITE); + g.setColor(Color.WHITE);// 先清空旧图 g.fillRect(0, 0, brainDispWidth, brainDispWidth); - g.setColor(Color.black); + g.setColor(Color.black); // 画边框 g.drawRect(0, 0, brainDispWidth, brainDispWidth); - for (Organ organ : frog.organs) + for (Organ organ : frog.organs)// 每个器官负责画出自已在脑图中的位置和形状 organ.drawOnBrainPicture(frog, this); // each organ draw itself } + // getters & setters public float getScale() { return scale; } diff --git a/core3d/src/main/java/com/github/drinkjava2/frog/brain/Cube.java b/core3d/src/main/java/com/github/drinkjava2/frog/brain/Cube.java new file mode 100644 index 0000000..218e353 --- /dev/null +++ b/core3d/src/main/java/com/github/drinkjava2/frog/brain/Cube.java @@ -0,0 +1,47 @@ +/* + * 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; + +import java.util.Arrays; + +/** + * Cube include 0~n cells and 0~n photons + * + * @author Yong Zhu + * @since 1.0 + */ +public class Cube { + Cell[] cells = new Cell[] {}; + + Photon[] photons = new Photon[] {}; + + public void addCell(Cell cell) {// 每个方格空间可以存在多个脑细胞 + cells = Arrays.copyOf(cells, cells.length + 1); + cells[cells.length - 1] = cell; + } + + public void addPhoton(Photon p) {// 每个方格空间可以存在多个光子 + for (Photon old : photons) + if (old.energy < 0.01 && p.energy > -0.01) {// 如果能量没有了,用新的代替空位 + old.x = p.x; + old.y = p.y; + old.z = p.z; + old.energy = p.energy; + return; + } + photons = Arrays.copyOf(photons, photons.length + 1);// 否则追加新光子到未尾 + photons[cells.length - 1] = p; + } + + public void removePhoton(int i) {// 清除光子 + photons[i].energy = 0; + } +} diff --git a/core3d/src/main/java/com/github/drinkjava2/frog/brain/Cuboid.java b/core3d/src/main/java/com/github/drinkjava2/frog/brain/Cuboid.java index 8b40e2b..81b055c 100644 --- a/core3d/src/main/java/com/github/drinkjava2/frog/brain/Cuboid.java +++ b/core3d/src/main/java/com/github/drinkjava2/frog/brain/Cuboid.java @@ -12,8 +12,6 @@ package com.github.drinkjava2.frog.brain; import java.io.Serializable; -import com.github.drinkjava2.frog.Env; - /** * Cuboid represents a rectangular prism zone in brain * @@ -23,52 +21,33 @@ import com.github.drinkjava2.frog.Env; public class Cuboid implements Serializable { private static final long serialVersionUID = 1L; - public float x; - public float y; - public float z; - public float xr;// xr为这个矩形体x边长的一半 - public float yr;// yr为这个矩形体y边长的一半 - public float zr;// zr为这个矩形体z边长的一半 + public int x;// x,y,z是长方体的左下角坐标 + public int y; + public int z; + public int xe;// xe,ye,ze分别是长方体三边长 + public int ye; + public int ze; public Cuboid() { // 空构造器不能省 } - public Cuboid(float x, float y, float z, float r) {// 用x,y,z,r来构造 - this.x = x; - this.y = y; - this.z = z; - this.xr = r; - this.yr = r; - this.zr = r; - if (this.x < 0) - this.x = 0; - if (this.y < 0) - this.y = 0; - if (this.x > Env.FROG_BRAIN_RADIUS) - this.x = Env.FROG_BRAIN_RADIUS; - if (this.y > Env.FROG_BRAIN_RADIUS) - this.y = Env.FROG_BRAIN_RADIUS; - if (this.z > Env.FROG_BRAIN_RADIUS) - this.z = Env.FROG_BRAIN_RADIUS; - } - - public Cuboid(float x, float y, float z, float xr, float yr, float zr) {// 用x,y,z,r来构造 + public Cuboid(int x, int y, int z, int xe, int ye, int ze) {// 用x,y,z,r来构造 this.x = x; this.y = y; this.z = z; - this.xr = xr; - this.yr = yr; - this.zr = zr; + this.xe = xe; + this.ye = ye; + this.ze = ze; } public Cuboid(Cuboid c) {// 用另一个Cube来构造 this.x = c.x; this.y = c.y; this.z = c.z; - this.xr = c.xr; - this.yr = c.yr; - this.zr = c.zr; + this.xe = c.xe; + this.ye = c.ye; + this.ze = c.ze; } public static void copyXYZ(Cuboid from, Cuboid to) { @@ -77,13 +56,13 @@ public class Cuboid implements Serializable { to.z = from.z; } - public static void copyXYZR(Cuboid from, Cuboid to) { + public static void copyXYZE(Cuboid from, Cuboid to) { to.x = from.x; to.y = from.y; to.z = from.z; - to.xr = from.xr; - to.xr = from.xr; - to.xr = from.xr; + to.xe = from.xe; + to.xe = from.xe; + to.xe = from.xe; } } diff --git a/core3d/src/main/java/com/github/drinkjava2/frog/brain/MouseAction.java b/core3d/src/main/java/com/github/drinkjava2/frog/brain/MouseAction.java index 92e00f7..3e13b27 100644 --- a/core3d/src/main/java/com/github/drinkjava2/frog/brain/MouseAction.java +++ b/core3d/src/main/java/com/github/drinkjava2/frog/brain/MouseAction.java @@ -7,10 +7,12 @@ import java.awt.event.MouseWheelEvent; import java.awt.event.MouseWheelListener; /** - * BrainPicture show first frog's brain structure, for debug purpose only + * MouseAction + * + * 这个类用来处理脑图上的鼠标动作,有平移、旋转、缩放三种 * * @author Yong Zhu - * @since 1.0 + * @since 2.0.2 */ public class MouseAction implements MouseListener, MouseWheelListener, MouseMotionListener { private BrainPicture brainPic; @@ -23,10 +25,10 @@ public class MouseAction implements MouseListener, MouseWheelListener, MouseMoti } @Override - public void mousePressed(MouseEvent e) { - if (e.getButton() == 1) + public void mousePressed(MouseEvent e) {// 记录当前鼠标点 + if (e.getButton() == 1)// 旋转 buttonPressed = 1; - else if (e.getButton() == 2) + else if (e.getButton() == 2)// 缩放 buttonPressed = 2; else buttonPressed = 0; @@ -40,7 +42,7 @@ public class MouseAction implements MouseListener, MouseWheelListener, MouseMoti } @Override - public void mouseWheelMoved(MouseWheelEvent e) { + public void mouseWheelMoved(MouseWheelEvent e) {// 缩放 if (e.getWheelRotation() < 0) brainPic.scale *= 1.1; else @@ -48,7 +50,7 @@ public class MouseAction implements MouseListener, MouseWheelListener, MouseMoti } @Override - public void mouseDragged(MouseEvent e) {// do nothing + public void mouseDragged(MouseEvent e) {// 旋转 if (buttonPressed == 1) { if (e.getX() > x && e.getY() > y) brainPic.zAngle -= .00f; @@ -67,7 +69,7 @@ public class MouseAction implements MouseListener, MouseWheelListener, MouseMoti x = e.getX(); y = e.getY(); } - if (buttonPressed == 2) { + if (buttonPressed == 2) {// 平移 if (e.getX() > x) brainPic.xOffset++; if (e.getX() < x) diff --git a/core3d/src/main/java/com/github/drinkjava2/frog/brain/Organ.java b/core3d/src/main/java/com/github/drinkjava2/frog/brain/Organ.java index 41147c7..c7b058b 100644 --- a/core3d/src/main/java/com/github/drinkjava2/frog/brain/Organ.java +++ b/core3d/src/main/java/com/github/drinkjava2/frog/brain/Organ.java @@ -33,7 +33,7 @@ public class Organ extends Cuboid { } /** Only call once when frog created , Child class can override this method */ - public void initFrog(Frog f) { // 仅在Frog生成时这个方法会调用一次,缺省啥也不干,通常用于在这一步播种脑细胞 + public void init(Frog f) { // 仅在Frog生成时这个方法会调用一次,缺省啥也不干,通常用于在这一步播种脑细胞 } /** Each loop step call active method, Child class can override this method */ @@ -56,7 +56,7 @@ public class Organ extends Cuboid { } catch (Exception e) { throw new UnknownError("Can not make new Organ copy for " + this); } - copyXYZR(this, newOrgan); + copyXYZE(this, newOrgan); newOrgan.fat = this.fat; return new Organ[] { newOrgan }; } diff --git a/core3d/src/main/java/com/github/drinkjava2/frog/brain/Photon.java b/core3d/src/main/java/com/github/drinkjava2/frog/brain/Photon.java new file mode 100644 index 0000000..d82abc0 --- /dev/null +++ b/core3d/src/main/java/com/github/drinkjava2/frog/brain/Photon.java @@ -0,0 +1,37 @@ +/* + * 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; + +/** + * Photon has direction and strength + * + * 用光子来代表信息传递的载体,这是一个矢量,具有方向和能量,能量可以为负值,光子的x,y,z值表示它在每个测试step中将移动多少个脑单位距离 + * 光子永远在移动,直到被吸收转化为Cell的能量贮存或出界 + * + * @author Yong Zhu + * @since 2.0.2 + */ +public class Photon { + public float x;// 用 x,y,z长度来表示矢量的方向,便于今后计算 + public float y; + public float z; + public float energy; + + public Photon() { // 缺省构造器 + } + + public Photon(float x, float y, float z, float energy) { // 缺省构造器 + this.x = x; + this.y = y; + this.z = z; + this.energy = energy; + } +} diff --git a/core3d/src/main/java/com/github/drinkjava2/frog/brain/organ/BrainFrame.java b/core3d/src/main/java/com/github/drinkjava2/frog/brain/organ/A.java similarity index 72% rename from core3d/src/main/java/com/github/drinkjava2/frog/brain/organ/BrainFrame.java rename to core3d/src/main/java/com/github/drinkjava2/frog/brain/organ/A.java index 69ff0e9..797b006 100644 --- a/core3d/src/main/java/com/github/drinkjava2/frog/brain/organ/BrainFrame.java +++ b/core3d/src/main/java/com/github/drinkjava2/frog/brain/organ/A.java @@ -11,26 +11,29 @@ package com.github.drinkjava2.frog.brain.organ; import com.github.drinkjava2.frog.Env; +import com.github.drinkjava2.frog.Frog; import com.github.drinkjava2.frog.brain.Organ; /** - * BrainFrame one used to drawing the brain frame in BrainPicture + * A means A * * @author Yong Zhu */ -public class BrainFrame extends Organ { +public class A extends Organ { + private static final long serialVersionUID = 1L; - public BrainFrame() { - x = 0; - y = 0; - z = 0; - xr = Env.FROG_BRAIN_RADIUS / 2; - yr = xr; - zr = xr; + public A() { + x = 30; + y = 10; + z = Env.FROG_BRAIN_ZSIZE - 1; + xe = 3; + ye = 3; + ze = 1; } - public boolean allowBorrow() { // 是否允许在精子中将这个器官借出 - return false; + @Override + public void active(Frog f) { } + } diff --git a/core3d/src/main/java/com/github/drinkjava2/frog/brain/organ/PictureEye.java b/core3d/src/main/java/com/github/drinkjava2/frog/brain/organ/B.java similarity index 71% rename from core3d/src/main/java/com/github/drinkjava2/frog/brain/organ/PictureEye.java rename to core3d/src/main/java/com/github/drinkjava2/frog/brain/organ/B.java index 3f1b926..82ae6b5 100644 --- a/core3d/src/main/java/com/github/drinkjava2/frog/brain/organ/PictureEye.java +++ b/core3d/src/main/java/com/github/drinkjava2/frog/brain/organ/B.java @@ -11,26 +11,29 @@ package com.github.drinkjava2.frog.brain.organ; import com.github.drinkjava2.frog.Env; +import com.github.drinkjava2.frog.Frog; import com.github.drinkjava2.frog.brain.Organ; /** - * PictureEye can only see the picture in env + * B means B * * @author Yong Zhu */ -public class PictureEye extends Organ { +public class B extends Organ { + private static final long serialVersionUID = 1L; - public PictureEye() { - x = 0; - y = 0; - z = Env.FROG_BRAIN_RADIUS / 2 - .5f; - xr = Env.FROG_BRAIN_RADIUS / 4; - yr = xr; - zr = 0.5f; + public B() { + x = 30; + y = 15; + z = Env.FROG_BRAIN_ZSIZE - 1; + xe = 3; + ye = 3; + ze = 1; } - public boolean allowBorrow() { // 是否允许在精子中将这个器官借出 - return false; + @Override + public void active(Frog f) { } + } diff --git a/core3d/src/main/java/com/github/drinkjava2/frog/brain/organ/Brain.java b/core3d/src/main/java/com/github/drinkjava2/frog/brain/organ/Brain.java new file mode 100644 index 0000000..94df6e8 --- /dev/null +++ b/core3d/src/main/java/com/github/drinkjava2/frog/brain/organ/Brain.java @@ -0,0 +1,51 @@ +/* + * 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.organ; + +import com.github.drinkjava2.frog.Env; +import com.github.drinkjava2.frog.Frog; +import com.github.drinkjava2.frog.brain.Cube; +import com.github.drinkjava2.frog.brain.Organ; + +/** + * Brain one used to define the brain position and size + * + * 脑的形状是一个扁平的长立方体。 + * 脑的内部分布着各种各样的神经元,每个神经元的位置位于青蛙的cells3维数组里,神经元的撒布和对信息的处理特性是由其它器官来决定,器官的数量、位置、大小、 + * 对信息的处理策略参数是随机生成、变异的,由生存竟争来淘汰不合理的器官 + * + * 分布在表面的神经元,称为外表的感觉和输出细胞,同时,脑的内部也有一些不可见的输入、输出器官 + * 脑可以处理信息,每个青蛙脑被分成许多小Cube空间,每个cube里可以存在0到多个神经元,也可以存在0到多个信息元(即光子), + * 信息元(Photon)是有方向和强度的矢量。 光子的产生是由神经元(如感觉神经元或拆分神经元)产生,光子的消灭是由神经元(例如运动神经元)吸收。 + * 光子是有强度的,最普通的信息处理策略是每经过一个神经元就削弱它的强度,用抽取它代表的能量来增肥神经元,这就模拟了波的传导。当多个不同方向的光子 + * 同时到达一个神经元时,神经元被多个信号同时增强,形成驻点。有些神经元的策略是如果驻点太肥就被穿透,存放不了这么多能量,光子传向下一个cell寻求处理。 + * 所有驻点随时间减肥,以倒指数形式直到归0,这是信息的遗忘。 信息不是由一两个神经元存贮,而是由无数的驻点来存贮。 光子可以是负能量值。 + * + * @author Yong Zhu + */ +public class Brain extends Organ { + private static final long serialVersionUID = 1L; + + public Brain() { + x = 0; + y = 0; + z = 0; + xe = Env.FROG_BRAIN_XSIZE; + ye = Env.FROG_BRAIN_YSIZE; + ze = Env.FROG_BRAIN_ZSIZE; + } + + @Override + public void init(Frog f) { + f.cubes = new Cube[Env.FROG_BRAIN_XSIZE][Env.FROG_BRAIN_YSIZE][Env.FROG_BRAIN_ZSIZE]; + } + +} diff --git a/core3d/src/main/java/com/github/drinkjava2/frog/brain/Point.java b/core3d/src/main/java/com/github/drinkjava2/frog/brain/organ/C.java similarity index 60% rename from core3d/src/main/java/com/github/drinkjava2/frog/brain/Point.java rename to core3d/src/main/java/com/github/drinkjava2/frog/brain/organ/C.java index 1e72fea..eeb5572 100644 --- a/core3d/src/main/java/com/github/drinkjava2/frog/brain/Point.java +++ b/core3d/src/main/java/com/github/drinkjava2/frog/brain/organ/C.java @@ -8,23 +8,32 @@ * 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; +package com.github.drinkjava2.frog.brain.organ; + +import com.github.drinkjava2.frog.Env; +import com.github.drinkjava2.frog.Frog; +import com.github.drinkjava2.frog.brain.Organ; /** - * Point has x, y, z value + * C means C * * @author Yong Zhu - * @since 2.0.2 */ -@SuppressWarnings("all") -public class Point { - public float x; - public float y; - public float z; +public class C extends Organ { + + private static final long serialVersionUID = 1L; - public Point(float x, float y, float z) { - this.x = x; - this.y = y; - this.z = z; + public C() { + x = 35; + y = 10; + z = Env.FROG_BRAIN_ZSIZE - 1; + xe = 3; + ye = 3; + ze = 1; } + + @Override + public void active(Frog f) { + } + } diff --git a/core3d/src/main/java/com/github/drinkjava2/frog/brain/organ/D.java b/core3d/src/main/java/com/github/drinkjava2/frog/brain/organ/D.java new file mode 100644 index 0000000..ee8bd50 --- /dev/null +++ b/core3d/src/main/java/com/github/drinkjava2/frog/brain/organ/D.java @@ -0,0 +1,39 @@ +/* + * 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.organ; + +import com.github.drinkjava2.frog.Env; +import com.github.drinkjava2.frog.Frog; +import com.github.drinkjava2.frog.brain.Organ; + +/** + * D means D + * + * @author Yong Zhu + */ +public class D extends Organ { + + private static final long serialVersionUID = 1L; + + public D() { + x = 35; + y = 15; + z = Env.FROG_BRAIN_ZSIZE - 1; + xe = 3; + ye = 3; + ze = 1; + } + + @Override + public void active(Frog f) { + } + +} diff --git a/core3d/src/main/java/com/github/drinkjava2/frog/brain/organ/Eye.java b/core3d/src/main/java/com/github/drinkjava2/frog/brain/organ/Eye.java new file mode 100644 index 0000000..01188d5 --- /dev/null +++ b/core3d/src/main/java/com/github/drinkjava2/frog/brain/organ/Eye.java @@ -0,0 +1,53 @@ +/* + * 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.organ; + +import com.github.drinkjava2.frog.Env; +import com.github.drinkjava2.frog.Frog; +import com.github.drinkjava2.frog.brain.Organ; + +/** + * Eye can only see env material + * + * @author Yong Zhu + */ +public class Eye extends Organ {//这个眼睛有nxn个感光细胞,可以看到青蛙周围nxn网络内有没有食物 + private static final long serialVersionUID = 1L; + public int n = 13; // 眼睛有n x n个感光细胞, 用随机试错算法自动变异(加1或减1,最小是3x3) + + @Override + public void init(Frog f) { // 仅在Frog生成时这个方法会调用一次,缺省啥也不干,通常用于Organ类的初始化 + if (!initilized) { + initilized = true; + } + } + + public Eye() { + x = 10; + y = 10; + z = Env.FROG_BRAIN_ZSIZE - 1; + xe = 10; + ye = xe; + ze = 1; + } + + @Override + public void active(Frog f) {// 如果看到食物就在视网膜所在位置的cube上产生一些光子 + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + if (Env.foundAnyThing(f.x - n/2 + i, f.y - n /2 + j)) { + + } + } + } + } + +} diff --git a/core3d/src/main/java/com/github/drinkjava2/frog/egg/Egg.java b/core3d/src/main/java/com/github/drinkjava2/frog/egg/Egg.java index 1dc2407..7b66dd1 100644 --- a/core3d/src/main/java/com/github/drinkjava2/frog/egg/Egg.java +++ b/core3d/src/main/java/com/github/drinkjava2/frog/egg/Egg.java @@ -16,8 +16,12 @@ import java.util.List; import com.github.drinkjava2.frog.Frog; import com.github.drinkjava2.frog.brain.Organ; -import com.github.drinkjava2.frog.brain.organ.BrainFrame; -import com.github.drinkjava2.frog.brain.organ.PictureEye; +import com.github.drinkjava2.frog.brain.organ.A; +import com.github.drinkjava2.frog.brain.organ.B; +import com.github.drinkjava2.frog.brain.organ.Brain; +import com.github.drinkjava2.frog.brain.organ.C; +import com.github.drinkjava2.frog.brain.organ.D; +import com.github.drinkjava2.frog.brain.organ.Eye; import com.github.drinkjava2.frog.util.RandomUtils; /** @@ -38,8 +42,13 @@ public class Egg implements Serializable { public List organs = new ArrayList<>(); public Egg() {// 无中生有,创建一个蛋,先有蛋,后有蛙 - organs.add(new BrainFrame()); // BrainCube是固有的,用来画一个脑的立方,什么都不做 - organs.add(new PictureEye()); // BrainCube是固有的,用来画一个脑的立方,什么都不做 + organs.add(new Brain()); // BrainCube是固有的,用来画一个脑的立方,什么都不做 + organs.add(new Eye()); // BrainCube是固有的,用来画一个脑的立方,什么都不做 + organs.add(new A()); + organs.add(new B()); + organs.add(new C()); + organs.add(new D()); + } /** Create egg from frog */ diff --git a/core3d/src/main/java/com/github/drinkjava2/frog/objects/Pictures.java b/core3d/src/main/java/com/github/drinkjava2/frog/objects/LetterTester.java similarity index 57% rename from core3d/src/main/java/com/github/drinkjava2/frog/objects/Pictures.java rename to core3d/src/main/java/com/github/drinkjava2/frog/objects/LetterTester.java index d4f9bb3..f23e8f5 100644 --- a/core3d/src/main/java/com/github/drinkjava2/frog/objects/Pictures.java +++ b/core3d/src/main/java/com/github/drinkjava2/frog/objects/LetterTester.java @@ -11,29 +11,28 @@ package com.github.drinkjava2.frog.objects; /** - * Object means some thing in Env + * LetterTester used to test A, B , C, D letter recognition + * + * 这是一个临时类,用来测试青蛙的视觉模式识别功能。 在测试的前半段,它在青蛙视觉区激活一个字母的图像并同时激活对应这个字母的区(如A的图像对应A区, + * B的图像对应B区...), 然后仅仅激活图像,检测是否对应字母的区能被图像激活, 有就增加青蛙的能量,让它在生存竟争中胜出 + * * * @author Yong Zhu * @since 1.0 */ -public class Pictures implements Object { +public class LetterTester implements Object { @Override - public void build() { - // TODO Auto-generated method stub - + public void build() { // do nothing } @Override - public void destory() { - // TODO Auto-generated method stub - + public void destory() {// do nothing } @Override public void active(int screen) { - + } - } diff --git a/core3d/src/main/java/com/github/drinkjava2/frog/objects/Material.java b/core3d/src/main/java/com/github/drinkjava2/frog/objects/Material.java index bf9a1c6..33852cb 100644 --- a/core3d/src/main/java/com/github/drinkjava2/frog/objects/Material.java +++ b/core3d/src/main/java/com/github/drinkjava2/frog/objects/Material.java @@ -19,15 +19,14 @@ import java.awt.Color; * @since 1.0 */ public class Material { - public static final byte VISIBLE = 10; // if>=10 will visible to frog - public static final byte KILLFROG = 20; // if>=20 will kill frog - - public static final byte NO = 0; + public static final byte NO = 0; // nothing public static final byte SEESAW_BASE = 1; // 1~9 is invisible to frog - + + public static final byte VISIBLE = 10; // if>=10 will visible to frog public static final byte FOOD = VISIBLE + 1; public static final byte SEESAW = VISIBLE + 2; + public static final byte KILLFROG = 20; // if>=20 will kill frog public static final byte BRICK = KILLFROG + 1;// brick will kill frog public static final byte TRAP = KILLFROG + 2; // trap will kill frog diff --git a/core3d/src/main/java/com/github/drinkjava2/frog/util/StringPixelUtils.java b/core3d/src/main/java/com/github/drinkjava2/frog/util/StringPixelUtils.java new file mode 100644 index 0000000..ba7d919 --- /dev/null +++ b/core3d/src/main/java/com/github/drinkjava2/frog/util/StringPixelUtils.java @@ -0,0 +1,75 @@ +/* Copyright 2018-2020 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.util; + +import java.awt.Font; +import java.awt.FontMetrics; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.image.BufferedImage; +import java.util.HashMap; +import java.util.Map; + +/** + * StringPixelUtils used to get pixel array from a given string + * + * 根据给定的字体和字符串,返回它的像素点阵,返回结果是boolean[][]二维数组 + * + * @author Yong Zhu + * @since 2.0.2 + */ +public class StringPixelUtils { + private static final Map lettersMap = new HashMap<>(); + + public static boolean[][] getSanserif12Pixels(String s) { + return getStringPixels(Font.SANS_SERIF, Font.PLAIN, 12, s); + } + + /* 在内存 BufferedImage里输出文本并获取它的像素点 */ + public static boolean[][] getStringPixels(String fontName, int fontStyle, int fontSize, String s) { + String key = new StringBuilder(fontName).append("_").append(fontStyle).append("_").append(fontSize).append("_") + .append(s).toString(); + if (lettersMap.containsKey(key)) + return lettersMap.get(key); + Font font = new Font(fontName, fontStyle, fontSize); + + BufferedImage bi = new BufferedImage(fontSize * 8, fontSize * 50, BufferedImage.TYPE_INT_RGB); + Graphics g = bi.getGraphics(); + Graphics2D g2d = (Graphics2D) g; + g2d.setFont(font); + FontMetrics fm = g2d.getFontMetrics(); + int strHeight = fm.getAscent() + fm.getDescent() - 3; + int strWidth = fm.stringWidth(s); + g2d.drawString(s, 0, fm.getAscent() - fm.getLeading() - 1); + boolean[][] b = new boolean[strHeight][strWidth]; + for (int y = 0; y < strHeight; y++) + for (int x = 0; x < strWidth; x++) + if (bi.getRGB(x, y) == -1) + b[y][x] = true; + else + b[y][x] = false; + lettersMap.put(key, b); + return b; + } + + public static void main(String[] args) { + boolean[][] c = getStringPixels(Font.SANS_SERIF, Font.PLAIN, 10, "∮ABCD中国人"); + for (int y = 0; y < c.length; y++) { + boolean[] line = c[y]; + for (int x = 0; x < line.length; x++) + if (c[y][x]) + System.out.print("*"); + else + System.out.print(" "); + System.out.println(); + } + } +} diff --git a/捐款记录.md b/捐款记录.md index 2fe56a1..628713b 100644 --- a/捐款记录.md +++ b/捐款记录.md @@ -1,9 +1,9 @@ ¼ĿΪ֧͸һĿˣյƽ̨ľҲ¼¼ʱ˳¼˺͵ǰܶǾ˲ʱȡԸԭҲͨʷ¼ѯÿʵľڴĵظлÿһλޡעĿͬѧ (ʱ˳): -wangtao -dotao -LongFer +wangtao +dotao +LongFer Ŀǰܶ:48.88Ԫ