示例#1
0
        public double CountFitness(Team team, int seed, bool verbose = false)
        {
            Random r = new Random(seed);

            var fitness = 0.0;

            for (int i = 0; i < parameters.PositionsOfShepherdsSet.Count; i++)
            {
                var seedForSheep = r.Next();

                var sheep = AgentFactory.GetSheep(parameters.PositionsOfSheepSet[i], parameters.SheepType, seedForSheep);

                team.ClearPath();
                team.SetPositions(parameters.PositionsOfShepherdsSet[i]);

                var countFitness = CountFitness(team, sheep);

                if (verbose)
                {
                    Log(i, countFitness, seedForSheep, parameters.PositionsOfShepherdsSet[i], parameters.PositionsOfSheepSet[i]);
                }

                if (!double.IsNaN(fitness))
                {
                    fitness += countFitness;
                }
            }

            return(fitness);
        }
示例#2
0
        private void StartHerding()
        {
            ButtonPause.Enabled  = true;
            ButtonResume.Enabled = false;

            ButtonStepBack.Enabled    = false;
            ButtonStepForward.Enabled = false;

            TransformPositions();
            var parameters = (OptimizationParameters) new XmlSerializer(typeof(OptimizationParameters)).Deserialize(new StringReader(richTextBoxParameters.Text));

            if (checkBoxRandom.Checked)
            {
                parameters.PositionsOfShepherds = parameters.PositionsOfShepherds.Randomize(0, 400).ToList();
                parameters.PositionsOfSheep     = parameters.PositionsOfSheep.Randomize(0, 400).ToList();
            }

            var sheep = AgentFactory.GetSheep(parameters.PositionsOfSheep, parameters.SheepType, parameters.SeedForRandomSheepForBest);

            var shepherds = richTextBoxShepherds.Text.Split('\n')
                            .Where(x => string.IsNullOrEmpty(x) == false)
                            .Select(x => repository.LoadShepherd(x));

            var team = TeamFactory.GetNotIdenticalTeam(shepherds.Cast <ThinkingAgent>().ToList());

            team.Resize(parameters.PositionsOfShepherds.Count());
            team.SetPositions(parameters.PositionsOfShepherds);

            world = new ViewableWorld(
                HerdingX,
                HerdingY,
                checkBoxAnimationMode.Checked ? MilisecondsBetweenSteps : 0,
                team,
                sheep);

            world.Start(parameters.TurnsOfHerding);

            paint = true;
        }