private void ActivationFunctionMutation() { string debugMsg = "ActivationFunctionMutation"; int nodeRoll = Random.Range(0, Nodes.Count); NodeGene ng = Nodes[nodeRoll]; if (ng == null) { throw new System.Exception("Node not found! " + nodeRoll); } FTYPE fTYPERoll = ActivationFunctions.RandomFTYPE(); if (fTYPERoll != ng.fTYPE) { debugMsg += " - Changing node " + nodeRoll + " from " + ng.GetFTYPE() + " to " + fTYPERoll; ng.SetFTYPE(fTYPERoll); } else { debugMsg += " - Not changing node. Random node selection was same as previous function type"; } if (ArtGallery.DEBUG_LEVEL > ArtGallery.DEBUG.NONE) { Debug.Log(debugMsg); } }
/// <summary> /// Create a new random TWEANN /// </summary> /// <param name="numInputs">Number of input sensors</param> /// <param name="numOutputs">Number of policy neurons per node</param> /// <param name="featureSelective">If true, nerons are only sparsely connected, fully connected otherwise</param> /// <param name="fType">Type of activation function on neurons</param> /// <param name="archetypeIndex">Archtype to align with for crossover</param> public TWEANN(int numInputs, int numOutputs, bool featureSelective, FTYPE fType, int archetypeIndex) { this.numInputs = numInputs; this.numOutputs = numOutputs; ArchetypeIndex = archetypeIndex; nodes = new List <TWEANNNode>(numInputs + numOutputs); long innovation = -1; for (int i = 0; i < numInputs; i++) { TWEANNNode n = new TWEANNNode(fType, NTYPE.INPUT, innovation--); nodes.Add(n); } long linkInnovationBound = innovation - 1; for (int j = 0; j < numOutputs; j++) { nodes.Add(new TWEANNNode(fType, NTYPE.OUTPUT, innovation--)); int[] inputSources = new int[numInputs]; // HACK making this be fully connected for now for (int i = 0; i < numInputs; i++) { inputSources[i] = i; } for (int k = 0; k < inputSources.Length; k++) { nodes[inputSources[k]].Connect(nodes[numInputs + j], RandomGenerator.NextGaussian(), linkInnovationBound - (j * numInputs) - inputSources[k], false, false); } } int outputStart = nodes.Count - numOutputs; }
public static string ActivationName(FTYPE fType) { string result = ""; result = activationFunctions[fType].Name(); return(result); }
public void DeactivateFunction(FTYPE fTYPE) { if (activeFunctions.Contains(fTYPE)) { activeFunctions.Remove(fTYPE); } }
/// <summary> /// New node with no target. Frozen by default /// </summary> /// <param name="fType">Activation function</param> /// <param name="nType">Node type</param> /// <param name="innovation">Uniqu innovation number for node</param> /// <param name="frozen">True if new link mutations cannot target this node</param> /// <param name="bias">Bias offset added to neuron sum before activation. Primarily needed by substrate networks from CPPNs</param> public TWEANNNode(FTYPE fType, NTYPE nType, long innovation, bool frozen, float bias) { this.fType = fType; this.nType = nType; this.innovation = innovation; this.frozen = frozen; this.bias = bias; outputs = new List <TWEANNLink>(); Flush(); }
public static bool DeactivateFunction(FTYPE fType) { bool successful = false; if (activeFunctions.Contains(fType)) { inactiveFunctions.Add(fType); activeFunctions.Remove(fType); successful = true; } return(successful); }
public void ActivateFunction(FTYPE fTYPE) { if (activeFunctions == null) { activeFunctions = new List <FTYPE>(); } if (!activeFunctions.Contains(fTYPE)) { activeFunctions.Add(fTYPE); } ActivationFunctions.ActivateFunction(activeFunctions); }
private void SpawnPickups() { FTYPE fTYPE = ag.GetRandomCollectedFunction(); if ((UnityEngine.Random.Range(0f, 1f) < ag.functionSpawnRate && !ag.FunctionIsActive(fTYPE)) || ag.ActivateFunctionsEmpty()) { GameObject functionPickup = Instantiate(functionPickupObject) as GameObject; FunctionPickup fp = functionPickup.GetComponent <FunctionPickup>(); SavedFunction sf = new SavedFunction { fTYPE = fTYPE }; sf.GenerateThumbnail(); fp.Function = sf; } }
private void SpliceMutation(FTYPE fType) // TODO add a factor to change the weights impact (maybe a single factor or one for each weight) { LinkGene lg = GetLinkByInnovationID(GetRandomLinkInnovation()); long sourceInnovation = lg.GetSourceInnovation(); long targetInnovation = lg.GetTargetInnovation(); long newNode = EvolutionaryHistory.NextInnovationID(); float weight1 = RandomGenerator.NextGaussian(); float weight2 = RandomGenerator.NextGaussian(); long toLink = EvolutionaryHistory.NextInnovationID(); long fromLink = EvolutionaryHistory.NextInnovationID(); string debugMsg = "SpliceMutation between " + sourceInnovation + " and " + targetInnovation + ". Adding a node with an innovation of " + newNode + " and an activation function of " + fType; if (ArtGallery.DEBUG_LEVEL > ArtGallery.DEBUG.NONE) { Debug.Log(debugMsg); } SpliceNode(fType, newNode, sourceInnovation, targetInnovation, weight1, weight2, toLink, fromLink); }
public void SpliceNode(FTYPE fType, long newNodeInnovation, long sourceInnovation, long targetInnovation, float weight1, float weight2, long toLinkInnovation, long fromLinkInnovation) { NodeGene ng = new NodeGene(NTYPE.HIDDEN, fType, newNodeInnovation); LinkGene lg = GetLinkBetween(sourceInnovation, targetInnovation); //lg.SetActive(false); // TODO active bool is not in use // HACK Links should not be removed when splicing in a new node, but active is not in use yet //links.Remove(lg); // HACK if this fails then it will be because the index is either < 0 or > count - add fixes or write a container w/ helper methods Nodes.Insert(System.Math.Min(OutputStartIndex(), System.Math.Max(numInputs, IndexOfNodeInnovation(sourceInnovation) + 1)), ng); //int index = EvolutionaryHistory.IndexOfArchetypeInnovation(archetypeIndex, sourceInnovation); //int pos = System.Math.Min(EvolutionaryHistory.FirstArchetypeOutputIndex(archetypeIndex), System.Math.Max(numInputs, index + 1)); EvolutionaryHistory.AddArchetype(archetypeIndex, Nodes.IndexOf(ng), ng.Clone(), "origin"); LinkGene toNew = new LinkGene(sourceInnovation, newNodeInnovation, weight1, toLinkInnovation); LinkGene fromNew = new LinkGene(newNodeInnovation, targetInnovation, weight2, fromLinkInnovation); Links.Add(toNew); Links.Add(fromNew); }
/// <summary> /// Get a random activation function /// </summary> /// <returns>FTYPE</returns> public static FTYPE RandomFTYPE2() { FTYPE type = FTYPE.ID; int rnd = Random.Range(0, 6) + 1; switch (rnd) { case 1: type = FTYPE.TANH; break; case 2: type = FTYPE.SIGMOID; break; case 3: type = FTYPE.SINE; break; case 4: type = FTYPE.COS; break; case 5: type = FTYPE.GAUSS; break; case 6: type = FTYPE.ID; break; case 7: type = FTYPE.FULLAPPROX; break; case 8: type = FTYPE.APPROX; break; case 9: type = FTYPE.ABSVAL; break; case 10: type = FTYPE.PIECEWISE; break; case 11: type = FTYPE.HLPIECEWISE; break; case 12: type = FTYPE.SAWTOOTH; break; case 13: type = FTYPE.STRETCHED_TANH; break; case 14: type = FTYPE.RE_LU; break; case 15: type = FTYPE.SOFTPLUS; break; case 16: type = FTYPE.LEAKY_RE_LU; break; case 17: type = FTYPE.FULLSAWTOOTH; break; case 18: type = FTYPE.TRIANGLEWAVE; break; case 19: type = FTYPE.SQUAREWAVE; break; case 20: type = FTYPE.FULLSIGMOID; break; case 21: type = FTYPE.FULLGAUSS; break; case 22: type = FTYPE.SIL; break; case 23: type = FTYPE.DSIL; break; default: break; } return(type); }
/// <summary> /// Use an activation function of a node by <see cref="FTYPE" /> /// </summary> /// <param name="fType">Enum <see cref="FTYPE" /></param> /// <param name="sum">Input sent to node</param> /// <returns></returns> public static float Activation(FTYPE fType, float sum) { return(activationFunctions[fType].Function(sum)); }
public FullNodeGene(NTYPE nType, FTYPE fType, long innovation, float bias) : base(nType, fType, innovation) { this.bias = bias; }
public TWEANNGenotype(int numInputs, int numOutputs, bool featureSelective, FTYPE fType, int archetypeIndex) : this(new TWEANN(numInputs, numOutputs, featureSelective, fType, archetypeIndex)) { }
/// <summary> /// New node with no target. Frozen by default /// </summary> /// <param name="fType">Activation function</param> /// <param name="nType">Node type</param> /// <param name="innovation">Unique innovation number for node</param> /// <param name="bias">Bias offset added to neuron sum before activation. Primarily needed by substrate networks from CPPNs</param> public TWEANNNode(FTYPE fType, NTYPE nType, long innovation, float bias) : this(fType, nType, innovation, false, bias) { }
public NodeGene(NTYPE nTYPE, FTYPE fTYPE, long innovation) : base(innovation) { this.fTYPE = fTYPE; this.nTYPE = nTYPE; bias = 0.0f; }
public bool FunctionIsActive(FTYPE fTYPE) { return(activeFunctions.Contains(fTYPE)); }
public void SetFTYPE(FTYPE fTYPE) { this.fTYPE = fTYPE; }
// TODO any other vars needed to create a graphical output goes here /// <summary> /// New node with no target. Frozen by default /// </summary> /// <param name="fType">Activation function</param> /// <param name="nType">Node type</param> /// <param name="innovation">Uniqu innovation number for node</param> public TWEANNNode(FTYPE fType, NTYPE nType, long innovation) : this(fType, nType, innovation, 0.0f) { }