Exemplo n.º 1
0
        public override ShipPartDNA GetNewDNA()
        {
            BrainNEATDNA retVal = (BrainNEATDNA)Design.GetDNA();

            retVal.UniqueID = UniqueID;

            //NOTE: The design class doesn't hold neurons, since it's only used by the editor, so fill out the rest of the dna here
            retVal.Neurons = _neuronsAll.
                             Select(o => o.Position).
                             ToArray();

            if (_genome != null)
            {
                retVal.Activation           = _activation;
                retVal.NEATPositions_Input  = _neatPositions_Input;
                retVal.NEATPositions_Output = _neatPositions_Output;
                retVal.Hyper  = _hyperneatArgs;
                retVal.Genome = ExperimentNEATBase.SavePopulation(new[] { _genome });
            }

            return(retVal);
        }
Exemplo n.º 2
0
        private void BrainNEAT_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (_experiment == null || _ea == null)
                {
                    //NOTE: Just doing this for laziness.  I don't want to write a bunch of logic to train a phenome up front
                    MessageBox.Show("Need to have a running experiment", this.Title, MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }

                // Get a genome
                NeatGenome genome = _ea.CurrentChampGenome;

                // Create a phenome
                IBlackBox phenome = ExperimentNEATBase.GetBlackBox(genome, _experimentArgs.Activation, _hyperneatArgs);

                // Instantiate a BrainNEAT
                EditorOptions options     = new EditorOptions();
                ItemOptions   itemOptions = new ItemOptions();

                Container energy = new Container()
                {
                    QuantityMax     = 1000,
                    QuantityCurrent = 1000,
                };

                BrainNEATDNA dnaBrain = new BrainNEATDNA()
                {
                    PartType = BrainNEAT.PARTTYPE, Position = new Point3D(0, 0, 0), Orientation = Quaternion.Identity, Scale = new Vector3D(1, 1, 1)
                };

                BrainNEAT brain = new BrainNEAT(options, itemOptions, dnaBrain, energy);


                brain.SetPhenome(phenome, genome, _experimentArgs.Activation, _hyperneatArgs);


                for (int cntr = 0; cntr < 100; cntr++)
                {
                    foreach (INeuron neuron in brain.Neruons_Writeonly)
                    {
                        neuron.SetValue(StaticRandom.NextDouble(-2.5, 2.5));
                    }

                    brain.Update_AnyThread(1);
                }



                #region save/load test2

                // let BrainNEAT do the save/load

                BrainNEATDNA dna2     = (BrainNEATDNA)brain.GetNewDNA();
                string       dna2Text = XamlServices.Save(dna2).Replace('"', '\'');
                BrainNEAT    brain2   = new BrainNEAT(options, itemOptions, dna2, energy);

                for (int cntr = 0; cntr < 100; cntr++)
                {
                    foreach (INeuron neuron in brain2.Neruons_Writeonly)
                    {
                        neuron.SetValue(StaticRandom.NextDouble(-2.5, 2.5));
                    }

                    brain2.Update_AnyThread(1);
                }

                BrainNEATDNA dna3   = (BrainNEATDNA)brain2.GetNewDNA();
                BrainNEAT    brain3 = new BrainNEAT(options, itemOptions, dna3, energy);

                for (int cntr = 0; cntr < 100; cntr++)
                {
                    foreach (INeuron neuron in brain3.Neruons_Writeonly)
                    {
                        neuron.SetValue(StaticRandom.NextDouble(-2.5, 2.5));
                    }

                    brain3.Update_AnyThread(1);
                }

                #endregion
                #region save/load test1

                // initial test, building minimum necessary dna

                BrainNEATDNA brainDNA = new BrainNEATDNA()
                {
                    Activation           = _experimentArgs.Activation,
                    Hyper                = _hyperneatArgs,
                    NEATPositions_Input  = brain.Neruons_Readonly.Select(o => o.Position).ToArray(),
                    NEATPositions_Output = brain.Neruons_Writeonly.Select(o => o.Position).ToArray(),
                    Genome               = ExperimentNEATBase.SavePopulation(new[] { genome }),
                };


                // Make sure this can be serialized/deserialized
                string testString = XamlServices.Save(brainDNA);
                brainDNA = UtilityCore.Clone(brainDNA);


                List <NeatGenome> genomeList = null;
                if (_hyperneatArgs == null)
                {
                    genomeList = ExperimentNEATBase.LoadPopulation(brainDNA.Genome, brainDNA.Activation, brainDNA.NEATPositions_Input.Length, brainDNA.NEATPositions_Output.Length);
                }
                else
                {
                    genomeList = ExperimentNEATBase.LoadPopulation(brainDNA.Genome, brainDNA.Activation, _hyperneatArgs);
                }

                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Exemplo n.º 3
0
        private void Reset2_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // If not hyperneat, just replace with a new net
                // If hyperneat, compare other args and retain genomes if the only change is input/output resolution

                string prevGenomeXML = null;
                if (chkHyperNEAT.IsChecked.Value && _experiment != null && _ea != null)
                {
                    //_ea.Stop();       // currently, Stop just calls RequestPause, so don't use it.  There needs to be a dispose that removes the underlying thread
                    _ea.RequestPauseAndWait();
                    prevGenomeXML = ExperimentNEATBase.SavePopulation(_ea.GenomeList);
                }

                RemoveExistingExperiment();

                // My stuff
                #region harness args

                _harnessArgs = new HarnessArgs(
                    trkMapSize.Value,
                    trkVisionSize.Value,
                    trkOutputSize.Value,
                    trkInputPixels.Value.ToInt_Round(),
                    trkOutputPixels.Value.ToInt_Round(),
                    trkDelayBetweenInstances.Value);

                #endregion
                #region eval args

                if (chkRandomStartingConditions.IsChecked.Value)
                {
                    _evalArgs = new EvaluatorArgs(
                        trkEvalIterations.Value.ToInt_Round(),
                        trkDelay.Value,
                        trkEvalElapsedTime.Value,
                        trkMaxSpeed.Value,
                        chkBounceOffWalls.IsChecked.Value,
                        new[] { (TrackedItemType)cboTrackedItemType.SelectedValue },
                        trkNewItemDuration.Value,
                        trkNewItemErrorMultiplier.Value,
                        (ScoreLeftRightBias)cboErrorBias.SelectedValue);
                }
                else
                {
                    Point  position = Math3D.GetRandomVector(_harnessArgs.MapSize / 2).ToPoint2D();
                    Vector velocity = Math3D.GetRandomVector_Circular(trkMaxSpeed.Value).ToVector2D();

                    // Don't let the velocity be in the same quadrant as the position (otherwise, you could have something spawn next to a wall, heading
                    // toward the wall).  These if statements force it to cross the x,y axiis
                    if (Math.Sign(position.X) == Math.Sign(velocity.X))
                    {
                        velocity = new Vector(-velocity.X, velocity.Y);
                    }

                    if (Math.Sign(position.Y) == Math.Sign(velocity.Y))
                    {
                        velocity = new Vector(velocity.X, -velocity.Y);
                    }

                    _evalArgs = new EvaluatorArgs(
                        trkEvalIterations.Value.ToInt_Round(),
                        trkDelay.Value,
                        trkEvalElapsedTime.Value,
                        new[] { Tuple.Create((TrackedItemType)cboTrackedItemType.SelectedValue, position, velocity, chkBounceOffWalls.IsChecked.Value) },
                        trkNewItemDuration.Value,
                        trkNewItemErrorMultiplier.Value,
                        (ScoreLeftRightBias)cboErrorBias.SelectedValue);
                }

                #endregion

                // SharpNEAT
                #region experiment args

                _experimentArgs = new ExperimentInitArgs()
                {
                    Description    = "Input is a pixel array.  Output is a pixel array.  The NN needs to watch the object and anticipate where it will be at some fixed time in the future",
                    InputCount     = _harnessArgs.InputSizeXY * _harnessArgs.InputSizeXY,
                    OutputCount    = _harnessArgs.OutputSizeXY * _harnessArgs.OutputSizeXY,
                    IsHyperNEAT    = chkHyperNEAT.IsChecked.Value,
                    PopulationSize = trkPopulationSize.Value.ToInt_Round(),
                    SpeciesCount   = trkSpeciesCount.Value.ToInt_Round(),
                    Activation     = new ExperimentInitArgs_Activation_CyclicFixedTimesteps()
                    {
                        TimestepsPerActivation = trkTimestepsPerActivation.Value.ToInt_Round(),
                        FastFlag = true
                    },
                    Complexity_RegulationStrategy = ComplexityCeilingType.Absolute,
                    Complexity_Threshold          = trkComplexityThreshold.Value.ToInt_Round(),
                };

                #endregion
                #region hyperneat args

                _hyperneatArgs = null;

                if (chkHyperNEAT.IsChecked.Value)
                {
                    // Use two square sheets
                    var hyperPoints = HyperNEAT_Args.GetSquareSheets(trkVisionSize.Value, trkOutputSize.Value, _harnessArgs.InputSizeXY, _harnessArgs.OutputSizeXY);

                    _hyperneatArgs = new HyperNEAT_Args()
                    {
                        InputPositions  = hyperPoints.inputs,
                        OutputPositions = hyperPoints.outputs,
                    };
                }

                #endregion

                #region create harness

                _harness = new TrackedItemHarness(_harnessArgs);

                _harness.ItemRemoved += (s1, e1) =>
                {
                    _harness.SetItem(AntPos_Evaluator.GetNewItem(_harness, _evalArgs));
                };

                _harness.SetItem(AntPos_Evaluator.GetNewItem(_harness, _evalArgs));

                #endregion
                #region create evaluator

                AntPos_Evaluator evaluator = new AntPos_Evaluator(_harnessArgs, _evalArgs);

                //FitnessInfo score = evaluator.Evaluate(new RandomBlackBoxNetwork(_harness.InputSizeXY * _harness.InputSizeXY, _harness.OutputSizeXY * _harness.OutputSizeXY, true));      // this is a good place to unit test the evaluator

                #endregion
                #region create experiment

                _experiment = new ExperimentNEATBase();
                _experiment.Initialize("anticipate position", _experimentArgs, evaluator);

                #endregion
                #region create evolution algorithm

                if (prevGenomeXML == null)
                {
                    if (chkHyperNEAT.IsChecked.Value)
                    {
                        _ea = _experiment.CreateEvolutionAlgorithm(_hyperneatArgs);
                    }
                    else
                    {
                        _ea = _experiment.CreateEvolutionAlgorithm();
                    }
                }
                else
                {
                    List <NeatGenome> genomeList;
                    if (_hyperneatArgs == null)
                    {
                        genomeList = ExperimentNEATBase.LoadPopulation(prevGenomeXML, _experimentArgs.Activation, _experimentArgs.InputCount, _experimentArgs.OutputCount);
                    }
                    else
                    {
                        genomeList = ExperimentNEATBase.LoadPopulation(prevGenomeXML, _experimentArgs.Activation, _hyperneatArgs);
                    }

                    // The factory is the same for all items, so just grab the first one
                    NeatGenomeFactory genomeFactory = genomeList[0].GenomeFactory;

                    _ea = _experiment.CreateEvolutionAlgorithm(genomeFactory, genomeList, _hyperneatArgs);
                }

                _ea.UpdateEvent += EA_UpdateEvent;
                _ea.PausedEvent += EA_PausedEvent;

                #endregion

                ShowBestGenome();                                           // this ensures the neural viewer is created
                _winningBrainTime = DateTime.UtcNow - TimeSpan.FromDays(1); // put it way in the past so the first tick will request a new winner
                _winningBrain     = null;
                //_winningBrain = new RandomBlackBoxNetwork(_harness.InputSizeXY * _harness.InputSizeXY, _harness.OutputSizeXY * _harness.OutputSizeXY, true);

                _tickCounter = _evalArgs.TotalNumberEvaluations * 2; // force the timer to get the winning NN right away (otherwise it will do a round before refreshing)

                _ea.StartContinue();                                 // this needs to be done last
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }