/// <summary> /// Evaluate the provided IBlackBox against the MNIST problem domain and return its fitness/novlety score. /// </summary> public FitnessInfo Evaluate(IBlackBox box) { //just keep track of evals //_evalCount++; double fitness, altFitness; //these are our inputs and outputs ISignalArray inputArr = box.InputSignalArray; ISignalArray outputArr = box.OutputSignalArray; List <Sample> sampleList = LEEAParams.DOMAIN.getSamples(); int sampleCount = sampleList.Count; fitness = sampleList.Count; foreach (Sample sample in sampleList) { inputArr.CopyFrom(sample.Inputs, 0); box.ResetState(); box.Activate(); fitness -= Math.Abs(outputArr[0] - sample.Outputs[0]) * Math.Abs(outputArr[0] - sample.Outputs[0]); } // set alternate fitness to fitness measured against all samples if (SharedParams.SharedParams.PERFORMFULLEVAL) { sampleList = LEEAParams.DOMAIN.getValidationSamples(); altFitness = sampleList.Count; foreach (Sample sample in sampleList) { inputArr.CopyFrom(sample.Inputs, 0); box.ResetState(); box.Activate(); altFitness -= Math.Abs(outputArr[0] - sample.Outputs[0]) * Math.Abs(outputArr[0] - sample.Outputs[0]); } } else { altFitness = 0; } fitness = Math.Max(fitness, LEEAParams.MINFITNESS) + LEEAParams.FITNESSBASE; return(new FitnessInfo(fitness, altFitness)); }
private double[] activate(IBlackBox box, IEnumerable <double> inputs, double[] outputs) { box.ResetState(); // Give inputs to the network var nodeId = 0; foreach (var input in inputs) { box.InputSignalArray[nodeId++] = input; } // Activate the network and get outputs back box.Activate(); box.OutputSignalArray.CopyTo(outputs, 0); // Normalize outputs when using NEAT if (phenotype == Phenotype.Neat) { for (var i = 0; i < outputs.Count(); i++) { outputs[i] = (outputs[i] + 1.0) / 2.0; } } return(outputs); }
/// <summary> /// Run a single cart-pole simulation/trial on the given black box, and with the given initial model state. /// </summary> /// <param name="box">The black box (neural net) to evaluate.</param> /// <param name="cartPos">Cart position on the track.</param> /// <param name="poleAngle1">Pole 1 angle in radians.</param> /// <param name="poleAngle2">Pole 2 angle in radians.</param> /// <returns>Fitness score.</returns> public float RunTrial( IBlackBox <double> box, float cartPos, float poleAngle1, float poleAngle2) { // Reset black box state. box.ResetState(); // Reset model state. _physics.ResetState(cartPos, poleAngle1, poleAngle2); // Get a local variable ref to the internal model state array. float[] state = _physics.State; // Run the cart-pole simulation. int timestep = 0; for (; timestep < _maxTimesteps; timestep++) { // Provide model state to the black box inputs (normalised to +-1.0). box.InputVector[0] = 1.0; // Bias input. box.InputVector[1] = state[0] * __TrackLengthHalf_Reciprocal; // Cart X position range is +-__TrackLengthHalf; here we normalize to [-1,1]. box.InputVector[2] = state[1]; // Cart velocity. Typical range is approx. +-10. box.InputVector[3] = state[2] * __MaxPoleAngle_Reciprocal; // Pole 1 angle. Range is +-__MaxPoleAngle radians; here we normalize to [-1,1]. box.InputVector[4] = state[3] * 0.2; // Pole 1 angular velocity. Typical range is approx. +-5. box.InputVector[5] = state[4] * __MaxPoleAngle_Reciprocal; // Pole 2 angle. box.InputVector[6] = state[5] * 0.2; // Pole 2 angular velocity. // Activate the network. box.Activate(); // Read the output to determine the force to be applied to the cart by the controller. float force = (float)(box.OutputVector[0] - 0.5) * 2f; ClipForce(ref force); force *= __MaxForce; // Update model state, i.e. move the model forward by one timestep. _physics.Update(force); // Check for failure state. I.e. has the cart run off the ends of the track, or has either of the pole // angles exceeded the defined threshold. if (MathF.Abs(state[0]) > __TrackLengthHalf || MathF.Abs(state[2]) > __MaxPoleAngle || MathF.Abs(state[4]) > __MaxPoleAngle) { break; } } // Fitness is given by the combination of two fitness components: // 1) Amount of simulation time that elapsed before the pole angle and/or cart position threshold was exceeded. Max score is 99 if the // end of the trial is reached without exceeding any thresholds. // 2) Cart position component. Max fitness of 1.0 for a cart position of zero (i.e. the cart is in the middle of the track range); // // Therefore the maximum possible fitness is 100.0. float fitness = (timestep * _maxTimesteps_Reciprocal * 99f) + (1f - (MathF.Min(MathF.Abs(state[0]), __TrackLengthHalf) * __TrackLengthHalf_Reciprocal)); return(fitness); }
/// <summary> /// Probe the given black box, and record the responses in <paramref name="responseArr"/> /// </summary> /// <param name="box">The black box to probe.</param> /// <param name="responseArr">Response array.</param> public void Probe(IBlackBox <double> box, double[] responseArr) { Debug.Assert(responseArr.Length == _paramSamplingInfo.SampleResolution); double[] xArr = _paramSamplingInfo.XArrNetwork; for (int i = 0; i < xArr.Length; i++) { // Reset black box internal state. box.ResetState(); // Set bias input, and function input value. box.InputVector[0] = 1.0; box.InputVector[1] = xArr[i]; // Activate the black box. box.Activate(); // Get the black box's output value. // TODO: Review this scheme. This replicates the behaviour in SharpNEAT 2.x but not sure if it's ideal, // for one it depends on the output range of the neural net activation function in use. double output = box.OutputVector[0]; Clip(ref output); responseArr[i] = ((output - 0.5) * _scale) + _offset; } }
public double[] GetChampOutput(int nodeDepth, int nodeSiblingNum, int maxDepth, int maxBranching) { IGenomeDecoder <NeatGenome, IBlackBox> decoder = experiment.CreateGenomeDecoder(); IBlackBox box = decoder.Decode(contentEA.CurrentChampGenome); // Normalize nodeDepth and nodeSiblingNum to range of 0-1. This may affect outputs? double normDepth = (double)nodeDepth / (double)maxDepth; double normSib; if (nodeDepth == 0) { normSib = 0; // Only one node at depth 0, prevent a divide by 0 error } else { normSib = (double)nodeSiblingNum / (double)(Mathf.Pow(maxBranching, nodeDepth) - 1); } box.InputSignalArray[0] = normDepth; box.InputSignalArray[1] = normSib; box.ResetState(); box.Activate(); //Debug.Log("(" + normDepth + "," + normSib + ") -> (" + box.OutputSignalArray[0] + "," + box.OutputSignalArray[1] + "," + box.OutputSignalArray[2] + "," //+ box.OutputSignalArray[3] + "," + box.OutputSignalArray[4] + "," + box.OutputSignalArray[5] + ")"); double[] outputs = new double[box.OutputCount]; for (int i = 0; i < box.OutputCount; i++) { outputs[i] = box.OutputSignalArray[i]; } return(outputs); }
/// <summary> /// Refresh/update the view with the provided genome. /// </summary> public override void RefreshView(object genome) { NeatGenome neatGenome = genome as NeatGenome; if (null == neatGenome) { return; } // Decode genome. IBlackBox box = _genomeDecoder.Decode(neatGenome); // Probe the black box. _blackBoxProbe.Probe(box, _yArrTarget); // Update plot points. double[] xArr = _paramSamplingInfo._xArr; for (int i = 0; i < xArr.Length; i++) { if (!_generativeMode) { box.ResetState(); box.InputSignalArray[0] = xArr[i]; } box.Activate(); _plotPointListResponse[i].Y = _yArrTarget[i]; } // Trigger graph to redraw. zed.AxisChange(); Refresh(); }
private double[] activate(IBlackBox box, IList <double> inputs, double[] outputs) { box.ResetState(); // Give inputs to the network var nodeId = 0; foreach (var input in inputs) { box.InputSignalArray[nodeId++] = input; } // Activate the network and get outputs back box.Activate(); box.OutputSignalArray.CopyTo(outputs, 0); // Normalize outputs when using NEAT if (phenotype == Phenotype.Neat) { NormalizeOutputs(outputs); } else { Debug.Assert(outputs.All(x => x >= 0.0 && x <= 1.0)); } return(outputs); }
public void Step() { phenome.ResetState(); foreach (int typeID in NeatConsts.typeIds) { for (int y = 0; y < NeatConsts.ViewY; y++) { for (int x = 0; x < NeatConsts.ViewX; x++) { var xpos = game.player.position.x + x; var ypos = game.player.position.y + (NeatConsts.ViewY / 2) - y; var index = typeID * (y * NeatConsts.ViewX + x); if (ypos < 0 || xpos < 0 || ypos >= game.map.map.GetLength(0) || xpos >= game.map.map.GetLength(1)) { phenome.InputSignalArray[index] = 0; } else { phenome.InputSignalArray[index] = (game.map.map[ypos, xpos]) == typeID ? 1 : 0; } } } } phenome.Activate(); game.Step(phenome.OutputSignalArray[0] > 0.5); }
public FitnessInfo Test(IBlackBox box) { double fitness, altFitness; //these are our inputs and outputs ISignalArray inputArr = box.InputSignalArray; ISignalArray outputArr = box.OutputSignalArray; List <Sample> sampleList = LEEAParams.DOMAIN.getTestSamples(); int sampleCount = sampleList.Count; fitness = sampleList.Count; foreach (Sample sample in sampleList) { inputArr.CopyFrom(sample.Inputs, 0); box.ResetState(); box.Activate(); fitness -= Math.Abs(outputArr[0] - sample.Outputs[0]) * Math.Abs(outputArr[0] - sample.Outputs[0]); } fitness = Math.Max(fitness, LEEAParams.MINFITNESS) + LEEAParams.FITNESSBASE; return(new FitnessInfo(fitness, 0)); }
/// <summary> /// Refresh/update the view with the provided genome. /// </summary> public override void RefreshView(object genome) { NeatGenome neatGenome = genome as NeatGenome; if (null == neatGenome) { return; } // Decode genome. IBlackBox box = _genomeDecoder.Decode(neatGenome); // Update plot points. double x = _xMin; for (int i = 0; i < _sampleCount; i++, x += _xIncr) { box.ResetState(); box.InputSignalArray[0] = x; box.Activate(); _plotPointListResponse[i].Y = box.OutputSignalArray[0]; } // Trigger graph to redraw. zed.AxisChange(); Refresh(); }
private void RedrawGraph(NeatGenome genome) { IBlackBox box = _experiment.GetBlackBox(genome); Color trueColor = panPlot.TrueColor; Color falseColor = panPlot.FalseColor; var samples = Enumerable.Range(0, 1000). Select(o => { Vector3D pos = Math3D.GetRandomVector(new Vector3D(0, 0, 0), new Vector3D(1, 1, 1)); box.ResetState(); //box.InputSignalArray.CopyFrom() box.InputSignalArray[0] = pos.X; box.InputSignalArray[1] = pos.Y; box.InputSignalArray[2] = pos.Z; box.Activate(); double percent = box.OutputSignalArray[0]; Color color = UtilityWPF.AlphaBlend(trueColor, falseColor, percent); return(Tuple.Create(pos.ToPoint(), color)); }); panPlot.ClearFrame(); panPlot.AddDots(samples, .01, false); }
public void Activate(IBlackBox brain, GameObject target) { this.target = target; this.brain = brain; brain.ResetState(); this.startPos = transform.position; shortestDistance = int.MaxValue; isRunning = true; }
public void test() { PokemonExperiment test = new PokemonExperiment(pD); config.Load("configfiles/NEATConfig.config.xml"); test.Initialize("Pokemon", config.DocumentElement); var genomeDecoder = test.CreateGenomeDecoder(); IBlackBox box = genomeDecoder.Decode(bestGenome); BattleHandler b = new BattleHandler(); int score = 0; List <PokemonTeam> evalTeams = pD.getEvalTeams(); foreach (PokemonTeam t in pD.getEvalTeams()) { string debug = "\nBattling against a team with: "; foreach (Pokemon p in t.getMembers()) { debug += p.getName() + " "; } Console.Write(debug); box.ResetState(); ISignalArray inputArray; inputArray = box.InputSignalArray; float[] pkmnDnaIn = pD.createDnaUsingPkmn(t); for (int j = 0; j < t.getMembers().Length; j++) { inputArray[j] = pkmnDnaIn[j]; } box.Activate(); ISignalArray outputArray; outputArray = box.OutputSignalArray; float[] pkmnDnaOut = new float[outputArray.Length]; for (int j = 0; j < pkmnDnaOut.Length; j++) { pkmnDnaOut[j] = (float)outputArray[j]; } PokemonTeam attacker = pD.createTeamUsingDNA(pkmnDnaOut); score += b.battle(attacker.getMembers(), t.getMembers()); } Console.Write("\nA Score of " + score + " was achieved agains the evaluation teams! (NEAT)"); Console.Write("\nThe NEAT has a complexity of " + bestGenome.Complexity); }
public void PrintStats() { // MUST BE CALLED AFTER RunEA NeatAlgorithmStats stats = contentEA.Statistics; Debug.Log("************* NEAT STATS **************"); Debug.Log("Generation = " + stats._generation + ", Total Evaluation = " + stats._totalEvaluationCount + ", Max Fitness = " + stats._maxFitness + ", MeanFitness = " + stats._meanFitness); IGenomeDecoder <NeatGenome, IBlackBox> decoder = experiment.CreateGenomeDecoder(); IBlackBox box = decoder.Decode(contentEA.CurrentChampGenome); FastAcyclicNetwork concrete = (FastAcyclicNetwork)box; Debug.Log("Champ:Num hidden nodes = " + concrete.hiddenNodeCount + ", num connections = " + concrete.connectionCount); // Get highlevel features PCGSharpHighLevelFeatures featureCounts = new PCGSharpHighLevelFeatures(); PCGSharpHighLevelFeatures.C_HL_ROOMTYPE lastRoomType = PCGSharpHighLevelFeatures.C_HL_ROOMTYPE.NoPreviousRoom; for (int i = 0; i < (experiment as PCGNeatExperiment).geomNodeList.Count; i++) { // Get cppn output for each node box.InputSignalArray[0] = (experiment as PCGNeatExperiment).geomNodeList[i].normDepth; box.InputSignalArray[1] = (experiment as PCGNeatExperiment).geomNodeList[i].normSiblingIndex; box.ResetState(); box.Activate(); string outputString = box.OutputSignalArray[0].ToString(); double[] outputs = new double[box.OutputCount]; outputs[0] = box.OutputSignalArray[0]; for (int j = 1; j < box.OutputCount; j++) { outputString = outputString + "," + box.OutputSignalArray[j]; outputs[j] = box.OutputSignalArray[j]; } Debug.Log("(" + (experiment as PCGNeatExperiment).geomNodeList[i].normDepth + "," + (experiment as PCGNeatExperiment).geomNodeList[i].normSiblingIndex + ") -> (" + outputString + ")"); // Convert each nodes cppn output into a contentId int combinedContentID = featureCounts.CPPNOutputToSettingsId(outputs); lastRoomType = featureCounts.UpdateFeatures(combinedContentID, lastRoomType); } // Pass the highlevel features into the player model to get the fitness if ((experiment as PCGNeatExperiment).playerModel != null) { double[] distributions = (experiment as PCGNeatExperiment).playerModel.ClassifyNewData(featureCounts.ToDoubleArray()); Debug.Log("Classifier: Champ Distributions: Dislike = " + distributions[0] + ", Like = " + distributions[1]); (experiment as PCGNeatExperiment).playerModel.PrintClassifierTestReport(); } Debug.Log("**************END NEAT STATS**************"); }
void FixedUpdate() { _blackBox.ResetState(); float[] rangeFinders = GetRangeFinders(); for (int i = 0; i < 6; i++) { _blackBox.InputSignalArray[i] = rangeFinders[i]; } float[] slices = GetSlices(); for (int i = 0; i < 4; i++) { _blackBox.InputSignalArray[i + 6] = slices[i]; } _blackBox.Activate(); float speedOutput = (float)(_blackBox.OutputSignalArray[0] / 2); float rotationOutput = (float)(_blackBox.OutputSignalArray[1] - 0.5); if (ManualInput) { speedOutput = 0; rotationOutput = 0; if (Input.GetKey(KeyCode.W)) { speedOutput += 0.5f; } if (Input.GetKey(KeyCode.S)) { speedOutput -= 0.5f; } if (Input.GetKey(KeyCode.A)) { rotationOutput += 0.5f; } if (Input.GetKey(KeyCode.D)) { rotationOutput -= 0.5f; } } Rigidbody2D body = GetComponent <Rigidbody2D>(); float rotation = body.rotation * Mathf.Deg2Rad; Vector2 direction = new Vector2(-Mathf.Sin(rotation), Mathf.Cos(rotation)) * Speed * speedOutput; float rotationSpeed = RotationSpeed * rotationOutput; body.position += direction; body.rotation += rotationSpeed; }
/// <summary> /// Run one simulation. /// </summary> private void RunTrial() { // Ensure flag is reset before we enter the main trial loop. _simStopFlag = false; // Get local copy of black box pointer so that the same one is used throughout each individual simulation trial/run // (_box is being continually updated by the evolution algorithm update events). This is probably an atomic // operation and thus thread safe. IBlackBox box = _box; box.ResetState(); // Create/init new simulation world. _simWorld = CreateSimulationWorld(); _simWorld.SetDebugDraw(_debugDraw); _timestepDelayMs = (int)(1000f / (float)_simWorld.SimulationParameters._frameRate); // Initialise the viewport on the GUI thread. Invoke(new MethodInvoker(delegate() { SetView(); })); // Run main simulation loop until a stop condition occurs. for (;;) { // Perform GUI operations on the GUI thread. Invoke(new MethodInvoker(delegate() { // Clear viewport buffer. Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT); // Advance the simulation world forward by one timestep. // The simulation world has callbacks to the debug drawer object, // hence this call is made on the GUI thread. _simWorld.Step(); // Draw the world in its new/current state. openGlControl.Draw(); })); // Invoke controller logic (if any). InvokeController(); if (TestStopCondition()) { break; } // Slow simulation down to run it in real-time. Thread.Sleep(_timestepDelayMs); } }
/// <summary> /// Refresh/update the view with the provided genome. /// </summary> public override void RefreshView(object genome) { NeatGenome neatGenome = genome as NeatGenome; if (null == neatGenome) { return; } // Decode genome. IBlackBox box = _genomeDecoder.Decode(neatGenome); box.ResetState(); // Probe the black box. //_blackBoxProbe.Probe(box, _yArrTarget); // Update plot points. double[] xArr = new double[_Data.GetLength(0)]; for (int i = 0; i < xArr.Length; i++) { box.InputSignalArray[1] = _Data[i, 1]; if (i == 0) { box.InputSignalArray[0] = _Data[i, 0]; box.InputSignalArray[2] = _Data[i, 2]; } else { box.InputSignalArray[0] = _Data[i, 0] - _Data[i - 1, 0]; box.InputSignalArray[2] = box.OutputSignalArray[0]; } box.Activate(); //_plotPointListResponse[i].Y = _yArrTarget[i]; _plotPointListResponse[i].Y = box.OutputSignalArray[0]; _plotPointListError[i].Y = _plotPointListResponse[i].Y - _yArrTarget[i]; if (System.Math.Abs(_plotPointListError[i].Y) < tol) { _plotPointListError[i].Y = 0; } if (i != 0) { _plotPointErrorSum[i].Y = Math.Abs(_plotPointListError[i].Y) + _plotPointErrorSum[i - 1].Y; } } _splotPointErrorSum = _plotPointErrorSum; _splotPointListResponse = _plotPointListResponse; _splotPointListError = _plotPointListError; // Trigger graph to redraw. zed.AxisChange(); Refresh(); }
public bool WantsToJump(ScreenInputSignal screenInputSignal) { // Clear the network _brain.ResetState(); // Convert the game board into an input array for the network SetInputSignalArray(_brain.InputSignalArray, screenInputSignal); // Activate the network _brain.Activate(); return(_brain.OutputSignalArray[0] == 1); }
internal static void ProcessInputProduceOutput(IBlackBox phenome, double[] input, out bool output) { phenome.ResetState(); phenome.InputSignalArray.CopyFrom(input, 0); phenome.Activate(); var outVal = phenome.OutputSignalArray[0]; var jumpValue = TanH.__DefaultInstance.Calculate(outVal, new double[0]); output = jumpValue > JumpTreshold; }
/// <summary> /// Evaluate the provided black box against the logical XOR task, /// and return its fitness score. /// </summary> /// <param name="box">The black box to evaluate.</param> public FitnessInfo Evaluate(IBlackBox <double> box) { double fitness = 0.0; bool success = true; // Test case 0, 0. double output = Activate(box, 0.0, 0.0); success &= output <= 0.5; fitness += 1.0 - (output * output); // Test case 1, 1. box.ResetState(); output = Activate(box, 1.0, 1.0); success &= output <= 0.5; fitness += 1.0 - (output * output); // Test case 0, 1. box.ResetState(); output = Activate(box, 0.0, 1.0); success &= output > 0.5; fitness += 1.0 - ((1.0 - output) * (1.0 - output)); // Test case 1, 0. box.ResetState(); output = Activate(box, 1.0, 0.0); success &= output > 0.5; fitness += 1.0 - ((1.0 - output) * (1.0 - output)); // If all four responses were correct then we add 10 to the fitness. if (success) { fitness += 10.0; } return(new FitnessInfo(fitness)); }
/// <summary> /// Based on game input will trigger the player to make a move. /// </summary> /// <param name="inputs"></param> public void MakeMove(int[] inputs) { // Clear the network _brain.ResetState(); // Convert the game state into an input array for the network for (int i = 0; i < _brain.InputCount; i++) { _brain.InputSignalArray[i] = inputs[i]; } // Activate the network _brain.Activate(); // Release all previous button presses ReleaseAllKeys(); // Execute button presses! if (EvaluateOutputNode(_brain.OutputSignalArray[0])) { _controller.ManualPressKey(7); } if (EvaluateOutputNode(_brain.OutputSignalArray[1])) { _controller.ManualPressKey(6); } // Up+Down And Left+Right are not valid button combos. // So only press the one with the highest value. if (_brain.OutputSignalArray[2] > _brain.OutputSignalArray[3]) { _controller.ManualPressKey(3); } else { _controller.ManualPressKey(2); } if (_brain.OutputSignalArray[4] > _brain.OutputSignalArray[5]) { _controller.ManualPressKey(1); } else { _controller.ManualPressKey(0); } }
private static void ComplexCyclic_Inner( IBlackBox <double> net, IActivationFunction <double> actFn) { // Simulate network in C# and compare calculated outputs with actual network outputs. double[] preArr = new double[3]; double[] postArr = new double[3]; postArr[0] = 3.0; net.InputVector[0] = 3.0; for (int i = 0; i < 10; i++) { preArr[1] = postArr[0] * -2.0 + postArr[2]; preArr[2] = postArr[0] + postArr[1]; postArr[1] = actFn.Fn(preArr[1]); postArr[2] = actFn.Fn(preArr[2]); net.Activate(); Assert.Equal(postArr[1], net.OutputVector[0]); } // Rest the network's internal state. net.ResetState(); Assert.Equal(0.0, net.OutputVector[0]); // Run the test again. Array.Clear(preArr, 0, preArr.Length); Array.Clear(postArr, 0, postArr.Length); postArr[0] = 3.0; net.InputVector[0] = 3.0; for (int i = 0; i < 10; i++) { preArr[1] = postArr[0] * -2.0 + postArr[2]; preArr[2] = postArr[0] + postArr[1]; postArr[1] = actFn.Fn(preArr[1]); postArr[2] = actFn.Fn(preArr[2]); net.Activate(); Assert.Equal(postArr[1], net.OutputVector[0]); } }
public void Move(Game game) { MoveDTO move; // Clear the network _brain.ResetState(); // Convert the game board into an input array for the network setInputSignalArray(_brain.InputSignalArray, game.Board); // Activate the network _brain.Activate(); move = GenerateMove(game); game.Move(move); }
public void Probe(IBlackBox box, double[] responseArr) { Debug.Assert(responseArr.Length == _paramSamplingInfo._sampleCount); // Reset black box internal state. box.ResetState(); double[] xArr = _paramSamplingInfo._xArrNetwork; for (int i = 0; i < xArr.Length; i++) { // Activate the black box. box.Activate(); // Get the black box's output value. responseArr[i] = ((box.OutputSignalArray[0] - 0.5) * _scale) + _offset; } }
private void QueryBotAction() { brain.ResetState(); brain.InputSignalArray[0] = NormalizeSensorData("forward"); brain.InputSignalArray[1] = NormalizeSensorData("left"); brain.InputSignalArray[2] = NormalizeSensorData("right"); brain.Activate(); var moveOutput = (float)brain.OutputSignalArray[0]; var turnOutput = (float)brain.OutputSignalArray[1]; var turnDirOutput = (float)brain.OutputSignalArray[2]; currentMoveSpeed = GetActionOutput(moveOutput, moveSpeed, 0, moveThreshold); isTurning = GetActionOutput(turnOutput, true, false, turnThreshold); currentTurnSpeed = GetActionOutput(turnDirOutput, turnSpeed, -turnSpeed, turnRightThreshold); }
public double[] ActivateNeuralNetwork(double[] environmentOutput) { // Activate the neural network Phenome.ResetState(); Phenome.InputSignalArray.CopyFrom(environmentOutput, 0); Phenome.Activate(); if (!Phenome.IsStateValid) { Console.WriteLine("Invalid state"); } double[] nnOutput = new double[Phenome.OutputSignalArray.Length]; Phenome.OutputSignalArray.CopyTo(nnOutput, 0); return(nnOutput); }
static void SaveValidation(IBlackBox box) { #region Validation int n = ControlSignal.GetLength(0); double Dt = 0; double desiredPos; double SatV = 9; box.ResetState(); double actualPos = 0; StreamWriter ValWriter = new StreamWriter(ValidationFolder + NameFile + "_" + MaxTMin + "m" + "_val.csv"); for (int j = 0; j < n; j++) { if (j != 0) { Dt = ControlSignal[j, 0] - ControlSignal[j - 1, 0]; } desiredPos = ControlSignal[j, 1]; double ctrlV = desiredPos - actualPos; if (Math.Abs(ctrlV) > SatV) { ctrlV = SatV * Math.Sign(ctrlV); } box.InputSignalArray[0] = Dt; box.InputSignalArray[1] = ctrlV; box.InputSignalArray[2] = actualPos; box.Activate(); actualPos = box.OutputSignalArray[0]; ValWriter.Flush(); ValWriter.WriteLine("{0},{1}", ControlSignal[j, 0], actualPos); } ValWriter.Close(); #endregion }
/// <summary> /// Probe the given black box, and record the responses in <paramref name="responseArr"/> /// </summary> /// <param name="box">The black box to probe.</param> /// <param name="responseArr">Response array.</param> public void Probe(IBlackBox <double> box, double[] responseArr) { Debug.Assert(responseArr.Length == _sampleCount); // Reset black box internal state. box.ResetState(); // Take the require number of samples. for (int i = 0; i < _sampleCount; i++) { // Set bias input. box.InputVector[0] = 1.0; // Activate the black box. box.Activate(); // Get the black box's output value. // TODO: Review. Note this scheme is different to the one in SharpNEAT 2.x responseArr[i] = (box.OutputVector[0] + _offset) * _scale; } }
/// <summary> /// Probe the given black box, and record the responses in <paramref name="responseArr"/> /// </summary> /// <param name="box">The black box to probe.</param> /// <param name="responseArr">Response array.</param> public void Probe(IBlackBox <double> box, double[] responseArr) { Debug.Assert(responseArr.Length == _paramSamplingInfo.SampleCount); double[] xArr = _paramSamplingInfo.XArrNetwork; for (int i = 0; i < xArr.Length; i++) { // Reset black box internal state. box.ResetState(); // Set bias input, and function input value. box.InputVector[0] = 1.0; box.InputVector[1] = xArr[i]; // Activate the black box. box.Activate(); // Get the black box's output value. // TODO: Review. Note this scheme is different to the one in SharpNEAT 2.x responseArr[i] = (box.OutputVector[0] + _offset) * _scale; } }
static void SavePerformance(IBlackBox box) { #region Performance double tol = 0.001; Random r = new Random(); double[][,] Datas = IDMotorUtils.GetData; double[,] _Data = Datas[r.Next(Datas.GetLength(0))]; box.ResetState(); double[] xArr = new double[_Data.GetLength(0)]; StreamWriter prfWriter = new StreamWriter(PerformanceFolder + NameFile + "_" + MaxTMin + "m" + "_prf.csv"); for (int j = 0; j < xArr.Length; j++) { box.InputSignalArray[1] = _Data[j, 1]; if (j == 0) { box.InputSignalArray[0] = _Data[j, 0]; box.InputSignalArray[2] = _Data[j, 2]; } else { box.InputSignalArray[0] = _Data[j, 0] - _Data[j - 1, 0]; box.InputSignalArray[2] = box.OutputSignalArray[0]; } box.Activate(); //_plotPointListResponse[i].Y = _yArrTarget[i]; double output = box.OutputSignalArray[0]; double error = box.OutputSignalArray[0] - _Data[j, 2]; if (System.Math.Abs(error) < tol) { error = 0; } prfWriter.WriteLine("{0},{1},{2},{3}", _Data[j, 0], _Data[j, 2], output, error); } prfWriter.Close(); prfWriter.Dispose(); #endregion }
private double[] activate(IBlackBox box, IList<double> inputs, double[] outputs) { box.ResetState(); // Give inputs to the network var nodeId = 0; foreach (var input in inputs) { box.InputSignalArray[nodeId++] = input; } // Activate the network and get outputs back box.Activate(); box.OutputSignalArray.CopyTo(outputs, 0); // Normalize outputs when using NEAT if (phenotype == Phenotype.Neat) { NormalizeOutputs(outputs); } else { Debug.Assert(outputs.All(x => x >= 0.0 && x <= 1.0)); } return outputs; }
private void PaintView(IBlackBox box) { if(_initializing) { return; } Graphics g = Graphics.FromImage(_image); g.FillRectangle(_brushBackground, 0, 0, _image.Width, _image.Height); g.SmoothingMode = SmoothingMode.AntiAlias; // Get control width and height. int width = Width; int height = Height; // Determine smallest dimension. Use that as the edge length of the square grid. width = height = Math.Min(width, height); // Pixel size is calculated using integer division to produce cleaner lines when drawing. // The inherent rounding down may produce a grid 1 pixel smaller then the determined edge length. // Also make room for a combo box above the grid. This will be used allow the user to change the grid resolution. int visualFieldPixelSize = (height-GridTop) / _visualFieldResolution; width = height = visualFieldPixelSize * _visualFieldResolution; // Paint pixel outline grid. // Vertical lines. int x = GridLeft; for(int i=0; i<=_visualFieldResolution; i++, x+=visualFieldPixelSize) { g.DrawLine(__penGrey, x, GridTop, x, GridTop+height); } // Horizontal lines. int y = GridTop; for(int i=0; i<=_visualFieldResolution; i++, y+=visualFieldPixelSize) { g.DrawLine(__penGrey, GridLeft, y, GridLeft+width, y); } // Apply test case to black box's visual field sensor inputs. BoxesVisualDiscriminationEvaluator.ApplyVisualFieldToBlackBox(_testCaseField, box, _visualFieldResolution, _visualOriginPixelXY, _visualPixelSize); // Activate the black box. box.ResetState(); box.Activate(); // Read output responses and paint them to the pixel grid. // Also, we determine the range of values so that we can normalize the range of pixel shades used. double low, high; low = high = box.OutputSignalArray[0]; for(int i=1; i<box.OutputSignalArray.Length; i++) { double val = box.OutputSignalArray[i]; if(val > high) { high = val; } else if(val <low) { low = val; } } double colorScaleFactor; if(0.0 == (high-low)) { colorScaleFactor = 1.0; } else { colorScaleFactor = 1.0 / (high-low); } IntPoint maxActivationPoint = new IntPoint(0,0); double maxActivation = -1.0; int outputIdx = 0; y = GridTop; for(int i=0; i<_visualFieldResolution; i++, y+=visualFieldPixelSize) { x = GridLeft; for(int j=0; j<_visualFieldResolution; j++, x+=visualFieldPixelSize, outputIdx++) { double output = box.OutputSignalArray[outputIdx]; if(output > maxActivation) { maxActivation = output; maxActivationPoint._x = j; maxActivationPoint._y = i; } Color color = GetResponseColor((output-low)*colorScaleFactor); Brush brush = new SolidBrush(color); g.FillRectangle(brush, x+1, y+1, visualFieldPixelSize-2, visualFieldPixelSize-2); } } // Paint lines around the small and large test case boxes to highlight them. // Small box. int testFieldPixelSize = (_visualFieldResolution / TestCaseField.TestFieldResolution) * visualFieldPixelSize; g.DrawRectangle(__penBoxOutline, GridLeft + (_testCaseField.SmallBoxTopLeft._x * testFieldPixelSize), GridTop + (_testCaseField.SmallBoxTopLeft._y * testFieldPixelSize), testFieldPixelSize, testFieldPixelSize); // Large box. g.DrawRectangle(__penBoxOutline, GridLeft + (_testCaseField.LargeBoxTopLeft._x * testFieldPixelSize), GridTop + (_testCaseField.LargeBoxTopLeft._y * testFieldPixelSize), testFieldPixelSize*3, testFieldPixelSize*3); // Draw red line around pixel with highest activation. g.DrawRectangle(__penSelectedPixel, GridLeft + (maxActivationPoint._x * visualFieldPixelSize), GridTop + (maxActivationPoint._y * visualFieldPixelSize), visualFieldPixelSize, visualFieldPixelSize); Refresh(); }
/// <summary> /// Create a network definition by querying the provided IBlackBox (typically a CPPN) with the /// substrate connection endpoints. /// </summary> /// <param name="blackbox">The HyperNEAT CPPN that defines the strength of connections between nodes on the substrate.</param> /// <param name="lengthCppnInput">Optionally we provide a connection length input to the CPPN.</param> public INetworkDefinition CreateNetworkDefinition(IBlackBox blackbox, bool lengthCppnInput) { // Get the sequence of substrate connections. Either a pre-built list or a dynamically // generated sequence. IEnumerable<SubstrateConnection> connectionSequence = _connectionList ?? GetConnectionSequence(); // Iterate over substrate connections. Determine each connection's weight and create a list // of network definition connections. ISignalArray inputSignalArr = blackbox.InputSignalArray; ISignalArray outputSignalArr = blackbox.OutputSignalArray; ConnectionList networkConnList = new ConnectionList(_connectionCountHint); int lengthInputIdx = inputSignalArr.Length - 1; foreach(SubstrateConnection substrateConn in connectionSequence) { int layerToLayerAdder = ((int)substrateConn._tgtNode._position[2] * 3); // Assign the connection's endpoint position coords to the CPPN/blackbox inputs. Note that position dimensionality is not fixed. for(int i=0; i<_dimensionality - 1; i++) { inputSignalArr[i] = substrateConn._srcNode._position[i]; inputSignalArr[i + _dimensionality - 1] = substrateConn._tgtNode._position[i]; inputSignalArr[i + 2 * (_dimensionality - 1)] = Math.Abs(substrateConn._srcNode._position[i] - substrateConn._tgtNode._position[i]); } // Optional connection length input. if(lengthCppnInput) { inputSignalArr[lengthInputIdx] = CalculateConnectionLength(substrateConn._srcNode._position, substrateConn._tgtNode._position); } // Reset blackbox state and activate it. blackbox.ResetState(); blackbox.Activate(); // Read connection weight from output 0. double weight = outputSignalArr[0 + layerToLayerAdder]; // Skip connections with a weight magnitude less than _weightThreshold. double weightAbs = Math.Abs(weight); if (outputSignalArr[2 + layerToLayerAdder] >= 0) { // For weights over the threshold we re-scale into the range [-_maxWeight,_maxWeight], // assuming IBlackBox outputs are in the range [-1,1]. weight = (weightAbs) * _weightRescalingCoeff * Math.Sign(weight); // Create network definition connection and add to list. networkConnList.Add(new NetworkConnection(substrateConn._srcNode._id, substrateConn._tgtNode._id, weight)); } } // Additionally we create connections from each hidden and output node to a bias node that is not defined at any // position on the substrate. The motivation here is that a each node's input bias is independent of any source // node (and associated source node position on the substrate). That we refer to a bias 'node' is a consequence of how input // biases are handled in NEAT - with a specific bias node that other nodes can be connected to. int setCount = _nodeSetList.Count; for(int i=1; i<setCount; i++) { SubstrateNodeSet nodeSet = _nodeSetList[i]; foreach(SubstrateNode node in nodeSet.NodeList) { // Assign the node's position coords to the blackbox inputs. The CPPN inputs for source node coords are set to zero when obtaining bias values. for(int j=0; j<_dimensionality - 1; j++) { inputSignalArr[j] = 0.0; inputSignalArr[j + _dimensionality - 1] = node._position[j]; } // Optional connection length input. if(lengthCppnInput) { inputSignalArr[lengthInputIdx] = CalculateConnectionLength(node._position); } // Reset blackbox state and activate it. blackbox.ResetState(); blackbox.Activate(); // Read bias connection weight from output 1. double weight = outputSignalArr[1+ (i- 1) * 3]; // Skip connections with a weight magnitude less than _weightThreshold. double weightAbs = Math.Abs(weight); if(weightAbs > _weightThreshold) { // For weights over the threshold we re-scale into the range [-_maxWeight,_maxWeight], // assuming IBlackBox outputs are in the range [-1,1]. weight = (weightAbs - _weightThreshold) * _weightRescalingCoeff * Math.Sign(weight); // Create network definition connection and add to list. Bias node is always ID 0. networkConnList.Add(new NetworkConnection(0, node._id, weight)); } } } // Check for no connections. // If no connections were generated then there is no point in further evaulating the network. // However, null is a valid response when decoding genomes to phenomes, therefore we do that here. if(networkConnList.Count == 0) { return null; } // Construct and return a network definition. NetworkDefinition networkDef = new NetworkDefinition(_inputNodeCount, _outputNodeCount, _activationFnLibrary, _netNodeList, networkConnList); // Check that the definition is valid and return it. Debug.Assert(networkDef.PerformIntegrityCheck()); return networkDef; }
private double[] activate(IBlackBox box, IEnumerable<double> inputs, double[] outputs) { box.ResetState(); // Give inputs to the network var nodeId = 0; foreach (var input in inputs) { box.InputSignalArray[nodeId++] = input; } // Activate the network and get outputs back box.Activate(); box.OutputSignalArray.CopyTo(outputs, 0); // Normalize outputs when necessary if (phenotype == Phenotype.Neat) { for (var i = 0; i < outputs.Count(); i++) { outputs[i] = (outputs[i] + 1.0) / 2.0; } } else { Debug.Assert(outputs.All(x => x >= 0.0 && x <= 1.0)); } return outputs; }
/// <summary> /// Runs one trial of the provided agent in the world. Returns true if the agent captures the prey within /// the maximum number of timesteps allowed. /// </summary> public bool RunTrial(IBlackBox agent) { // Init world state. InitPositions(); // Clear any prior agent state. agent.ResetState(); // Prey gets a head start (simulate world as normal but agent is frozen). int t = 0; for(; t<_preyInitMoves; t++) { SetAgentInputsAndActivate(agent); MovePrey(); } // Let the chase begin! for(; t<_maxTimesteps; t++) { SetAgentInputsAndActivate(agent); MoveAgent(agent); if(IsPreyCaptured()) { return true; } MovePrey(); if(IsPreyCaptured()) { // The prey walked directly into the agent. return true; } } // Agent failed to capture prey in the alloted time. return false; }