Ejemplo n.º 1
0
        public PeaResult Start(PeaSettings settings, IEvaluationInitData initData)
        {
            var       inbox  = Inbox.Create(System);
            PeaResult result = null;

            try
            {
                inbox.Send(SystemActor, new CreateSystem(settings));
                var response = inbox.Receive(TimeSpan.FromSeconds(30));
                if (response is CreatedSuccessfully)
                {
                    inbox.Send(SystemActor, new InitEvaluator(initData));
                    result = (PeaResult)inbox.Receive(TimeSpan.FromMinutes(30));
                }
            }
            catch (Exception e)
            {
                result = new PeaResult(new List <string>()
                {
                    e.ToString()
                }, new List <IEntity>());
            }


            //var response = await SystemActor.Ask(new CreateSystem(settings));

            //if (response is CreatedSuccessfully)
            //{
            //result = await SystemActor.Ask(new InitEvaluator(initData));
            //}

            return(result);
        }
Ejemplo n.º 2
0
        private void CreateIslands(PeaSettings settings)
        {
            var key          = new string[settings.SubProblemList.Count];
            var parameterSet = new ParameterSet();

            parameterSet.SetValueRange(settings.ParameterSet);
            for (int i = 0; i < settings.SubProblemList.Count; i++)
            {
                key[i] = settings.SubProblemList[i].Encoding.Key;
                parameterSet.SetValueRange(settings.SubProblemList[i].ParameterSet);
            }

            var random = (IRandom)Activator.CreateInstance(Settings.Random, settings.Seed);

            //foreach (var subProblem in settings.SubProblemList)
            //{
            //    string key = subProblem.Encoding.Key;
            //var parameterSet = new ParameterSet(subProblem.ParameterSet);

            int islandsCount = parameterSet.GetInt(ParameterNames.IslandsCount);

            Islands = new List <IActorRef>(islandsCount);
            for (int i = 0; i < islandsCount; i++)
            {
                int seed = random.GetInt(0, Int32.MaxValue);

                var islandSettings = settings.GetIslandSettings(new MultiKey(key));
                var islandProps    = IslandActor.CreateProps(islandSettings, seed);
                var actorRef       = Context.ActorOf(islandProps);
                Islands.Add(actorRef);
                IslandsCount++;
            }
            //}
        }
Ejemplo n.º 3
0
        private void CreateSystem(PeaSettings settings)
        {
            _starter = Sender;

            var parameterSet      = new ParameterSet(settings.ParameterSet);
            int archipelagosCount = parameterSet.GetInt(ParameterNames.ArchipelagosCount);
            int islandsCount      = parameterSet.GetInt(ParameterNames.IslandsCount);

            ResultsWaitForCount = archipelagosCount * islandsCount;

            Archipelagos = new List <IActorRef>(archipelagosCount);

            for (int a = 0; a < archipelagosCount; a++)
            {
                var archipelagoProps = ArchipelagoActor.CreateProps(settings);
                var actorRef         = Context.ActorOf(archipelagoProps);
                Archipelagos.Add(actorRef);
                ArchipelagosCount++;
            }

            var fitness = (IFitnessFactory)Activator.CreateInstance(settings.Fitness);

            FitnessComparer = fitness.GetFitnessComparer();

            AddCallbackEvents(settings.NewEntityMergedToBest);
        }
Ejemplo n.º 4
0
        private static ParameterSet CreateParameters(PeaSettings settings)
        {
            var parameterSet = new ParameterSet(settings.ParameterSet);

            foreach (var subProblem in settings.SubProblemList)
            {
                parameterSet.SetValueRange(subProblem.ParameterSet);
            }
            return(parameterSet);
        }
Ejemplo n.º 5
0
        public ArchipelagoActor(PeaSettings settings)
        {
            Settings = settings;
            CreateIslands(Settings);
            //TODO: Stacking, Become counting
            Receive <CreatedSuccessfully>(m => CountCreatedIslands());

            Receive <InitEvaluator>(m => InitIslands(m));

            Receive <Travel>(m => Transit(m));

            Receive <PeaResult>(m => MergeResults(m));
        }
Ejemplo n.º 6
0
        public static IslandEngine Create(MultiKey islandKey, PeaSettings settings, int seed)
        {
            if (seed == 0)
            {
                seed = islandKey.GetHashCode() + Environment.TickCount;
            }

            var random       = (IRandom)Activator.CreateInstance(settings.Random, seed);
            var parameterSet = CreateParameters(settings);

            var fitness         = (IFitnessFactory)Activator.CreateInstance(settings.Fitness);
            var fitnessComparer = fitness.GetFitnessComparer();

            var engine = new IslandEngine()
            {
                Random     = random,
                Settings   = settings,
                Parameters = parameterSet
            };

            var algorithm         = CreateAlgorithm(engine, settings);
            var conflictDetectors = CreateConflictDetectors(settings.SubProblemList);

            var chromosomeFactories = CreateChromosomeFactories(engine, settings, conflictDetectors, random);
            var defaultCreator      = new EntityCreator(settings.EntityType, chromosomeFactories, random);

            engine.EntityCreators = CreateEntityCreators(settings.SubProblemList, defaultCreator, random);

            IMigrationStrategy migrationStrategy = CreateMigrationStrategy(engine, random, fitnessComparer, parameterSet, settings);

            engine.Algorithm         = algorithm.GetAlgorithm(engine);
            engine.FitnessComparer   = fitnessComparer;
            engine.ConflictDetectors = conflictDetectors;
            engine.Selections        = CreateSelections(algorithm, settings, parameterSet, random, fitnessComparer);
            engine.Replacements      = CreateReinsertions(algorithm, settings, parameterSet, random, fitnessComparer);
            engine.MigrationStrategy = migrationStrategy;

            engine.Reduction = new Population.Reduction.CleanOutTournamentLosers(random, parameterSet);
            //engine.Reduction = new Population.Reduction.DoNothingReduction();

            engine.Parameters.SetValueRange(algorithm.GetParameters());

            engine.EntityMutation  = new EntityMutation(chromosomeFactories, random);
            engine.EntityCrossover = new EntityCrossover(chromosomeFactories, random);
            engine.StopCriteria    = settings.StopCriteria;

            return(engine);
        }
Ejemplo n.º 7
0
        public PeaResult Run(PeaSettings settings, IEvaluationInitData initData, LaunchTravelersDelegate launchTravelers = null)
        {
            string[] keys = new string[settings.SubProblemList.Count];
            for (int i = 0; i < settings.SubProblemList.Count; i++)
            {
                keys[i] = settings.SubProblemList[i].Encoding.Key;
            }
            Key = new MultiKey(keys);

            var islandEngine = IslandEngineFactory.Create(Key, settings, settings.Seed);

            AddCallbackEvents(islandEngine, settings.NewEntityMergedToBest);

            Evaluator = (EvaluationBase)TypeLoader.CreateInstance(settings.Evaluation, settings.ParameterSet);
            Evaluator.Init(initData);

            islandEngine.Algorithm.SetEvaluationCallback(Evaluate);
            islandEngine.Init(initData);
            if (launchTravelers != null)
            {
                islandEngine.LaunchTravelers += launchTravelers;
            }

            var          c = 0;
            StopDecision stopDecision;

            while (true)
            {
                islandEngine.Algorithm.RunOnce();
                stopDecision = islandEngine.StopCriteria.MakeDecision(islandEngine, islandEngine.Algorithm.Population);
                if (stopDecision.MustStop)
                {
                    Debug.WriteLine(stopDecision.Reasons[0]);
                    break;
                }
                c++;
            }

            return(new PeaResult(stopDecision.Reasons, islandEngine.Algorithm.Population.Bests));
        }
Ejemplo n.º 8
0
 public CreateSystem(PeaSettings settings)
 {
     Settings = settings;
 }
Ejemplo n.º 9
0
        public static Props CreateProps(PeaSettings settings)
        {
            var props = Props.Create(() => new ArchipelagoActor(settings));

            return(props);
        }
Ejemplo n.º 10
0
        private static IDictionary <string, IChromosomeFactory> CreateChromosomeFactories(IEngine engine, PeaSettings settings, IDictionary <string, IList <IConflictDetector> > conflictDetectors, IRandom random)
        {
            var factories = new Dictionary <string, IChromosomeFactory>();

            foreach (var subProblem in settings.SubProblemList)
            {
                var parameterSet    = new ParameterSet(subProblem.ParameterSet);
                var factoryInstance = Activator.CreateInstance(subProblem.Encoding.ChromosomeType, random,
                                                               parameterSet, conflictDetectors[subProblem.Encoding.Key]) as IChromosomeFactory;

                factoryInstance.Apply(engine);

                factories.Add(subProblem.Encoding.Key, factoryInstance);
            }

            return(factories);
        }
Ejemplo n.º 11
0
        private static IProvider <IReplacement> CreateReinsertions(IAlgorithmFactory algorithm, PeaSettings settings, ParameterSet parameterSet, IRandom random, IFitnessComparer fitnessComparer)
        {
            var reinsertions = algorithm.GetReinsertions();

            //TODO: impement override by settings


            var reinsertionProvider = CreateProvider <IReplacement>(reinsertions.Count, random);

            foreach (var reinsertion in reinsertions)
            {
                var reinsertionInstance = (IReplacement)Activator.CreateInstance(reinsertion, random, fitnessComparer, parameterSet);
                reinsertionProvider.Add(reinsertionInstance, 1.0);
            }

            return(reinsertionProvider);
        }
Ejemplo n.º 12
0
        public static IProvider <ISelection> CreateSelections(IAlgorithmFactory algorithm, PeaSettings settings, ParameterSet parameterSet, IRandom random, IFitnessComparer fitnessComparer)
        {
            var selections = algorithm.GetSelections();

            //TODO: impement override by settings

            var selectionProvider = CreateProvider <ISelection>(selections.Count, random);

            foreach (var selectionType in selections)
            {
                var selectionInstance = (ISelection)Activator.CreateInstance(selectionType, random, fitnessComparer, parameterSet);
                selectionProvider.Add(selectionInstance, 1.0);
            }

            return(selectionProvider);
        }
Ejemplo n.º 13
0
        private static IMigrationStrategy CreateMigrationStrategy(IslandEngine engine, IRandom random, IFitnessComparer fitnessComparer, ParameterSet parameters, PeaSettings settings)
        {
            //TODO: MigrationFactory!
            var selection   = new TournamentSelection(random, fitnessComparer, parameters);
            var replacement = new ReplaceWorstEntitiesOfPopulation(random, fitnessComparer, parameters);
            var strategy    = new Migration.Implementation.MigrationStrategy(random, selection, replacement, engine.Parameters);

            strategy.Parameters.SetValue(Migration.ParameterNames.MigrationReceptionRate, 0.01);
            return(strategy);
        }
Ejemplo n.º 14
0
 private static IAlgorithmFactory CreateAlgorithm(IEngine engine, PeaSettings settings)
 {
     //TODO: choose first matching algorithm type from chromosomeFactories, or apply settings
     return(new Algorithm.SteadyState());
 }
Ejemplo n.º 15
0
        public static IslandEngine Create(string islandName, PeaSettings settings, int seed)
        {
            var islandKey = new MultiKey(islandName);

            return(Create(islandKey, settings, seed));
        }