Skip to content

Commit

Permalink
Old code for a 3D 4-legged walker powered by AI (currently unused)
Browse files Browse the repository at this point in the history
  • Loading branch information
MuleaneEve committed Nov 7, 2018
1 parent 149c3fe commit 4c0acb9
Show file tree
Hide file tree
Showing 118 changed files with 14,298 additions and 0 deletions.
120 changes: 120 additions & 0 deletions SharpNeatWalker/OscillatorQuadruped/CTRNN/CTRNNExperiment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpNeatLib.Experiments;
using SharpNeatLib.Evolution;
using SharpNeatLib.NeuralNetwork;

namespace OscillatorQuadruped
{
class CTRNNExperiment : IExperiment
{
private uint inputs;
private uint outputs;
private uint hidden;
private int cppnInputs;
private int cppnOutputs;
private IPopulationEvaluator populationEvaluator = null;
private NeatParameters neatParams = null;

public CTRNNExperiment(uint inputs, uint outputs, uint hidden, int cppnInputs, int cppnOutputs)
{
this.inputs = inputs;
this.outputs = outputs;
this.hidden = hidden;
this.cppnInputs = cppnInputs;
this.cppnOutputs = cppnOutputs;
}

#region IExperiment Members

public void LoadExperimentParameters(System.Collections.Hashtable parameterTable)
{
//throw new Exception("The method or operation is not implemented.");
}

public IPopulationEvaluator PopulationEvaluator
{
get
{
if (populationEvaluator == null)
ResetEvaluator(HyperNEATParameters.substrateActivationFunction);

return populationEvaluator;
}
}

public void ResetEvaluator(IActivationFunction activationFn)
{
populationEvaluator = new CTRNNPopulationEvaluator(new CTRNNNetworkEvaluator(inputs, outputs, hidden));
}

public int InputNeuronCount
{
get { return cppnInputs; }
}

public int OutputNeuronCount
{
get { return cppnOutputs; }
}

public NeatParameters DefaultNeatParameters
{
get
{
if (neatParams == null)
{
NeatParameters np = new NeatParameters();
np.activationProbabilities = new double[4];
np.activationProbabilities[0] = .25;
np.activationProbabilities[1] = .25;
np.activationProbabilities[2] = .25;
np.activationProbabilities[3] = .25;
np.compatibilityDisjointCoeff = 1;
np.compatibilityExcessCoeff = 1;
np.compatibilityThreshold = 100;
np.compatibilityWeightDeltaCoeff = 3;
np.connectionWeightRange = 3;
np.elitismProportion = .1;
np.pInitialPopulationInterconnections = 1;
np.pInterspeciesMating = 0.01;
np.pMutateAddConnection = .06;
np.pMutateAddNode = .01;
np.pMutateConnectionWeights = .96;
np.pMutateDeleteConnection = 0;
np.pMutateDeleteSimpleNeuron = 0;
np.populationSize = 300;
np.pruningPhaseBeginComplexityThreshold = float.MaxValue;
np.pruningPhaseBeginFitnessStagnationThreshold = int.MaxValue;
np.pruningPhaseEndComplexityStagnationThreshold = int.MinValue;
np.selectionProportion = .8;
np.speciesDropoffAge = 1500;
np.targetSpeciesCountMax = np.populationSize / 10;
np.targetSpeciesCountMin = np.populationSize / 10 - 2;

neatParams = np;
}
return neatParams;
}
}

public IActivationFunction SuggestedActivationFunction
{
get { return HyperNEATParameters.substrateActivationFunction; }
}

public AbstractExperimentView CreateExperimentView()
{
return null;
}

public string ExplanatoryText
{
get { return "A HyperNEAT experiemnt quadruped locomotion"; }
}

#endregion
}
}
48 changes: 48 additions & 0 deletions SharpNeatWalker/OscillatorQuadruped/CTRNN/CTRNNNetworkEvaluator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using SharpNeatLib.Experiments;
using SharpNeatLib.NeuralNetwork;

namespace OscillatorQuadruped
{
internal class CTRNNNetworkEvaluator : INetworkEvaluator
{
public static CTRNNSubstrate substrate;
private NoveltyArchive noveltyArchive;

public CTRNNNetworkEvaluator(uint inputs, uint outputs, uint hidden)
{
substrate = new CTRNNSubstrate(inputs, outputs, hidden, HyperNEATParameters.substrateActivationFunction);
noveltyArchive = new NoveltyArchive();
}

#region INetworkEvaluator Members

public double[] threadSafeEvaluateNetwork(INetwork network)
{
var tempGenome = substrate.generateGenome(network);
var tempNet = tempGenome.Decode(null);

using (var quadDomain = new Domain(noveltyArchive, MainProgram.novelty))
{
var fitness = quadDomain.EvaluateController(new Controller(tempNet));
return fitness;
}
}

public void endOfGeneration()
{
noveltyArchive.endOfGeneration();
}

public double EvaluateNetwork(INetwork network)
{
return 1;
}

public string EvaluatorStateMessage
{
get { return ""; }
}

#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpNeatLib.Experiments;

namespace OscillatorQuadruped
{
class CTRNNPopulationEvaluator : MultiThreadedPopulationEvaluator
{

public CTRNNPopulationEvaluator(INetworkEvaluator eval)
: base(eval, null)
{

}
}
}
Loading

0 comments on commit 4c0acb9

Please sign in to comment.