private void loadNetworkToolStripMenuItem_Click(object sender, EventArgs e) { string filename; OpenFileDialog oDialog = new OpenFileDialog(); oDialog.AddExtension = true; oDialog.DefaultExt = "xml"; oDialog.Title = "Load Seed Genome"; oDialog.RestoreDirectory = true; // Show Open Dialog and Respond to OK if (oDialog.ShowDialog() == DialogResult.OK) { filename = oDialog.FileName; } else { return; } NeatGenome.NeatGenome seedGenome = null; try { XmlDocument doc = new XmlDocument(); doc.Load(filename); seedGenome = XmlNeatGenomeReaderStatic.Read(doc); } catch (Exception ex) { MessageBox.Show("Problem loading genome. \n" + ex.Message); return; } currentBest = GenomeDecoder.DecodeToFloatFastConcurrentNetwork(seedGenome, null); }
private void ShowGenomeNetwork(NeatGenome genome) { NetworkControl networkControl = new NetworkControl(); networkControl.Dock = DockStyle.Fill; panelNetWorkViewer.Controls.Clear(); panelNetWorkViewer.Controls.Add(networkControl); /* create network model to draw the network */ NetworkModel networkModel = GenomeDecoder.DecodeToNetworkModel(genome); GridLayoutManager layoutManager = new GridLayoutManager(); layoutManager.Layout(networkModel, networkControl.Size); networkControl.NetworkModel = networkModel; }
public static Stats postHocAnalyzer(NeatGenome.NeatGenome genome) { Stats stats = new Stats(); FloatFastConcurrentNetwork ffcn = GenomeDecoder.DecodeToFloatFastConcurrentNetwork(genome, null); INetwork network; network = substrate.generateNetwork(ffcn); stats.NNconnections = ((FloatFastConcurrentNetwork)network).connectionArray.Length; double avgSpeed = 0; double totalTime = 0; int numFood = 0; double timetaken = 0; for (int i = 0; i < FoodGatherParams.foodLocations.Length; i++) { Board testingArena = new Board(0, 500); Robot tester = new Robot(new PointF(testingArena.Size.Width / 2.0F, testingArena.Size.Height / 2.0F), (int)FoodGatherParams.resolution, network); testingArena.AddRobot(tester); testingArena.AddFood(FoodGatherParams.foodLocations[i]); timetaken = testingArena.game(); if (timetaken < 1000) { numFood++; } totalTime += timetaken; } avgSpeed = totalTime / FoodGatherParams.foodLocations.Length; stats.numFoods = numFood; stats.avgSpeed = avgSpeed; return(stats); }
public ESBodyInformation genomeIntoBodyObject(IGenome genome, out bool isEmpty) { INetwork net = GenomeDecoder.DecodeToModularNetwork((NeatGenome)genome); isEmpty = false; //we want the genome, so we can acknowledge the genomeID! //now convert a network to a set of hidden neurons and connections //we'll make body specific function calls later var allBodyOutputs = new List <List <float> >(); var allBodyInputs = new List <PointPair>(); var indexToConnectionMap = new Dictionary <int, int>(); List <PointF> inputs, outputs, hiddenNeurons; inputs = new List <PointF>(); outputs = new List <PointF>(); hiddenNeurons = new List <PointF>(); //inputs.Add(new PointF(0,0)); //int initialDepth, ESIterations; //uint inputCount, outputCount; //float varianceThreshold, bandThreshold; ConnectionGeneList connections = new ConnectionGeneList(); //loop through a grid, defined by some resolution, and test every connection against another using leo int resolution = 9; //int resolutionHalf = resolution / 2; List <PointF> queryPoints = gridQueryPoints(resolution); float xDistanceThree = dXDistance(resolution, 3.0f); float yDistanceThree = dYDistance(resolution, 3.0f); bool useLeo = true; int counter = 0; Dictionary <long, PointF> conSourcePoints = new Dictionary <long, PointF>(); Dictionary <long, PointF> conTargetPoints = new Dictionary <long, PointF>(); //Dictionary<string, List<PointF>> pointsChecked = new Dictionary<string, List<PointF>>(); //List<PointF> pList; int src, tgt; //for each points we have for (int p1 = 0; p1 < queryPoints.Count; p1++) { PointF xyPoint = queryPoints[p1]; //query against all other points (possibly limiting certain connection lengths for (int p2 = p1; p2 < queryPoints.Count; p2++) { PointF otherPoint = queryPoints[p2]; if (p1 != p2 && (Math.Abs(xyPoint.X - otherPoint.X) < xDistanceThree && Math.Abs(xyPoint.Y - otherPoint.Y) < yDistanceThree)) { //if(!pointsChecked.TryGetValue(xyPoint.ToString(), out pList)) //{ // pList = new List<PointF>(); // pointsChecked.Add(xyPoint.ToString(), pList); //} //pList.Add(otherPoint); //if (!pointsChecked.TryGetValue(otherPoint.ToString(), out pList)) //{ // pList = new List<PointF>(); // pointsChecked.Add(otherPoint.ToString(), pList); //} //pList.Add(xyPoint); //Console.WriteLine("Checking: ({0}, {1}) => ({2}, {3}) ", xyPoint.X, xyPoint.Y, otherPoint.X, otherPoint.Y); float[] outs = queryCPPNOutputs((ModularNetwork)net, xyPoint.X, xyPoint.Y, otherPoint.X, otherPoint.Y, maxXDistanceCenter(xyPoint, otherPoint), minYDistanceGround(xyPoint, otherPoint)); float weight = outs[0]; allBodyInputs.Add(new PointPair(xyPoint, otherPoint)); allBodyOutputs.Add(new List <float>(outs)); if (useLeo) { if (outs[1] > 0) { //Console.WriteLine("XY: " + xyPoint + " Other: " + otherPoint + " LEO : " + outs[1]) ; //Console.WriteLine(" XDist: " + sqrt(xDistanceSq(xyPoint, otherPoint)) // + " yDist : " + sqrt(yDistanceSq(xyPoint, otherPoint)) // + " MaxDist: " + maxXDistanceCenter(xyPoint, otherPoint)) //+ " MinY: " + minYDistanceGround(xyPoint, otherPoint)); //Console.WriteLine(); //add to hidden neurons if (!hiddenNeurons.Contains(xyPoint)) { hiddenNeurons.Add(xyPoint); } src = hiddenNeurons.IndexOf(xyPoint); if (!hiddenNeurons.Contains(otherPoint)) { hiddenNeurons.Add(otherPoint); } tgt = hiddenNeurons.IndexOf(otherPoint); conSourcePoints.Add(counter, xyPoint); conTargetPoints.Add(counter, otherPoint); indexToConnectionMap.Add(allBodyOutputs.Count - 1, counter); connections.Add(new ConnectionGene(counter++, (src), (tgt), weight * HyperNEATParameters.weightRange, new float[] { xyPoint.X, xyPoint.Y, otherPoint.X, otherPoint.Y }, outs)); } } else { //add to hidden neurons if (!hiddenNeurons.Contains(xyPoint)) { hiddenNeurons.Add(xyPoint); } src = hiddenNeurons.IndexOf(xyPoint); if (!hiddenNeurons.Contains(otherPoint)) { hiddenNeurons.Add(otherPoint); } tgt = hiddenNeurons.IndexOf(otherPoint); conSourcePoints.Add(counter, xyPoint); conTargetPoints.Add(counter, otherPoint); indexToConnectionMap.Add(allBodyOutputs.Count - 1, counter); connections.Add(new ConnectionGene(counter++, (src), (tgt), weight * HyperNEATParameters.weightRange, new float[] { xyPoint.X, xyPoint.Y, otherPoint.X, otherPoint.Y }, outs)); } //PointF newp = new PointF(p.x2, p.y2); //targetIndex = hiddenNeurons.IndexOf(newp); //if (targetIndex == -1) //{ // targetIndex = hiddenNeurons.Count; // hiddenNeurons.Add(newp); //} //connections.Add(new ConnectionGene(counter++, (sourceIndex), (targetIndex + inputCount + outputCount), p.weight * HyperNEATParameters.weightRange, new float[] { p.x1, p.y1, p.x2, p.y2 }, p.Outputs)); } } } //esSubstrate.generateSubstrate(inputs, outputs, net, // HyperNEATParameters.initialDepth, // (float)HyperNEATParameters.varianceThreshold, // (float)HyperNEATParameters.bandingThreshold, // HyperNEATParameters.ESIterations, // (float)HyperNEATParameters.divisionThreshold, // HyperNEATParameters.maximumDepth, // (uint)inputs.Count, (uint)outputs.Count, // ref connections, ref hiddenNeurons, true); //generateSubstrate(List<System.Drawing.PointF> inputNeuronPositions, List<PointF> outputNeuronPositions, //INetwork genome, int initialDepth, float varianceThreshold, float bandThreshold, int ESIterations, // float divsionThreshold, int maxDepth, // uint inputCount, uint outputCount, // ref ConnectionGeneList connections, ref List<PointF> hiddenNeurons) //blow out the object, we don't care about testing it //foreach (var pPair in pointsChecked) //{ // Console.WriteLine("Checking: " + pPair.Key + " processed: "); // foreach (var xyPoint in pPair.Value) // { // Console.WriteLine("({0}, {1}) ", xyPoint.X, xyPoint.Y); // } //} var beforeConn = connections.Count; var beforeNeuron = hiddenNeurons.Count; //var hiddenCopy = new List<PointF>(hiddenNeurons); ensureSingleConnectedStructure(connections, hiddenNeurons, conSourcePoints, conTargetPoints); if (hiddenNeurons.Count > 20 || connections.Count > 100) { hiddenNeurons = new List <PointF>(); connections = new ConnectionGeneList(); } if (hiddenNeurons.Count == 0 || connections.Count == 0) { isEmpty = true; } NeatGenome ng = (NeatGenome)genome; bool behaviorExists = (ng.Behavior != null); ESBodyInformation esbody = new ESBodyInformation() { AllBodyOutputs = allBodyOutputs, AllBodyInputs = allBodyInputs, indexToConnection = indexToConnectionMap, //PreHiddenLocations = hiddenCopy, BeforeNeuron = beforeNeuron, BeforeConnection = beforeConn, GenomeID = genome.GenomeId, Connections = connections, HiddenLocations = hiddenNeurons, InputLocations = inputs, Objectives = ng.objectives, Fitness = ng.Fitness, Locality = ng.locality, useLEO = useLeo }; Console.WriteLine(" Nodes: " + hiddenNeurons.Count + " Connections: " + connections.Count); return(esbody); }