Пример #1
0
        public void PopulationSelector_Ctor()
        {
            PopulationSelector selector = new PopulationSelector();

            Assert.Null(selector.SelectedPopulation);
            Assert.Equal(-1, selector.SelectedPopulationIndex);
            Assert.Null(selector.Environment);
        }
Пример #2
0
        public void PopulationSelector_OnEnvironmentChanged_NoPopulation()
        {
            PopulationSelector selector = new PopulationSelector
            {
                Environment = new GeneticEnvironment(Mock.Of <GeneticAlgorithm>())
            };

            Assert.Null(selector.SelectedPopulation);
        }
Пример #3
0
        public void PopulationSelector_SelectedPopulationIndexChanged_NoEnvironment()
        {
            PopulationSelector selector = new PopulationSelector
            {
                SelectedPopulationIndex = 1
            };

            Assert.Null(selector.SelectedPopulation);
        }
Пример #4
0
        public void Agent0x1Simulation_AgentTrajectories1()
        {
            Console.WriteLine("Agent0x1Simulation_AgentTrajectories1");
            LoggerInitialization.SetThreshold(typeof(sim_tests), LogLevel.Debug);
            LoggerInitialization.SetThreshold(typeof(Agent0x0), LogLevel.Info);

            int dim = 0;

            string []     names = new string [0];
            double []     mins  = new double [0];
            double []     maxs  = new double [0];
            IBlauSpace    s     = BlauSpace.create(dim, names, mins, maxs);
            IBlauPoint    mean  = new BlauPoint(s);
            IBlauPoint    std   = new BlauPoint(s);
            IDistribution d     = new Distribution_Gaussian(s, mean, std);

            IAgentFactory afact     = new Agent0x0_Factory(d);
            int           NUMAGENTS = 10;
            IPopulation   pop       = PopulationFactory.Instance().create(afact, NUMAGENTS);

            foreach (IAgent ag in pop)
            {
                SingletonLogger.Instance().DebugLog(typeof(metrics_tests), "agent: " + ag);
            }

            string PATH = "" + ApplicationConfig.EXECDIR + "orderbooks/orderbook.csv";
            AgentOrderbookLoader loader = MakeAgentOrderbookLoader(PATH);

            pop.addAgent(loader);

            IOrderbook_Observable ob = new Orderbook();

            // 1 hours
            ISimulation sim = new Simulation(pop, ob, 0.0, 3600.0);

            IAgent             agent = PopulationSelector.Select(pop);
            ITrajectoryFactory agTF  = new TrajectoryFactory_AgentOrders(agent, 10.0, 0.0);

            sim.add(agTF);
            ITrajectoryFactory agTF2 = new TrajectoryFactory_AgentNamedMetric(agent, "NetWorth", 10.0, 0.0);

            sim.add(agTF2);
            ITrajectoryFactory agTF3 = new TrajectoryFactory_AgentNamedMetric(agent, "NetWorth", 10.0, 0.99);

            sim.add(agTF3);

            SingletonLogger.Instance().DebugLog(typeof(sim_tests), "Running Simulation");
            ISimulationResults res = sim.run();

            SingletonLogger.Instance().DebugLog(typeof(sim_tests), "Stopping Simulation");

            SingletonLogger.Instance().DebugLog(typeof(sim_tests), "Results\n" + res.ToStringLong());
            Assert.AreEqual(res.Valid, true);
        }
Пример #5
0
        public void PopulationSelector_OnEnvironmentChanged_WithPopulation()
        {
            PopulationSelector selector = new PopulationSelector();

            GeneticEnvironment environment = new GeneticEnvironment(Mock.Of <GeneticAlgorithm>());
            Population         population  = Mock.Of <Population>();

            environment.Populations.Add(population);

            selector.Environment = environment;

            Assert.Same(population, selector.SelectedPopulation);
        }
Пример #6
0
        public void PopulationSelector_InitialPopulationAdded()
        {
            PopulationSelector selector = new PopulationSelector();

            GeneticEnvironment environment = new GeneticEnvironment(Mock.Of <GeneticAlgorithm>());

            selector.Environment = environment;

            Population population = Mock.Of <Population>();

            environment.Populations.Add(population);
            DispatcherHelper.DoEvents();

            Assert.Same(population, selector.SelectedPopulation);
        }
Пример #7
0
        public void PopulationSelector_SelectedPopulationRemoved()
        {
            PopulationSelector selector = new PopulationSelector();

            GeneticEnvironment environment = new GeneticEnvironment(Mock.Of <GeneticAlgorithm>());
            Population         population  = Mock.Of <Population>();

            environment.Populations.Add(population);

            selector.Environment = environment;

            environment.Populations.RemoveAt(0);
            DispatcherHelper.DoEvents();

            Assert.Null(selector.SelectedPopulation);
        }
Пример #8
0
        public void PopulationSelector_SelectedPopulationIndexChanged_GreaterThanAllowed()
        {
            PopulationSelector selector = new PopulationSelector();

            GeneticEnvironment environment = new GeneticEnvironment(Mock.Of <GeneticAlgorithm>());
            Population         population  = Mock.Of <Population>();

            environment.Populations.Add(population);

            selector.Environment = environment;

            Assert.Same(population, selector.SelectedPopulation);

            selector.SelectedPopulationIndex = 1;

            Assert.Null(selector.SelectedPopulation);
        }
Пример #9
0
        public void PopulationSelector_SelectedPopulationUpdatedOnRemoval_NonDefaultIndex()
        {
            PopulationSelector selector = new PopulationSelector();

            GeneticEnvironment environment = new GeneticEnvironment(Mock.Of <GeneticAlgorithm>());
            Population         population  = Mock.Of <Population>();

            environment.Populations.Add(population);
            Population population2 = Mock.Of <Population>();

            environment.Populations.Add(population2);

            selector.Environment             = environment;
            selector.SelectedPopulationIndex = 1;

            environment.Populations.RemoveAt(1);
            DispatcherHelper.DoEvents();

            Assert.Same(population, selector.SelectedPopulation);
        }
Пример #10
0
        public void PopulationSelector_OnEnvironmentChanged_RemoveEnvironment()
        {
            PopulationSelector selector = new PopulationSelector();

            GeneticEnvironment environment = new GeneticEnvironment(Mock.Of <GeneticAlgorithm>());
            Population         population  = Mock.Of <Population>();

            environment.Populations.Add(population);

            selector.Environment = environment;

            selector.Environment = null;

            Assert.Null(selector.SelectedPopulation);
            Assert.Equal(-1, selector.SelectedPopulationIndex);

            // Verify the control doesn't respond to a population being added to removed environment
            environment.Populations.Add(Mock.Of <Population>());

            Assert.Null(selector.SelectedPopulation);
            Assert.Equal(-1, selector.SelectedPopulationIndex);
        }
Пример #11
0
        public static void Main(string[] args)
        {
            Random             rand = new Random();
            ItemSetController  itemSetController  = new ItemSetController(rand, ITERATION_COUNT);
            PopulationSelector populationSelector = new PopulationSelector(1, 106, rand, ITERATION_COUNT);
            CrossoverManager   crossoverManager   = new CrossoverManager(rand, ITERATION_COUNT);
            SelectionManager   selectionManager   = new SelectionManager(rand);
            MutationManager    mutationManager    = new MutationManager(rand, ITERATION_COUNT);

            for (int i = 0; i < ITERATION_COUNT; i++)
            {
                itemSetController.addItem(new Item(rand.Next(1, 30), rand.Next(1, 30)), i);
            }

            itemSetController.ShowItems();
            itemSetController.IniSets();
            itemSetController.ShowSets();

            string pairSelection;

            do
            {
                Console.WriteLine("Выберите тип выборки пары: 1 - Лучшая пара 2 - Случайный");
                pairSelection = Console.ReadLine();
            } while (pairSelection != "1" && pairSelection != "2");

            string crossover;

            do
            {
                Console.WriteLine("Выберите вид кроссовера: 1 - Двухточечный(Случайный) 2 - Одноточечный классический");
                crossover = Console.ReadLine();
            } while (crossover != "1" && crossover != "2");

            string mutation;

            do
            {
                Console.WriteLine("Выберите вид мутации: 1 - изменеие бита на противоположный 2 - инверсия в случайном интервале");
                mutation = Console.ReadLine();
            } while (mutation != "1" && mutation != "2");

            string selection;

            do
            {
                Console.WriteLine("Выберите вид селекции: 1 - Бинарный турнир 2 - Пропорциональная");
                selection = Console.ReadLine();
            } while (selection != "1" && selection != "2");
            do
            {
                if (pairSelection == "1")
                {
                    populationSelector.ChooseBestPair(itemSetController.getSets());
                }
                else
                {
                    populationSelector.ChoosePair(itemSetController.getSets());
                }

                Set[] childrens    = new Set[CHILDS_COUNT];
                int   dividerPoint = rand.Next(1, ITERATION_COUNT - 1);
                Set[] bestPair     = populationSelector.getPair();
                for (int i = 0; i < childrens.Length; i++)
                {
                    childrens[i] = crossover == "1" ? crossoverManager.DoubleCross(bestPair, itemSetController.getItems()) :
                                   crossoverManager.ClassicCross(bestPair, itemSetController.getItems(), dividerPoint);
                    if (mutation == "1")
                    {
                        mutationManager.RandomMutation(childrens[i]);
                    }
                    else
                    {
                        mutationManager.InversionMutation(childrens[i]);
                    }
                }

                populationSelector.NewPopulation(itemSetController.getSets(), selection == "1" ?
                                                 selectionManager.BinaryTournament(childrens, CHILDS_COUNT / 2) :
                                                 selectionManager.ProportionallySelection(childrens, CHILDS_COUNT / 2));

                Console.WriteLine("Новая популяция:");
                itemSetController.ShowSets();

                populationSelector.ShowBestOf(itemSetController.getSets());
            } while (!populationSelector.End(itemSetController.getSets(), 85) && populationSelector.getAge() != 10);
            Console.ReadKey();
        }
Пример #12
0
 /// <summary>
 ///     Creates a new <see cref="RouletteWheelSelection{TProgram}" /> with the given arguments.
 /// </summary>
 /// <param name="selector">The scheme used to attribute proportion of selection.</param>
 /// <param name="fitnessFunction">The function used to evaluate the programs' fitness.</param>
 public RouletteWheelSelection(PopulationSelector selector, IFitnessFunction <TProgram> fitnessFunction)
 {
     this._fitnessFunction = fitnessFunction;
     this._selector        = selector;
 }
Пример #13
0
 public static MathExpressionTree FirstOrNew(this IEnumerable <MathExpressionTree> selected, PopulationSelector populationSelector)
 => selected.FirstOrDefault() ?? populationSelector.GenerateIndividual();