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.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 class AntProgType : ProgTypeAbstract 28 { 29 this() 30 { 31 auto tmng = TypeMng.getSingleton(); 32 auto omng = OperatorMng.getSingleton(); 33 34 tmng.registerType!TypeBool(); 35 tmng.registerType!TypeInt(); 36 37 omng.registerOperator!If(); 38 omng.registerOperator!Plus(); 39 omng.registerOperator!OpSense(); 40 omng.registerOperator!GoForward(); 41 omng.registerOperator!TurnLeft(); 42 omng.registerOperator!TurnRight(); 43 } 44 45 @property uint progMinSize() 46 { 47 return 4; 48 } 49 50 @property uint progMaxSize() 51 { 52 return 8; 53 } 54 55 @property float newOpGenChance() 56 { 57 return 0.3; 58 } 59 60 @property float newScopeGenChance() 61 { 62 return 0.1; 63 } 64 65 @property float newLeafGenChance() 66 { 67 return 0.6; 68 } 69 70 @property uint scopeMinSize() 71 { 72 return 2; 73 } 74 75 @property uint scopeMaxSize() 76 { 77 return 5; 78 } 79 80 @property float mutationChance() 81 { 82 return 0.3; 83 } 84 85 @property float crossingoverChance() 86 { 87 return 0.7; 88 } 89 90 @property float mutationChangeChance() 91 { 92 return 0.5; 93 } 94 95 @property float mutationReplaceChance() 96 { 97 return 0.3; 98 } 99 100 @property float mutationDeleteChance() 101 { 102 return 0.2; 103 } 104 105 @property float mutationAddLineChance() 106 { 107 return 0.1; 108 } 109 110 @property float mutationRemoveLineChance() 111 { 112 return 0.05; 113 } 114 115 @property string maxMutationChange() 116 { 117 return "100"; 118 } 119 120 @property float copyingPart() 121 { 122 return 0.1; 123 } 124 125 Line[] initValues(WorldAbstract pWorld) 126 { 127 return new Line[0]; 128 } 129 130 double getFitness(IndAbstract pInd, WorldAbstract pWorld, double time) 131 { 132 return (cast(Ant)(pInd)).FoodCount; 133 } 134 } 135 136 alias bool delegate() Cheat; 137 class Ant : Individ 138 { 139 this() 140 { 141 x = AntWorld.size/2; 142 y = AntWorld.size/2; 143 Direction = Faces.EAST; 144 FoodCount = 0; 145 } 146 int x,y; //Ant's position; 147 enum Faces //What direction is it looking 148 { 149 NORTH, 150 SOUTH, 151 EAST, 152 WEST 153 }; 154 override void initialize() 155 { 156 x = AntWorld.size/2; 157 y = AntWorld.size/2; 158 } 159 Faces Direction; 160 bool IsFood; 161 int FoodCount; 162 int prevFoodCount; 163 164 Cheat MyCheat = null; 165 void addCheat(Cheat a) 166 { 167 MyCheat = a; 168 } 169 bool isCheater() { return MyCheat != null; } 170 171 override @property Ant dup() 172 { 173 auto ind = new Ant(); 174 foreach(line; mProgram) 175 ind.mProgram ~= line.dup; 176 foreach(line; mMemory) 177 ind.mMemory ~= line.dup; 178 foreach(line; inVals) 179 ind.inVals ~= line.dup; 180 foreach(line; outVals) 181 ind.outVals ~= line.dup; 182 return ind; 183 } 184 }