public Individual GenerateIndividual(List <Card> cardSet)
 {
     _individualsDispatched++;
     return(_individualsDispatched <= _params.Search.InitialPopulation ?
            Individual.GenerateRandomIndividual(cardSet) :
            _featureMap.GetRandomElite().Mutate());
 }
示例#2
0
        private void reset()
        {
            if (_featureMap.EliteMap.Count == 0)
            {
                _mean = LA.Vector <double> .Build.Dense(_numParams);
            }
            else
            {
                _mean = DenseVector.OfArray(_featureMap.GetRandomElite().ParamVector);
            }
            _direction = LA.Vector <double> .Build.Dense(_featureMap.NumFeatures);

            for (int i = 0; i < _featureMap.NumFeatures; i++)
            {
                _direction[i] = Sampler.gaussian() * _featureMap.GetFeatureScalar(i);
            }

            _mutationPower = _params.MutationPower;
            _pc            = LA.Vector <double> .Build.Dense(_numParams);

            _ps = LA.Vector <double> .Build.Dense(_numParams);

            _C = new DecompMatrix(_numParams);

            _individualsEvaluated = 0;
        }
示例#3
0
        public Individual GenerateIndividual()
        {
            _individualsDispatched += 1;
            if (_individualsDispatched <= _params.Search.InitialPopulation)
            {
                var ind = new Individual(_numParams);
                for (int i = 0; i < _numParams; i++)
                {
                    ind.ParamVector[i] = rnd.NextDouble() * 2 - 1;
                }
                return(ind);
            }

            Individual parent1 = _featureMap.GetRandomElite();
            Individual parent2 = _featureMap.GetRandomElite();
            var        child   = new Individual(_numParams);

            // Perform the line mutation
            double scalar1 = _params.Search.MutationPower;
            double scalar2 = _params.Search.MutationPower2;
            var    p1      = LA.Vector <double> .Build.DenseOfEnumerable(parent1.ParamVector);

            var p2 = LA.Vector <double> .Build.DenseOfEnumerable(parent2.ParamVector);

            var randomVector = MathNet.Numerics.LinearAlgebra.Vector <double>
                               .Build.Dense(_numParams, j => gaussian(scalar1));

            var lineVector = scalar2 * (p2 - p1) * gaussian(1.0);

            p1 += randomVector + lineVector;

            for (int i = 0; i < _numParams; i++)
            {
                child.ParamVector[i] = p1[i];
            }

            return(child);
        }
示例#4
0
        private void reset()
        {
            if (_featureMap.EliteMap.Count == 0)
            {
                _mean = LA.Vector <double> .Build.Dense(_numParams);
            }
            else
            {
                _mean = DenseVector.OfArray(_featureMap.GetRandomElite().ParamVector);
            }

            _mutationPower = _params.MutationPower;
            _pc            = LA.Vector <double> .Build.Dense(_numParams);

            _ps = LA.Vector <double> .Build.Dense(_numParams);

            _C = new DecompMatrix(_numParams);

            _individualsEvaluated = 0;
        }
        public Individual GenerateIndividual()
        {
            _individualsDispatched += 1;
            if (_individualsDispatched <= _params.Search.InitialPopulation)
            {
                var ind = new Individual(_numParams);
                for (int i = 0; i < _numParams; i++)
                {
                    ind.ParamVector[i] = rnd.NextDouble() * 2 - 1;
                }
                return(ind);
            }

            Individual parent = _featureMap.GetRandomElite();
            var        child  = new Individual(_numParams);
            double     scalar = _params.Search.MutationPower;

            for (int i = 0; i < _numParams; i++)
            {
                child.ParamVector[i] = gaussian(scalar) + parent.ParamVector[i];
            }
            return(child);
        }
示例#6
0
        public void Run()
        {
            _individualsEvaluated = 0;
            _maxWins        = 0;
            _maxFitness     = Int32.MinValue;
            _runningWorkers = new Queue <int>();
            _idleWorkers    = new Queue <int>();

            string boxesDirectory = "boxes/";
            string inboxTemplate  = boxesDirectory
                                    + "deck-{0,4:D4}-inbox.tml";
            string outboxTemplate = boxesDirectory
                                    + "deck-{0,4:D4}-outbox.tml";

            // Let the workers know we are here.
            string activeDirectory      = "active/";
            string activeWorkerTemplate = activeDirectory
                                          + "worker-{0,4:D4}.txt";
            string activeSearchPath = activeDirectory
                                      + "search.txt";

            using (FileStream ow = File.Open(activeSearchPath,
                                             FileMode.Create, FileAccess.Write, FileShare.None))
            {
                WriteText(ow, "MAP Elites");
                WriteText(ow, _configFilename);
                ow.Close();
            }

            Console.WriteLine("Begin search...");
            while (_individualsEvaluated < _params.NumToEvaluate)
            {
                // Look for new workers.
                string[] hailingFiles = Directory.GetFiles(activeDirectory);
                foreach (string activeFile in hailingFiles)
                {
                    string prefix = activeDirectory + "worker-";
                    if (activeFile.StartsWith(prefix))
                    {
                        string suffix   = ".txt";
                        int    start    = prefix.Length;
                        int    end      = activeFile.Length - suffix.Length;
                        string label    = activeFile.Substring(start, end - start);
                        int    workerId = Int32.Parse(label);
                        _idleWorkers.Enqueue(workerId);
                        _individualStable.Add(workerId, null);
                        File.Delete(activeFile);
                        Console.WriteLine("Found worker " + workerId);
                    }
                }

                // Dispatch jobs to the available workers.
                while (_idleWorkers.Count > 0)
                {
                    if (_individualsDispatched >= _params.InitialPopulation &&
                        _individualsEvaluated == 0)
                    {
                        break;
                    }

                    int workerId = _idleWorkers.Dequeue();
                    _runningWorkers.Enqueue(workerId);
                    Console.WriteLine("Starting worker: " + workerId);

                    Individual choiceIndividual =
                        _individualsDispatched < _params.InitialPopulation ?
                        Individual.GenerateRandomIndividual(_cardSet) :
                        _featureMap.GetRandomElite().Mutate();

                    string inboxPath = string.Format(inboxTemplate, workerId);
                    SendWork(inboxPath, choiceIndividual);
                    _individualStable[workerId] = choiceIndividual;
                    _individualsDispatched++;
                }

                // Look for individuals that are done.
                int numActiveWorkers = _runningWorkers.Count;
                for (int i = 0; i < numActiveWorkers; i++)
                {
                    int    workerId   = _runningWorkers.Dequeue();
                    string inboxPath  = string.Format(inboxTemplate, workerId);
                    string outboxPath = string.Format(outboxTemplate, workerId);

                    // Test if this worker is done.
                    if (File.Exists(outboxPath) && !File.Exists(inboxPath))
                    {
                        // Wait for the file to finish being written.
                        Console.WriteLine("Worker done: " + workerId);

                        ReceiveResults(outboxPath, _individualStable[workerId]);
                        _featureMap.Add(_individualStable[workerId]);
                        _idleWorkers.Enqueue(workerId);
                        _individualsEvaluated++;
                        _map_log.UpdateLog();
                    }
                    else
                    {
                        _runningWorkers.Enqueue(workerId);
                    }
                }

                Thread.Sleep(1000);
            }

            // Let the workers know that we are done.
            File.Delete(activeSearchPath);
        }