Exemple #1
0
        public Evaluator3(WorldAccessor worldAccessor, TrainingRoom room, BotTrackingStorage log, ExperimentInitArgs_Activation activation, HyperNEAT_Args hyperneatArgs = null, double maxEvalTime = 15, double?initialRandomVelocity = null)
        {
            _worldAccessor         = worldAccessor;
            _room                  = room;
            _log                   = log;
            _activation            = activation;
            _hyperneatArgs         = hyperneatArgs;
            _maxEvalTime           = maxEvalTime;
            _initialRandomVelocity = initialRandomVelocity;

            // Set distances based on room size (or take as params?, or just a percent param?)
            double roomWidth = Math1D.Min
                               (
                room.AABB.Item2.X - room.AABB.Item1.X,
                room.AABB.Item2.Y - room.AABB.Item1.Y,
                room.AABB.Item2.Z - room.AABB.Item1.Z
                               );

            // Want radius, not diameter
            _roomRadius = roomWidth / 2;

            _maxDistance2 = _roomRadius * MULT_MAXDIST2;
            _maxDistance  = _roomRadius * MULT_MAXDIST;
            _minDistance  = _roomRadius * MULT_MINDIST;
        }
Exemple #2
0
        public Evaluator_WeaponSpin(WorldAccessor worldAccessor, TrainingRoom room, BotTrackingStorage log, ExperimentInitArgs_Activation activation, double weaponSpeed_Min, double weaponSpeed_Max, HyperNEAT_Args hyperneatArgs = null, double maxEvalTime = 15)
        {
            _worldAccessor = worldAccessor;
            _room          = room;
            _log           = log;
            _activation    = activation;
            _hyperneatArgs = hyperneatArgs;
            _maxEvalTime   = maxEvalTime;

            // Set distances based on room size (or take as params?, or just a percent param?)
            double roomWidth = Math1D.Min
                               (
                room.AABB.Item2.X - room.AABB.Item1.X,
                room.AABB.Item2.Y - room.AABB.Item1.Y,
                room.AABB.Item2.Z - room.AABB.Item1.Z
                               );

            // Want radius, not diameter
            _roomRadius = roomWidth / 2;

            _maxDistance2 = _roomRadius * MULT_MAXDIST2;
            _maxDistance  = _roomRadius * MULT_MAXDIST;

            // Weapon Speed
            _weaponSpeed_Min  = weaponSpeed_Min;
            _weaponSpeed_Max  = weaponSpeed_Max;
            _weaponSpeed_Max2 = _weaponSpeed_Max + ((_weaponSpeed_Max - _weaponSpeed_Min) / 2);
        }
Exemple #3
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;
        }
Exemple #4
0
        //TODO: In more complete evaluators, take in an arena manager that lets you check out a room.  That will return the bot/brain that was
        //created for that room (request a room each time StartNewEvaluation is called, or if no rooms available at that moment, each tick until
        //a room is free - then start evaluating from that moment)
        public Evaluator1_FAIL(Bot bot, BrainNEAT brainPart, SensorHoming[] homingParts, Point3D homePoint, ExperimentInitArgs_Activation activation, HyperNEAT_Args hyperneatArgs = null)
        {
            _bot         = bot;
            _brainPart   = brainPart;
            _homingParts = homingParts;

            _homePoint = homePoint;

            _activation    = activation;
            _hyperneatArgs = hyperneatArgs;

            foreach (SensorHoming homingPart in homingParts)
            {
                homingPart.HomePoint  = homePoint;
                homingPart.HomeRadius = _maxDistance * 1.25;
            }
        }
Exemple #5
0
        /// <summary>
        /// This replaces the internal neural net with a new one.  If the part is created with dna that has an internal NN, then there is
        /// no need to call this method
        /// </summary>
        /// <remarks>
        /// TODO: For hyperneat, see if genome is CPPN (it should be).  Then is activation cppn or final?
        /// </remarks>
        /// <param name="phenome">
        /// This is the actual neural net
        /// NOTE: If this is hyperneat, the phenome is still the final neural net, not the cppn
        /// </param>
        public void SetPhenome(IBlackBox phenome, NeatGenome genome, ExperimentInitArgs_Activation activation, HyperNEAT_Args hyperneatArgs = null)
        {
            if (phenome.InputCount != _neuronsInput.Length || phenome.OutputCount != _neuronsOutput.Length)
            {
                // Without this constraint, it would be difficult to map between external and phenome, because it can't be known
                // what the phenome's intended neuron positions would be.  It's expected that during training, a candidate phenome
                // will be placed into a BrainNEAT, then the bot will be tested and scored.  So that during training there is always a
                // 1:1 between external and phenome.  It's only future generations that mutate where a fuzzy mapping needs to be
                // done
                throw new ArgumentException(string.Format("The phenome passed in must have the same number of inputs and outputs as this wrapper brain's external inputs and outputs. Phenome ({0}, {1}), External ({2}, {3})", phenome.InputCount, phenome.OutputCount, _neuronsInput.Length, _neuronsOutput.Length));
            }

            _brain         = phenome;
            _genome        = genome;
            _activation    = activation;
            _hyperneatArgs = hyperneatArgs;

            _neatPositions_Input = _neuronsInput.
                                   Select(o => o.Position).
                                   ToArray();

            _neatPositions_Output = _neuronsOutput.
                                    Select(o => o.Position).
                                    ToArray();

            // It's a 1:1 mapping between external and internal
            _inputsMap = Enumerable.Range(0, _neuronsInput.Length).
                         Select(o => new NeuronMapping()
            {
                Index_External = o,
                Index_NEAT     = o,
                Weight         = 1d,
            }).
                         ToArray();

            _outputsMap = Enumerable.Range(0, _neuronsOutput.Length).
                          Select(o => new NeuronMapping()
            {
                Index_External = o,
                Index_NEAT     = o,
                Weight         = 1d,
            }).
                          ToArray();
        }
Exemple #6
0
        private void BuildNEATBrain(BrainNEATDNA dna)
        {
            var nn = DeserializeBrain(dna);

            if (nn == null)
            {
                return;
            }

            _inputsMap  = MapNeurons(_neuronsInput.Select(o => o.Position).ToArray(), dna.NEATPositions_Input);
            _outputsMap = MapNeurons(_neuronsOutput.Select(o => o.Position).ToArray(), dna.NEATPositions_Output);

            _neatPositions_Input  = dna.NEATPositions_Input;
            _neatPositions_Output = dna.NEATPositions_Output;

            _activation    = dna.Activation;
            _hyperneatArgs = dna.Hyper;

            _genome = nn.Value.genome;
            _brain  = nn.Value.phenome;
        }
Exemple #7
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);
            }
        }