1 /** 2 * Copyright: © 2012-2014 Anton Gushcha 3 * License: Subject to the terms of the MIT license, as written in the included LICENSE file. 4 * Authors: NCrashed <ncrashed@gmail.com>, 5 * LeMarwin <lemarwin42@gmail.com>, 6 * Nazgull09 <nazgull90@gmail.com> 7 */ 8 module ant.progtype; 9 10 import std.stdio; 11 12 import devol.programtype; 13 import devol.world; 14 import devol.individ; 15 import devol.std.line; 16 17 import devol.typemng; 18 import devol.std.typepod; 19 import devol.std.plus; 20 import devol.std.opif; 21 import devol.operatormng; 22 23 import ant.world; 24 import ant.sense; 25 import ant.operators; 26 27 import dyaml.all; 28 29 class AntProgType : ProgTypeAbstract 30 { 31 this() 32 { 33 auto tmng = TypeMng.getSingleton(); 34 auto omng = OperatorMng.getSingleton(); 35 36 tmng.registerType!TypeBool(); 37 tmng.registerType!TypeInt(); 38 39 omng.registerOperator!If(); 40 omng.registerOperator!Plus(); 41 omng.registerOperator!OpSense(); 42 omng.registerOperator!GoForward(); 43 omng.registerOperator!TurnLeft(); 44 omng.registerOperator!TurnRight(); 45 } 46 47 @property uint progMinSize() 48 { 49 return 4; 50 } 51 52 @property uint progMaxSize() 53 { 54 return 8; 55 } 56 57 @property float newOpGenChance() 58 { 59 return 0.3; 60 } 61 62 @property float newScopeGenChance() 63 { 64 return 0.1; 65 } 66 67 @property float newLeafGenChance() 68 { 69 return 0.6; 70 } 71 72 @property uint scopeMinSize() 73 { 74 return 2; 75 } 76 77 @property uint scopeMaxSize() 78 { 79 return 5; 80 } 81 82 @property float mutationChance() 83 { 84 return 0.3; 85 } 86 87 @property float crossingoverChance() 88 { 89 return 0.7; 90 } 91 92 @property float mutationChangeChance() 93 { 94 return 0.5; 95 } 96 97 @property float mutationReplaceChance() 98 { 99 return 0.3; 100 } 101 102 @property float mutationDeleteChance() 103 { 104 return 0.2; 105 } 106 107 @property float mutationAddLineChance() 108 { 109 return 0.1; 110 } 111 112 @property float mutationRemoveLineChance() 113 { 114 return 0.05; 115 } 116 117 @property string maxMutationChange() 118 { 119 return "100"; 120 } 121 122 @property float copyingPart() 123 { 124 return 0.1; 125 } 126 127 size_t deleteMutationRiseGenomeSize() 128 { 129 return 5; 130 } 131 132 size_t maxGenomeSize() 133 { 134 return 10; 135 } 136 137 Line[] initValues(WorldAbstract pWorld) 138 { 139 return new Line[0]; 140 } 141 142 double getFitness(IndAbstract pInd, WorldAbstract pWorld, double time) 143 { 144 return (cast(Ant)(pInd)).FoodCount; 145 } 146 } 147 148 alias bool delegate() Cheat; 149 class Ant : Individ 150 { 151 this() 152 { 153 x = AntWorld.size/2; 154 y = AntWorld.size/2; 155 Direction = Faces.EAST; 156 FoodCount = 0; 157 } 158 159 this(Individ ind) 160 { 161 this(); 162 loadFrom(ind); 163 } 164 165 int x,y; //Ant's position; 166 enum Faces //What direction is it looking 167 { 168 NORTH, 169 SOUTH, 170 EAST, 171 WEST 172 }; 173 override void initialize(WorldAbstract world) 174 { 175 x = AntWorld.size/2; 176 y = AntWorld.size/2; 177 } 178 Faces Direction; 179 bool IsFood; 180 int FoodCount; 181 int prevFoodCount; 182 183 Cheat MyCheat = null; 184 void addCheat(Cheat a) 185 { 186 MyCheat = a; 187 } 188 bool isCheater() { return MyCheat != null; } 189 190 override @property Ant dup() 191 { 192 auto ind = new Ant(); 193 foreach(line; mProgram) 194 ind.mProgram ~= line.dup; 195 foreach(line; mMemory) 196 ind.mMemory ~= line.dup; 197 foreach(line; inVals) 198 ind.inVals ~= line.dup; 199 foreach(line; outVals) 200 ind.outVals ~= line.dup; 201 return ind; 202 } 203 204 static Ant loadYaml(Node node) 205 { 206 auto ind = Individ.loadYaml(node); 207 auto ant = new Ant(); 208 209 foreach(line; ind.program) 210 ant.mProgram ~= line.dup; 211 foreach(line; ind.memory) 212 ant.mMemory ~= line.dup; 213 foreach(line; ind.invals) 214 ant.inVals ~= line.dup; 215 foreach(line; ind.outvals) 216 ant.outVals ~= line.dup; 217 218 return ant; 219 } 220 }