コード例 #1
0
        public NetworkFloatDllTools(NEATNetwork network)
        {
            parm = new NEATNetworkParm();

            parm.linkCount = network.Links.Length;
            _linkArr       = new Link[parm.linkCount];
            int index = 0;

            foreach (NEATLink l in network.Links)
            {
                _linkArr[index].fromNeuron = l.FromNeuron;
                _linkArr[index].toNeuron   = l.ToNeuron;
                _linkArr[index].weight     = (float)l.Weight;
                index++;
            }
            parm.link = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Link)) * parm.linkCount);
            IntPtr pAddr = parm.link;

            for (int i = 0; i < _linkArr.Length; i++)
            {
                Marshal.StructureToPtr(_linkArr[i], pAddr, false);
                pAddr += Marshal.SizeOf(typeof(Link));
            }

            parm.neuronCount    = network.PostActivation.Length;
            parm.outputIndex    = network.OutputIndex;
            parm.preActivation  = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(float)) * parm.neuronCount);
            parm.postActivation = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(float)) * parm.neuronCount);

            parm.outBufferCtrlCount = network.PreActivation.Length;
            parm.outBufferCtrl      = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(OutputBufferCtrl)) * parm.outBufferCtrlCount);
            int outBufferLineLength = sizeof(float) * network.PreActivation.Length;

            _outBuffer = Marshal.AllocHGlobal(outBufferLineLength * parm.outBufferCtrlCount);
            IntPtr           pAddr2     = parm.outBufferCtrl;
            IntPtr           pOutAddr   = _outBuffer;
            OutputBufferCtrl bufferCtrl = new OutputBufferCtrl();

            bufferCtrl.length = 0;
            bufferCtrl.pBuf   = IntPtr.Zero;
            for (int i = 0; i < parm.outBufferCtrlCount; i++)
            {
                bufferCtrl.pBufStart = pOutAddr;
                Marshal.StructureToPtr(bufferCtrl, pAddr2, false);
                pAddr2   += Marshal.SizeOf(typeof(OutputBufferCtrl));
                pOutAddr += outBufferLineLength;
            }

            parm.inputCount = network.InputCount;
            parm.input      = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(double)) * parm.inputCount);

            parm.outputCount = network.OutputCount;
            _outputBuffer    = new double[network.OutputCount];
            parm.output      = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(double)) * parm.outputCount);

            parm.activationCycles = network.ActivationCycles;
        }
コード例 #2
0
        static NEATNetworkTest()
        {
            // Create Network
            _net                  = new NEATNetwork();
            _net.InputCount       = _inputCount;
            _net.OutputCount      = _outputCount;
            _net.ActivationCycles = 4;
            _net.OutputIndex      = _net.InputCount + 1;

            _net.Links = new NEATLink[(int)(_net.InputCount * _net.OutputCount * 1.5)];
            Random rand = new Random();

            for (int i = 0; i < _net.Links.Length; i++)
            {
                _net.Links[i] = new NEATLink()
                {
                    FromNeuron = rand.Next(_net.Links.Length),
                    ToNeuron   = rand.Next(_net.Links.Length),
                    Weight     = (rand.NextDouble() - 0.5) * 0.02,
                };
            }
            _net.PreActivation  = new double[_net.Links.Length];
            _net.PostActivation = new double[_net.Links.Length];


            // Create sampleData;
            _sampleTestData   = new double[_inputCount];
            _sampleResultData = new double[_outputCount];

            for (int i = 0; i < _inputCount; i++)
            {
                _sampleTestData[i] = rand.NextDouble() * 2 - 1;
            }

            _sampleResultData = _net.Compute(_sampleTestData);
        }
コード例 #3
0
        public NetworkFloatDllTools(NEATNetwork network)
        {
            parm = new NEATNetworkParm();

            parm.linkCount = network.Links.Length;
            _linkArr = new Link[parm.linkCount];
            int index = 0;
            foreach (NEATLink l in network.Links)
            {
                _linkArr[index].fromNeuron = l.FromNeuron;
                _linkArr[index].toNeuron = l.ToNeuron;
                _linkArr[index].weight = (float)l.Weight;
                index++;
            }
            parm.link = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Link)) * parm.linkCount);
            IntPtr pAddr = parm.link;
            for (int i = 0; i < _linkArr.Length; i++)
            {
                Marshal.StructureToPtr(_linkArr[i], pAddr, false);
                pAddr += Marshal.SizeOf(typeof(Link));
            }

            parm.neuronCount = network.PostActivation.Length;
            parm.outputIndex = network.OutputIndex;
            parm.preActivation = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(float)) * parm.neuronCount);
            parm.postActivation = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(float)) * parm.neuronCount);

            parm.outBufferCtrlCount = network.PreActivation.Length;
            parm.outBufferCtrl = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(OutputBufferCtrl)) * parm.outBufferCtrlCount);
            int outBufferLineLength = sizeof(float) * network.PreActivation.Length;
            _outBuffer = Marshal.AllocHGlobal(outBufferLineLength * parm.outBufferCtrlCount);
            IntPtr pAddr2 = parm.outBufferCtrl;
            IntPtr pOutAddr = _outBuffer;
            OutputBufferCtrl bufferCtrl = new OutputBufferCtrl();
            bufferCtrl.length = 0;
            bufferCtrl.pBuf = IntPtr.Zero;
            for (int i = 0; i < parm.outBufferCtrlCount; i++)
            {
                bufferCtrl.pBufStart = pOutAddr;
                Marshal.StructureToPtr(bufferCtrl, pAddr2, false);
                pAddr2 += Marshal.SizeOf(typeof(OutputBufferCtrl));
                pOutAddr += outBufferLineLength;
            }

            parm.inputCount = network.InputCount;
            parm.input = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(double)) * parm.inputCount);

            parm.outputCount = network.OutputCount;
            _outputBuffer = new double[network.OutputCount];
            parm.output = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(double)) * parm.outputCount);

            parm.activationCycles = network.ActivationCycles;
        }
コード例 #4
0
        static NEATNetworkTest()
        {
            // Create Network
            _net = new NEATNetwork();
            _net.InputCount = _inputCount;
            _net.OutputCount = _outputCount;
            _net.ActivationCycles = 4;
            _net.OutputIndex = _net.InputCount + 1;

            _net.Links = new NEATLink[(int)(_net.InputCount * _net.OutputCount * 1.5)];
            Random rand = new Random();
            for (int i = 0; i < _net.Links.Length; i++)
            {
                _net.Links[i] = new NEATLink()
                {
                    FromNeuron = rand.Next(_net.Links.Length),
                    ToNeuron = rand.Next(_net.Links.Length),
                    Weight = (rand.NextDouble() - 0.5) * 0.02,
                };
            }
            _net.PreActivation = new double[_net.Links.Length];
            _net.PostActivation = new double[_net.Links.Length];

            // Create sampleData;
            _sampleTestData = new double[_inputCount];
            _sampleResultData = new double[_outputCount];

            for (int i = 0; i < _inputCount; i++)
            {
                _sampleTestData[i] = rand.NextDouble() * 2 - 1;
            }

            _sampleResultData = _net.Compute(_sampleTestData);
        }