Beispiel #1
0
        /// <summary>
        /// Constructs a CyclicNetwork with the provided pre-built neurons and connections.
        /// </summary>
        public CyclicNetwork(List <Neuron> neuronList,
                             List <Connection> connectionList,
                             int inputNeuronCount,
                             int outputNeuronCount,
                             int timestepsPerActivation,
                             bool boundedOutput)
        {
            _neuronList              = neuronList;
            _connectionList          = connectionList;
            _inputNeuronCount        = inputNeuronCount;
            _outputNeuronCount       = outputNeuronCount;
            _inputAndBiasNeuronCount = inputNeuronCount + 1;
            _timestepsPerActivation  = timestepsPerActivation;

            _inputSignalArray  = new double[_inputNeuronCount];
            _outputSignalArray = new double[_outputNeuronCount];

            _inputSignalArrayWrapper = new SignalArray(_inputSignalArray, 0, _inputNeuronCount);

            if (boundedOutput)
            {
                _outputSignalArrayWrapper = new OutputSignalArray(_outputSignalArray, 0, outputNeuronCount);
            }
            else
            {
                _outputSignalArrayWrapper = new SignalArray(_outputSignalArray, 0, outputNeuronCount);
            }
        }
        /// <summary>
        /// Constructs a FastCyclicNetwork with the provided pre-built FastConnection array and
        /// associated data.
        /// </summary>
        public FastCyclicNetwork(FastConnection[] connectionArray,
                                 IActivationFunction[] neuronActivationFnArray,
                                 double[][] neuronAuxArgsArray,
                                 int neuronCount,
                                 int inputNeuronCount,
                                 int outputNeuronCount,
                                 int timestepsPerActivation)
        {
            _connectionArray         = connectionArray;
            _neuronActivationFnArray = neuronActivationFnArray;
            _neuronAuxArgsArray      = neuronAuxArgsArray;

            // Create neuron pre- and post-activation signal arrays.
            _preActivationArray  = new double[neuronCount];
            _postActivationArray = new double[neuronCount];

            // Wrap sub-ranges of the neuron signal arrays as input and output arrays for IBlackBox.
            // Offset is 1 to skip bias neuron (The value at index 1 is the first black box input).
            _inputSignalArrayWrapper = new SignalArray(_postActivationArray, 1, inputNeuronCount);

            // Offset to skip bias and input neurons. Output neurons follow input neurons in the arrays.
            _outputSignalArrayWrapper = new SignalArray(_postActivationArray, inputNeuronCount + 1, outputNeuronCount);

            // Store counts for use during activation.
            _inputNeuronCount        = inputNeuronCount;
            _inputAndBiasNeuronCount = inputNeuronCount + 1;
            _outputNeuronCount       = outputNeuronCount;
            _timestepsPerActivation  = timestepsPerActivation;

            // Initialise the bias neuron's fixed output value.
            _postActivationArray[0] = 1.0;
        }
Beispiel #3
0
 public QuickBlackBox(QEAGenome genome)
 {
     this.genome        = genome;
     inputs             = new double[InputCount];
     outputs            = new double[OutputCount];
     _inputSignalArray  = new SignalArray(inputs, 0, inputs.Length);
     _outputSignalArray = new SignalArray(outputs, 0, outputs.Length);
 }
Beispiel #4
0
        public RandomBlackBoxNetwork(int inputCount, int outputCount, bool isPositiveOnly)
        {
            InputCount  = inputCount;
            OutputCount = outputCount;

            _isPositiveOnly = isPositiveOnly;

            InputSignalArray  = new SignalArray(new double[inputCount], 0, inputCount);
            OutputSignalArray = new SignalArray(new double[outputCount], 0, outputCount);
        }
        /// <summary>
        /// Called at every step in the world. Given the sensor input, returns the change in orientation and velocity
        /// in the range [0,1].
        /// </summary>
        public override ISignalArray activateNetwork(double[] sensors)
        {
            // Update the value function for the previously-chosen actions
            updateValueFunction(sensors);

            // Select the actions to take
            _prevState = selectEpsilonGreedy(sensors);

            // Return the result
            var results = new SignalArray(new double[] { _prevState[_prevState.Length - 2], _prevState[_prevState.Length - 1] }, 0, 2);

            //Console.WriteLine("Selecting: ({0},{1})", results[0], results[1]);

            return(results);
        }
Beispiel #6
0
        /// <summary>
        /// Construct a FastAcyclicNetwork with provided network definition data structures.
        /// </summary>
        /// <param name="nodeActivationFnArr">Array of neuron activation functions.</param>
        /// <param name="nodeAuxArgsArr">Array of neuron activation function arguments.</param>
        /// <param name="connectionArr">Array of connections.</param>
        /// <param name="layerInfoArr">Array of layer information.</param>
        /// <param name="outputNodeIdxArr">An array that specifies the index of each output neuron within _activationArr.
        /// This is necessary because the neurons have been sorted by their depth in the network structure and are therefore
        /// no longer in their original positions. Note however that the bias and input neurons *are* in their original
        /// positions as they are defined as being at depth zero.</param>
        /// <param name="nodeCount">Number of nodes in the network.</param>
        /// <param name="inputNodeCount">Number of input nodes in the network.</param>
        /// <param name="outputNodeCount">Number of output nodes in the network.</param>
        /// <param name="boundedOutput">Indicates that the output values at the output nodes should be bounded to the interval [0,1]</param>
        public FastAcyclicNetwork(IActivationFunction[] nodeActivationFnArr,
                                  double[][] nodeAuxArgsArr,
                                  FastConnection[] connectionArr,
                                  LayerInfo[] layerInfoArr,
                                  int[] outputNodeIdxArr,
                                  int nodeCount,
                                  int inputNodeCount,
                                  int outputNodeCount,
                                  bool boundedOutput)
        {
            // Store refs to network structure data.
            _nodeActivationFnArr = nodeActivationFnArr;
            _nodeAuxArgsArr      = nodeAuxArgsArr;
            _connectionArr       = connectionArr;
            _layerInfoArr        = layerInfoArr;

            // Create working array for node activation signals.
            _activationArr = new double[nodeCount];

            // Wrap a sub-range of the _activationArr that holds the activation values for the input nodes.
            // Offset is 1 to skip bias neuron (The value at index 1 is the first black box input).
            _inputSignalArrayWrapper = new SignalArray(_activationArr, 1, inputNodeCount);

            // Wrap the output nodes. Nodes have been sorted by depth within the network therefore the output
            // nodes can no longer be guaranteed to be in a contiguous segment at a fixed location. As such their
            // positions are indicated by outputNodeIdxArr, and so we package up this array with the node signal
            // array to abstract away the level of indirection described by outputNodeIdxArr.
            if (boundedOutput)
            {
                _outputSignalArrayWrapper = new OutputMappingSignalArray(_activationArr, outputNodeIdxArr);
            }
            else
            {
                _outputSignalArrayWrapper = new MappingSignalArray(_activationArr, outputNodeIdxArr);
            }

            // Store counts for use during activation.
            _inputNodeCount        = inputNodeCount;
            _inputAndBiasNodeCount = inputNodeCount + 1;
            _outputNodeCount       = outputNodeCount;

            // Initialise the bias neuron's fixed output value.
            _activationArr[0] = 1.0;
        }
Beispiel #7
0
        /// <summary>
        /// Constructs a CyclicNetwork with the provided pre-built neurons and connections.
        /// </summary>
        public CyclicNetwork(List<Neuron> neuronList,
                                List<Connection> connectionList,
                                int inputNeuronCount,
                                int outputNeuronCount,
                                int timestepsPerActivation)
        {
            _neuronList = neuronList;
            _connectionList = connectionList;
            _inputNeuronCount = inputNeuronCount;
            _outputNeuronCount = outputNeuronCount;
            _inputAndBiasNeuronCount = inputNeuronCount + 1;
            _timestepsPerActivation = timestepsPerActivation;

            _inputSignalArray = new double[_inputNeuronCount];
            _outputSignalArray = new double[_outputNeuronCount];

            _inputSignalArrayWrapper = new SignalArray(_inputSignalArray, 0, _inputNeuronCount);
            _outputSignalArrayWrapper = new SignalArray(_outputSignalArray, 0, outputNeuronCount);
        }
        /// <summary>
        /// Constructs a FastCyclicNetwork with the provided pre-built FastConnection array and 
        /// associated data.
        /// </summary>
        public FastCyclicNetwork(FastConnection[] connectionArray,
                                 IActivationFunction[] neuronActivationFnArray,
                                 double[][] neuronAuxArgsArray,
                                 int neuronCount,
                                 int inputNeuronCount,
                                 int outputNeuronCount,
                                 int timestepsPerActivation)
        {
            _connectionArray = connectionArray;
            _neuronActivationFnArray = neuronActivationFnArray;
            _neuronAuxArgsArray = neuronAuxArgsArray;

            // Create neuron pre- and post-activation signal arrays.
            _preActivationArray = new double[neuronCount];
            _postActivationArray = new double[neuronCount];

            // Wrap sub-ranges of the neuron signal arrays as input and output arrays for IBlackBox.
            // Offset is 1 to skip bias neuron (The value at index 1 is the first black box input).
            _inputSignalArrayWrapper = new SignalArray(_postActivationArray, 1, inputNeuronCount);

            // Offset to skip bias and input neurons. Output neurons follow input neurons in the arrays.
            _outputSignalArrayWrapper = new SignalArray(_postActivationArray, inputNeuronCount+1, outputNeuronCount);

            // Store counts for use during activation.
            _inputNeuronCount = inputNeuronCount;
            _inputAndBiasNeuronCount = inputNeuronCount+1;
            _outputNeuronCount = outputNeuronCount;
            _timestepsPerActivation = timestepsPerActivation;

            // Initialise the bias neuron's fixed output value.
            _postActivationArray[0] = 1.0;
        }
        /// <summary>
        /// Construct a FastAcyclicNetwork with provided network definition data structures.
        /// </summary>
        /// <param name="nodeActivationFnArr">Array of neuron activation functions.</param>
        /// <param name="nodeAuxArgsArr">Array of neuron activation function arguments.</param>
        /// <param name="connectionArr">Array of connections.</param>
        /// <param name="layerInfoArr">Array of layer information.</param>
        /// <param name="outputNodeIdxArr">An array that specifies the index of each output neuron within _activationArr.
        /// This is necessary because the neurons have been sorted by their depth in the network structure and are therefore
        /// no longer in their original positions. Note however that the bias and input neurons *are* in their original 
        /// positions as they are defined as being at depth zero.</param>
        /// <param name="nodeCount">Number of nodes in the network.</param>
        /// <param name="inputNodeCount">Number of input nodes in the network.</param>
        /// <param name="outputNodeCount">Number of output nodes in the network.</param>
        public FastAcyclicNetwork(IActivationFunction[] nodeActivationFnArr,
                                  double[][] nodeAuxArgsArr,
                                  FastConnection[] connectionArr,
                                  LayerInfo[] layerInfoArr,
                                  int[] outputNodeIdxArr,
                                  int nodeCount,
                                  int inputNodeCount,
                                  int outputNodeCount)
        {
            // Store refs to network structrue data.
            _nodeActivationFnArr = nodeActivationFnArr;
            _nodeAuxArgsArr = nodeAuxArgsArr;
            _connectionArr = connectionArr;
            _layerInfoArr = layerInfoArr;

            // Create working array for node activation signals.
            _activationArr = new double[nodeCount];

            // Wrap a sub-range of the _activationArr that holds the activation values for the input nodes.
            // Offset is 1 to skip bias neuron (The value at index 1 is the first black box input).
            _inputSignalArrayWrapper = new SignalArray(_activationArr, 1, inputNodeCount);

            // Wrap the output nodes. Nodes have been sorted by depth within the network therefore the output
            // nodes can no longer be guaranteed to be in a contiguous segment at a fixed location. As such their
            // positions are indicated by outputNodeIdxArr, and so we package up this array with the node signal
            // array to abstract away the level of indirection described by outputNodeIdxArr.
            _outputSignalArrayWrapper = new MappingSignalArray(_activationArr, outputNodeIdxArr);

            // Store counts for use during activation.
            _inputNodeCount = inputNodeCount;
            _inputAndBiasNodeCount = inputNodeCount+1;
            _outputNodeCount = outputNodeCount;

            // Initialise the bias neuron's fixed output value.
            _activationArr[0] = 1.0;
        }
Beispiel #10
0
        /// <summary>
        /// Constructs a FastCyclicNetwork.
        /// </summary>
        public EspCyclicNetwork(PhenomeVariables phenomeVariables)
        {
            // This structure contains all we need. However, we will make a
            // local copy of a few variables for extra performance.
            _phenVars = phenomeVariables;

            // Connectivity data
            _connectionArray             = _phenVars.fastConnectionArray;
            _localOutToRegLInConnect     = _phenVars.localOutToRegOrLInConnect;
            _localOutToOutConnect        = _phenVars.localOutToOutConnect;
            _localOutToRegLInModuleCount = _phenVars.lOutToRegOrLInModuleCount;
            _localOutToOutModuleCount    = _phenVars.localOutToOutModuleCount;

            // Create neuron pre- and post-activation signal arrays.
            _preActivationArray  = new double[_phenVars.neuronCount];
            _postActivationArray = new double[_phenVars.neuronCount];
            // Sets activation values to 0.
            ResetState();

            // Wrap sub-ranges of the neuron signal arrays as input and output
            // arrays for IBlackBox.
            // Input neurons: offset is 1 to skip bias neuron.
            _inputSignalArrayWrapper = new SignalArray(_postActivationArray, 1,
                                                       _phenVars.inputBiasCount - 1);
            // Output neurons: offset to skip bias and input neurons.
            _outputSignalArrayWrapper = new SignalArray(_postActivationArray,
                                                        _phenVars.inputBiasCount,
                                                        _phenVars.outputCount);

            // Pandemonium data
            // _numberOfPandem (use _phenVars.numberOfPandem)
            _pandemoniumCounts = _phenVars.pandemoniumCounts;
            _pandemonium       = _phenVars.pandemonium;

            // Counts and indices
            _inputCount            = _phenVars.inputBiasCount - 1;
            _inToRegEndIndex       = _connectionArray.Length - _phenVars.nonProtectedCount;
            _connectionArrayLength = _connectionArray.Length;
            // We get the index for the first regulatory because we are counting
            // elements. The index for the las element is our result - 1 (because
            // of the index 0) so we already have the index for the next element!
            _firstRegIndex           = _phenVars.inputBiasCount + _phenVars.outputCount;
            _inBiasOutRegEndIndex    = _firstRegIndex + _phenVars.regulatoryCount;
            _localOutToOutFirstIndex = _phenVars.neuronCount -
                                       _phenVars.localOutToOnlyOut;
            _localInFromBiasInEndIndex = _localOutToOutFirstIndex -
                                         _phenVars.localInFromLocalOutCount;
            _hiddenLocalOutNoOutEndIndex = _localInFromBiasInEndIndex -
                                           _phenVars.localInFromBiasInCount;

            // The first connection is always the auxiliary connection, which
            // is protected and has maximum weight:
            //_maxWeight = _connectionArray[0]._weight;
            // BUT: For protected connections we may not consider the weight,
            // since it is constant.

            // Activation functions

            /*
             * _neuronActivationFnArray = _phenVars.neuronActivationFnArray;
             * _neuronAuxArgsArray = _phenVars.neuronAuxArgsArray;
             */
            _normalNeuronActivFn = _phenVars.normalNeuronActivFn;
            _regulatoryActivFn   = _phenVars.regulatoryActivFn;
            _outputNeuronActivFn = _phenVars.outputNeuronActivFn;

            // Initialise the bias neuron's fixed output value.
            _postActivationArray[0] = 1.0;

/*            UnityEngine.Debug.Log("TIMESTEPS TO 2");
 *          _phenVars.timestepsPerActivation = 2;*/
        }