Esempio n. 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;
        }
Esempio n. 2
0
        public AntPos_Evaluator(HarnessArgs harnessArgs, EvaluatorArgs evalArgs)
        {
            lock (_lock)
            {
                _harnessArgs = harnessArgs;
            }

            _evalArgs = evalArgs;
        }
Esempio n. 3
0
 public static TrackedItemBase GetNewItem(TrackedItemHarness harness, EvaluatorArgs evalArgs)
 {
     if (evalArgs.NewItemStart != null)
     {
         var choice = evalArgs.NewItemStart[StaticRandom.Next(evalArgs.NewItemStart.Length)];
         return(GetNewItem_Predefined(harness, choice.Item1, choice.Item2, choice.Item3, choice.Item4));
     }
     else
     {
         return(GetNewItem_Random(harness, evalArgs.ItemTypes[StaticRandom.Next(evalArgs.ItemTypes.Length)], evalArgs.BounceOffWalls, evalArgs.MaxSpeed));
     }
 }
Esempio n. 4
0
        private static double[] GetExpectedOutput_Matched(Tuple <TrackedItemBase, Point, Vector> position, TrackedItemHarness harness, EvaluatorArgs evaluatorArgs, double?additionalErrorMult = null)
        {
            //TODO: Pull the sqrt out of this method
            // This is the smallest radius that will fit in the grid's pixels.  If the dot is smaller than this, then it could nearly dissapear if the
            // center is between sample points
            double minDotRadius = (harness.OutputSize / harness.OutputSizeXY) * Math.Sqrt(2);

            // Convert error into real world distance: item's max speed * avg tick time
            double errorRadius = position.Item1.MaxPositionError + (additionalErrorMult ?? 0);

            errorRadius *= evaluatorArgs.MaxDistancePerTick;

            // Get the final dot radius
            double dotRadius = Math.Max(minDotRadius, errorRadius);

            // Draw onto the array
            double[] retVal = new double[harness.OutputSizeXY * harness.OutputSizeXY];
            ApplyPoint(retVal, harness.OutputCellCenters, dotRadius, position.Item2, true);

            return(retVal);
        }
Esempio n. 5
0
        public static double[] GetExpectedOutput(Tuple <TrackedItemBase, Point, Vector> currentPos, TrackedItemHarness harness, EvaluatorArgs evaluatorArgs)
        {
            const int NUMFRAMES = 2;

            double[] retVal = null;

            if (currentPos == null)
            {
                return(GetExpectedOutput_Zeros(harness));
            }
            else if (harness.Time - currentPos.Item1.CreateTime - harness.DelayBetweenInstances < evaluatorArgs.Delay_Seconds + (evaluatorArgs.ElapsedTime_Seconds * NUMFRAMES))
            {
                // Can't show the current's position yet, because previous doesn't exist yet
                return(GetExpectedOutput_Zeros(harness));
            }
            else
            {
                retVal = GetExpectedOutput_Matched(currentPos, harness, evaluatorArgs);
            }

            return(retVal);
        }
Esempio n. 6
0
        public static double[] GetExpectedOutput_OLD2(Tuple <TrackedItemBase, Point, Vector> currentPos, TrackedItemHarness harness, EvaluatorArgs evaluatorArgs)
        {
            // This is too simplistic.  It draws current before previous even exists

            double[] retVal = null;

            if (currentPos == null)
            {
                return(GetExpectedOutput_Zeros(harness));
            }
            else
            {
                retVal = GetExpectedOutput_Matched(currentPos, harness, evaluatorArgs);
            }

            return(retVal);
        }
Esempio n. 7
0
        public static double[] GetExpectedOutput_OLD1(Tuple <TrackedItemBase, Point, Vector> prevPos, Tuple <TrackedItemBase, Point, Vector> currentPos, TrackedItemHarness harness, EvaluatorArgs evaluatorArgs)
        {
            // This is too complicated

            double[] retVal = null;

            if (prevPos == null)
            {
                //If prev is null, then output should be zeros
                retVal = GetExpectedOutput_Zeros(harness);
            }
            else if (currentPos == null || currentPos.Item1.Token != prevPos.Item1.Token)
            {
                //If prev and current tokens don't match, then????
                //desiredOutput = GetError_Unmatched(outputArr);        // might not even want to add to the error
            }
            else if (harness.Time - prevPos.Item1.CreateTime < evaluatorArgs.NewItem_Duration_Multiplier * evaluatorArgs.Delay_Seconds)
            {
                //If prev is new, then error size gradient
                double aliveTime = harness.Time - prevPos.Item1.CreateTime;                                               // figure out how long it's been alive
                double percent   = aliveTime / (evaluatorArgs.NewItem_Duration_Multiplier * evaluatorArgs.Delay_Seconds); // get the percent of the range
                percent = UtilityCore.Cap(1 - percent, 0, 1);                                                             // invert the percent so that an old percent of zero will now be the full error mult
                double newItemMult = percent * evaluatorArgs.NewItem_ErrorMultiplier;

                retVal = GetExpectedOutput_Matched(currentPos, harness, evaluatorArgs, newItemMult);
            }
            else
            {
                //otherwise, standard scoring
                retVal = GetExpectedOutput_Matched(currentPos, harness, evaluatorArgs);
            }

            return(retVal);
        }
Esempio n. 8
0
        public static void GetPrevCurrentPositions(out Tuple <TrackedItemBase, Point, Vector> prevPos, out Tuple <TrackedItemBase, Point, Vector> currentPos, TrackedItemHarness harness, EvaluatorArgs evaluatorArgs)
        {
            TrackedItemBase item = harness.Item;

            currentPos = null;
            if (item != null)
            {
                currentPos = Tuple.Create(item, item.Position, item.Velocity);
            }

            prevPos = harness.GetPreviousPosition(harness.Time - evaluatorArgs.Delay_Seconds);
        }
Esempio n. 9
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);
            }
        }