Car.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Xml.Serialization;
  5. using OpenTK.Graphics.OpenGL;
  6. using Raahn;
  7. namespace RaahnSimulation
  8. {
  9. [XmlRoot("Robot")]
  10. public class CarConfig
  11. {
  12. [XmlElement("X")]
  13. public double x;
  14. [XmlElement("Y")]
  15. public double y;
  16. [XmlElement("Angle")]
  17. public double angle;
  18. }
  19. public partial class Car : Entity
  20. {
  21. public const double HALF_QUERY_WIDTH = Simulator.WORLD_WINDOW_WIDTH / 2.0;
  22. public const double HALF_QUERY_HEIGHT = Simulator.WORLD_WINDOW_HEIGHT / 2.0;
  23. private const int UNIQUE_GROUP_TYPES = 3;
  24. private const double CONTROL_THRESHOLD = 0.5;
  25. private const double ROTATE_SPEED = 2.0;
  26. private const double SPEED_X = 15.0;
  27. private const double SPEED_Y = 12.0;
  28. private const double MAX_ROTATE = 1.0;
  29. private const double MIN_ROTATE = 0.0;
  30. //Between 0 and twice ROTATE_SPEED
  31. private const double ROTATE_RANGE = 2.0 * ROTATE_SPEED;
  32. public List<Entity> entitiesHovering;
  33. private bool configLoaded;
  34. private bool userControlled;
  35. private uint rangeFinderCount;
  36. private uint pieSliceSensorCount;
  37. private QuadTree quadTree;
  38. private ControlScheme.SchemeFunction controlScheme;
  39. //Each list corresponds to a modulation scheme.
  40. private List<List<uint>> modulationSignals;
  41. private List<List<int>> orderdGroupIds;
  42. private List<ModulationScheme.SchemeFunction> modulationSchemes;
  43. private List<RangeFinderGroup> rangeFinderGroups;
  44. private List<PieSliceSensorGroup> pieSliceSensorGroups;
  45. private NeuralNetwork brain;
  46. public Car(Simulator sim, QuadTree tree) : base(sim)
  47. {
  48. texture = TextureManager.TextureType.CAR;
  49. type = EntityType.CAR;
  50. userControlled = false;
  51. quadTree = tree;
  52. modulationSignals = new List<List<uint>>();
  53. modulationSchemes = new List<ModulationScheme.SchemeFunction>();
  54. rangeFinderGroups = new List<RangeFinderGroup>();
  55. pieSliceSensorGroups = new List<PieSliceSensorGroup>();
  56. speed.x = SPEED_X;
  57. speed.y = SPEED_Y;
  58. entitiesHovering = new List<Entity>();
  59. //3 types of ids, input, hidden, output
  60. orderdGroupIds = new List<List<int>>(UNIQUE_GROUP_TYPES);
  61. for (int i = 0; i < UNIQUE_GROUP_TYPES; i++)
  62. orderdGroupIds.Add(new List<int>());
  63. }
  64. //Updates only sensors.
  65. public void UpdateMinimal()
  66. {
  67. base.Update();
  68. for (int i = 0; i < rangeFinderGroups.Count; i++)
  69. rangeFinderGroups[i].Update();
  70. for (int i = 0; i < pieSliceSensorGroups.Count; i++)
  71. pieSliceSensorGroups[i].Update();
  72. }
  73. public void Control()
  74. {
  75. if (controlScheme != null)
  76. userControlled = controlScheme(this);
  77. }
  78. public override void Update()
  79. {
  80. base.Update();
  81. double worldX = GetWorldX();
  82. double worldY = GetWorldY();
  83. Utils.Vector2 lowerLeft = camera.TransformWorld(worldX - HALF_QUERY_WIDTH, worldY - HALF_QUERY_HEIGHT);
  84. Utils.Vector2 upperRight = camera.TransformWorld(worldX + HALF_QUERY_WIDTH, worldY + HALF_QUERY_HEIGHT);
  85. AABB viewBounds = new AABB(upperRight.x - lowerLeft.x, upperRight.y - lowerLeft.y);
  86. viewBounds.Translate(lowerLeft.x, lowerLeft.y);
  87. Utils.LineSegment collisionLine = new Utils.LineSegment();
  88. Utils.Point2 original = new Utils.Point2(center.x, center.y);
  89. Utils.Point2 projected = new Utils.Point2(center.x + velocity.x, center.y + velocity.y);
  90. collisionLine.SetUp(original, projected);
  91. List<Entity> entitiesInBounds = quadTree.Query(viewBounds);
  92. bool canMove = true;
  93. for (int i = 0; i < entitiesInBounds.Count; i++)
  94. {
  95. if (entitiesInBounds[i].GetEntityType() == EntityType.WALL)
  96. {
  97. Utils.LineSegment compare = ((Wall)entitiesInBounds[i]).GetLineSegment();
  98. List<Utils.Point2> intersections = collisionLine.Intersects(compare);
  99. //If there is a collision, don't move.
  100. if (intersections.Count > 0)
  101. {
  102. canMove = false;
  103. break;
  104. }
  105. }
  106. }
  107. if (canMove)
  108. {
  109. drawingVec.x += velocity.x;
  110. drawingVec.y += velocity.y;
  111. }
  112. for (int i = 0; i < modulationSchemes.Count; i++)
  113. {
  114. if (!userControlled)
  115. modulationSchemes[i](this, entitiesInBounds, modulationSignals[i]);
  116. else
  117. {
  118. for (int j = 0; j < modulationSignals[i].Count; j++)
  119. ModulationSignal.SetSignal(modulationSignals[i][j], ModulationSignal.NO_MODULATION);
  120. }
  121. }
  122. brain.Train();
  123. //Update sensors.
  124. for (int i = 0; i < rangeFinderGroups.Count; i++)
  125. rangeFinderGroups[i].Update();
  126. for (int i = 0; i < pieSliceSensorGroups.Count; i++)
  127. pieSliceSensorGroups[i].Update();
  128. }
  129. public override void UpdateEvent(Event e)
  130. {
  131. base.UpdateEvent(e);
  132. }
  133. public override void Draw()
  134. {
  135. base.Draw();
  136. GL.PushMatrix();
  137. GL.Translate(center.x, center.y, Utils.DISCARD_Z_POS);
  138. GL.Rotate(angle, 0.0, 0.0, 1.0);
  139. GL.Translate(-center.x, -center.y, -Utils.DISCARD_Z_POS);
  140. GL.Translate(drawingVec.x, drawingVec.y, Utils.DISCARD_Z_POS);
  141. GL.Scale(width, height, Utils.DISCARD_Z_SCALE);
  142. GL.DrawElements(mesh.GetRenderMode(), mesh.GetIndexCount(), DrawElementsType.UnsignedShort, IntPtr.Zero);
  143. GL.PopMatrix();
  144. for (int i = 0; i < rangeFinderGroups.Count; i++)
  145. rangeFinderGroups[i].Draw();
  146. for (int i = 0; i < pieSliceSensorGroups.Count; i++)
  147. pieSliceSensorGroups[i].Draw();
  148. }
  149. public override void DebugDraw()
  150. {
  151. base.DebugDraw();
  152. }
  153. public override void Clean()
  154. {
  155. RangeFinderGroup.Clean();
  156. PieSliceSensorGroup.Clean();
  157. }
  158. public void Reset()
  159. {
  160. if (brain != null)
  161. brain.Reset();
  162. for (int x = 0; x < modulationSignals.Count; x++)
  163. {
  164. for (int y = 0; y < modulationSignals[x].Count; y++)
  165. ModulationSignal.SetSignal(modulationSignals[x][y], ModulationSignal.NO_MODULATION);
  166. }
  167. ModulationScheme.Reset();
  168. }
  169. public bool LoadConfig(string sensorFile, string networkFile)
  170. {
  171. bool loadSucceeded = true;
  172. //If a configuration was already loaded delete the
  173. //VBOs and IBOs used as new ones will be allocated.
  174. if (configLoaded)
  175. {
  176. RangeFinderGroup.Clean();
  177. PieSliceSensorGroup.Clean();
  178. configLoaded = false;
  179. }
  180. if (!string.IsNullOrEmpty(sensorFile))
  181. {
  182. if (!InitSensors(sensorFile))
  183. loadSucceeded = false;
  184. }
  185. if (!string.IsNullOrEmpty(networkFile))
  186. {
  187. if (!InitBrain(networkFile))
  188. loadSucceeded = false;
  189. }
  190. configLoaded = true;
  191. return loadSucceeded;
  192. }
  193. public uint GetRangeFinderCount()
  194. {
  195. return rangeFinderCount;
  196. }
  197. public uint GetPieSliceSensorCount()
  198. {
  199. return pieSliceSensorCount;
  200. }
  201. public List<List<int>> GetNeuronGroupIds()
  202. {
  203. return orderdGroupIds;
  204. }
  205. public NeuralNetwork GetBrain()
  206. {
  207. return brain;
  208. }
  209. private bool InitSensors(string sensorFile)
  210. {
  211. if (!File.Exists(sensorFile))
  212. {
  213. Console.WriteLine(string.Format(Utils.FILE_NOT_FOUND, sensorFile));
  214. return false;
  215. }
  216. TextReader configReader = new StreamReader(sensorFile);
  217. SensorConfig sensorConfig = null;
  218. try
  219. {
  220. XmlSerializer deserializer = new XmlSerializer(typeof(SensorConfig));
  221. sensorConfig = (SensorConfig)deserializer.Deserialize(configReader);
  222. }
  223. catch (Exception e)
  224. {
  225. Console.WriteLine(Utils.XML_READ_ERROR);
  226. Console.WriteLine(Utils.SENSOR_LOAD_ERROR);
  227. Console.WriteLine(e.Message);
  228. return false;
  229. }
  230. finally
  231. {
  232. configReader.Close();
  233. }
  234. if (sensorConfig.rangeFinderGroups != null)
  235. {
  236. for (int i = 0; i < sensorConfig.rangeFinderGroups.Length; i++)
  237. {
  238. RangeFinderGroupConfig current = sensorConfig.rangeFinderGroups[i];
  239. if (current == null)
  240. continue;
  241. rangeFinderCount += current.count;
  242. RangeFinderGroup rfg = new RangeFinderGroup(context, this, quadTree, current.count);
  243. rfg.Configure(current.length, current.angleOffset, current.angleBetween);
  244. if (current.entitiesToDetect != null)
  245. {
  246. for (int n = 0; n < current.entitiesToDetect.Length; n++)
  247. {
  248. Entity.EntityType type = Entity.GetTypeFromString(current.entitiesToDetect[n]);
  249. if (type != Entity.EntityType.NONE)
  250. rfg.AddEntityToDetect(type);
  251. }
  252. }
  253. rangeFinderGroups.Add(rfg);
  254. }
  255. }
  256. if (sensorConfig.pieSliceSensorGroups != null)
  257. {
  258. for (int i = 0; i < sensorConfig.pieSliceSensorGroups.Length; i++)
  259. {
  260. PieSliceSensorGroupConfig current = sensorConfig.pieSliceSensorGroups[i];
  261. if (current == null)
  262. continue;
  263. pieSliceSensorCount += current.count;
  264. PieSliceSensorGroup pieGroup = new PieSliceSensorGroup(context, this, quadTree);
  265. pieGroup.AddSensors(current.count);
  266. pieGroup.ConfigureSensors(current.maxDetection, current.angleOffset, current.angleBetween, current.outerRadius, current.innerRadius);
  267. if (current.entitiesToDetect != null)
  268. {
  269. for (int n = 0; n < current.entitiesToDetect.Length; n++)
  270. {
  271. Entity.EntityType type = Entity.GetTypeFromString(current.entitiesToDetect[n]);
  272. if (type != Entity.EntityType.NONE)
  273. pieGroup.AddEntityToDetect(type);
  274. }
  275. }
  276. pieSliceSensorGroups.Add(pieGroup);
  277. }
  278. }
  279. return true;
  280. }
  281. private bool InitBrain(string networkFile)
  282. {
  283. if (!File.Exists(networkFile))
  284. {
  285. Console.WriteLine(Utils.FILE_NOT_FOUND, networkFile);
  286. //brain must be instantiated before leaving.
  287. brain = new NeuralNetwork();
  288. return false;
  289. }
  290. TextReader configReader = new StreamReader(networkFile);
  291. NeuralNetworkConfig networkConfig = null;
  292. try
  293. {
  294. XmlSerializer deserializer = new XmlSerializer(typeof(NeuralNetworkConfig));
  295. networkConfig = (NeuralNetworkConfig)deserializer.Deserialize(configReader);
  296. }
  297. catch (Exception e)
  298. {
  299. Console.WriteLine(Utils.XML_READ_ERROR);
  300. Console.WriteLine(Utils.NETWORK_LOAD_ERROR);
  301. Console.WriteLine(e.Message);
  302. brain = new NeuralNetwork();
  303. return false;
  304. }
  305. finally
  306. {
  307. configReader.Close();
  308. }
  309. if (networkConfig.historyBufferSize > 0)
  310. brain = new NeuralNetwork(networkConfig.historyBufferSize, networkConfig.useNovelty);
  311. else
  312. brain = new NeuralNetwork();
  313. brain.SetOutputNoiseMagnitude(networkConfig.outputNoiseMagnitude);
  314. brain.SetWeightNoiseMagnitude(networkConfig.weightNoiseMagnitude);
  315. //No neuron groups, connection groups, or control scheme.
  316. if (networkConfig.neuronGroups == null)
  317. {
  318. Console.WriteLine(Utils.NO_NEURON_GROUPS);
  319. return false;
  320. }
  321. if (networkConfig.connectionGroups == null)
  322. {
  323. Console.WriteLine(Utils.NO_CONNECTION_GROUPS);
  324. return false;
  325. }
  326. if (networkConfig.controlScheme == null)
  327. {
  328. Console.WriteLine(Utils.NO_CONTROL_SCHEME);
  329. return false;
  330. }
  331. if (networkConfig.weightCap != 0.0)
  332. brain.SetWeightCap(networkConfig.weightCap);
  333. ControlScheme.Scheme cDescriptor = ControlScheme.GetSchemeFromString(networkConfig.controlScheme);
  334. if (cDescriptor == ControlScheme.Scheme.NONE)
  335. {
  336. Console.WriteLine(Utils.NO_CONTROL_SCHEME);
  337. return false;
  338. }
  339. controlScheme = ControlScheme.GetSchemeFunction(cDescriptor);
  340. //Save the modulation descriptions in addition to the functions
  341. //because the parameter interpreting function takes the descriptions.
  342. List<ModulationScheme.Scheme> mDescriptions = new List<ModulationScheme.Scheme>();
  343. List<NeuronGroupConfig> neuronGroups = new List<NeuronGroupConfig>(networkConfig.neuronGroups.Length);
  344. List<int> neuronGroupIds = new List<int>(networkConfig.neuronGroups.Length);
  345. for (int i = 0; i < networkConfig.neuronGroups.Length; i++)
  346. neuronGroups.Add(networkConfig.neuronGroups[i]);
  347. //Add each neuron group.
  348. for (int i = 0; i < neuronGroups.Count; i++)
  349. {
  350. NeuronGroupConfig nGroupConfig = neuronGroups[(int)i];
  351. NeuralNetwork.NeuronGroup.Type type = Utils.GetGroupTypeFromString(nGroupConfig.type);
  352. if (type == NeuralNetwork.NeuronGroup.Type.NONE)
  353. neuronGroups.RemoveAt(i);
  354. else
  355. {
  356. int index = brain.AddNeuronGroup(nGroupConfig.count, type);
  357. neuronGroupIds.Add(index);
  358. orderdGroupIds[(int)type].Add(index);
  359. }
  360. }
  361. //Add each connection group.
  362. for (int i = 0; i < networkConfig.connectionGroups.Length; i++)
  363. {
  364. ConnectionConfig cGroupConfig = networkConfig.connectionGroups[i];
  365. uint inputGroupIndex = GetIdIndex(cGroupConfig.inputGroupId, neuronGroups);
  366. uint outputGroupIndex = GetIdIndex(cGroupConfig.outputGroupId, neuronGroups);
  367. //Make sure the neuron groups to be connected exist.
  368. if (inputGroupIndex < networkConfig.neuronGroups.Length
  369. && outputGroupIndex < networkConfig.neuronGroups.Length)
  370. {
  371. NeuralNetwork.NeuronGroup.Identifier inputGroup;
  372. inputGroup.index = neuronGroupIds[(int)inputGroupIndex];
  373. string inputTypeString = neuronGroups[(int)inputGroupIndex].type;
  374. inputGroup.type = Utils.GetGroupTypeFromString(inputTypeString);
  375. NeuralNetwork.NeuronGroup.Identifier outputGroup;
  376. outputGroup.index = neuronGroupIds[(int)outputGroupIndex];
  377. string outputTypeString = neuronGroups[(int)outputGroupIndex].type;
  378. outputGroup.type = Utils.GetGroupTypeFromString(outputTypeString);
  379. NeuralNetwork.ConnectionGroup.TrainFunctionType trainMethod = Utils.GetMethodFromString(cGroupConfig.trainingMethod);
  380. //Check if a modulation scheme is specified.
  381. ModulationScheme.Scheme mDescriptor;
  382. if (!string.IsNullOrEmpty(cGroupConfig.modulationScheme))
  383. mDescriptor = ModulationScheme.GetSchemeFromString(cGroupConfig.modulationScheme);
  384. else
  385. mDescriptor = ModulationScheme.Scheme.NONE;
  386. //If a scheme is specified, add a signal for it.
  387. if (mDescriptor != ModulationScheme.Scheme.NONE)
  388. {
  389. uint modSig = ModulationSignal.AddSignal();
  390. ModulationScheme.SchemeFunction mFunction = ModulationScheme.GetSchemeFunction(mDescriptor);
  391. //Check if the modulation scheme function has already
  392. //been added to the list of modulation scheme functions.
  393. bool hasFunction = false;
  394. for (int n = 0; n < modulationSchemes.Count; n++)
  395. {
  396. if (modulationSchemes[n] == mFunction)
  397. {
  398. modulationSignals[n].Add(modSig);
  399. hasFunction = true;
  400. break;
  401. }
  402. }
  403. //If the modulation scheme function was not added
  404. //add it to the list of scheme functions.
  405. if (!hasFunction)
  406. {
  407. mDescriptions.Add(mDescriptor);
  408. modulationSchemes.Add(mFunction);
  409. modulationSignals.Add(new List<uint>());
  410. modulationSignals[modulationSignals.Count - 1].Add(modSig);
  411. }
  412. brain.ConnectGroups(inputGroup, outputGroup, trainMethod, (int)modSig,
  413. cGroupConfig.samplesPerTick, cGroupConfig.learningRate, cGroupConfig.useBias);
  414. }
  415. else
  416. brain.ConnectGroups(inputGroup, outputGroup, trainMethod, ModulationSignal.INVALID_INDEX,
  417. cGroupConfig.samplesPerTick, cGroupConfig.learningRate, cGroupConfig.useBias);
  418. }
  419. }
  420. //Interpret the parameters of the network file
  421. //for the control scheme and the modulation schemes.
  422. if (networkConfig.parameters != null)
  423. ControlScheme.InterpretParameters(networkConfig.parameters, cDescriptor);
  424. for (int i = 0; i < mDescriptions.Count; i++)
  425. ModulationScheme.InterpretParameters(networkConfig.parameters, mDescriptions[i]);
  426. return true;
  427. }
  428. private uint GetIdIndex(uint id, List<NeuronGroupConfig> neuronGroups)
  429. {
  430. for (uint i = 0; i < neuronGroups.Count; i++)
  431. {
  432. if (id == neuronGroups[(int)i].id)
  433. return i;
  434. }
  435. //Return the first index if the id is not found.
  436. return 0;
  437. }
  438. private Utils.Point2 GetNearestIntersection(List<Utils.Point2> intersections)
  439. {
  440. Utils.Point2 nearest = intersections[0];
  441. for (int x = 1; x < intersections.Count; x++)
  442. {
  443. Utils.Point2 currentIntersection = intersections[x];
  444. Utils.Point2 centerPoint = new Utils.Point2(center.x, center.y);
  445. if (Utils.GetDist(nearest, centerPoint) > Utils.GetDist(currentIntersection, centerPoint))
  446. nearest = intersections[x];
  447. }
  448. return nearest;
  449. }
  450. }
  451. }