예제 #1
0
        private void RemoveExistingExperiment()
        {
            if (_ea != null)
            {
                //_ea.Stop();
                _ea.Dispose();
            }

            if (_experiment != null)
            {
                //_experiment.
            }

            _experiment = null;
            _ea         = null;

            _experimentArgs = null;
            _hyperneatArgs  = null;

            _harness     = null;
            _harnessArgs = null;
            _evalArgs    = null;

            _winningBrain = null;
        }
예제 #2
0
        public TrainingSession(ShipDNA dna, ShipExtraArgs shipExtraArgs, int inputCount, int outputCount, Func <WorldAccessor, TrainingRoom, IPhenomeTickEvaluator <IBlackBox, NeatGenome> > getNewEvaluator, int roomCount = 25, double roomSize = ROOMSIZE)
        {
            DNA           = dna;
            ShipExtraArgs = shipExtraArgs;

            double roomMargin = roomSize / 10;

            Arena = new ArenaAccessor(roomCount, roomSize, roomMargin, false, false, new Type[] { typeof(Bot) }, new Type[] { typeof(Bot) }, shipExtraArgs.NeuralPoolManual, (.1, .25));
            Arena.WorldCreated += Arena_WorldCreated;

            foreach (var(room, _) in Arena.AllRooms)
            {
                room.Evaluator = getNewEvaluator(Arena.WorldAccessor, room);
            }

            #region experiment args

            ExperimentInitArgs experimentArgs = new ExperimentInitArgs()
            {
                Description    = "Trains an individual BrainNEAT part",
                InputCount     = inputCount,
                OutputCount    = outputCount,
                IsHyperNEAT    = false,
                PopulationSize = roomCount,        // may want to do a few more than rooms in case extra are made
                SpeciesCount   = Math.Max(2, (roomCount / 4d).ToInt_Ceiling()),
                Activation     = GetActivationFunctionArgs(),
                Complexity_RegulationStrategy = ComplexityCeilingType.Absolute,
                Complexity_Threshold          = 200,
            };

            #endregion

            Experiment = new ExperimentNEATBase();

            // Use the overload that takes the tick phenome
            Experiment.Initialize("BrainNEAT Trainer", experimentArgs, Arena.AllRooms.Select(o => o.room.Evaluator).ToArray(), Arena.WorldAccessor.RoundRobinManager, Arena.WorldAccessor.UpdateWorld);

            EA = Experiment.CreateEvolutionAlgorithm();

            // look in AnticipatePositionWindow for an example.  The update event just displays stats
            //ea.UpdateEvent += EA_UpdateEvent;
            //ea.PausedEvent += EA_PausedEvent;
        }
예제 #3
0
        private void Reset_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                #region ANNOYING XML

                //                string xml = "<?xml version=\"1.0\" encoding=\"utf - 8\" ?>" + @"
                //  <Experiments>

                //    " + "<Experiment name=\"Pick a Number\"" + @">
                //  <AssemblyPath>SharpNeatDomains.dll</AssemblyPath >
                //  <ClassName>SharpNeat.Domains.FunctionRegression.FunctionRegressionExperiment</ClassName>
                //  <Config>
                //    <PopulationSize>150</PopulationSize>
                //    <SpecieCount>10</SpecieCount>
                //    <Activation>
                //      <Scheme>Acyclic</ Scheme >
                //    </Activation>
                //    <ComplexityRegulationStrategy>Absolute</ ComplexityRegulationStrategy >
                //    <ComplexityThreshold>20</ComplexityThreshold>
                //    <Description>Pick a Number</Description>
                //    </Config>
                //  </Experiment>

                //</Experiments>
                //";

                //                //< Function > Sine </ Function >
                //                //< SampleResolution > 21 </ SampleResolution >
                //                //< SampleMin > -3.14159 </ SampleMin >
                //                //< SampleMax > 3.14159 </ SampleMax >


                //                XmlDocument doc = new XmlDocument();
                //                doc.LoadXml(xml);

                //                XmlElement element = doc.SelectSingleNode("Config") as XmlElement;

                //                _experiment.Initialize("Pick a Number", element);

                #endregion

                int[] trueValues = UtilityCore.RandomRange(0, 8, StaticRandom.Next(1, 8)).ToArray();

                ExperimentInitArgs config = new ExperimentInitArgs()
                {
                    Description    = "Some numbers are chosen, the NN needs to train itself to return true when one of those numbers are presented",
                    InputCount     = 3,
                    OutputCount    = 1,
                    PopulationSize = 150,
                    SpeciesCount   = 10,
                    Activation     = new ExperimentInitArgs_Activation_Acyclic(),
                    Complexity_RegulationStrategy = ComplexityCeilingType.Absolute,
                    Complexity_Threshold          = 20,
                };

                PAN_Evaluator evaluator = new PAN_Evaluator(config.InputCount, PAN_Experiment.GetTrueVectors(config.InputCount, trueValues));

                _experiment = new PAN_Experiment(config.InputCount, trueValues);
                _experiment.Initialize("pick a number", config, evaluator);

                //_experiment.NeatGenomeParameters.InitialInterconnectionsProportion = .1;

                _ea = _experiment.CreateEvolutionAlgorithm();

                // Attach update event listener.
                _ea.UpdateEvent += EA_UpdateEvent;
                _ea.PausedEvent += EA_PausedEvent;

                ShowBestGenome();
                panPlot.ResetLabels(trueValues);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
예제 #4
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);
            }
        }