Example #1
0
        private static SetOfPlants TakeSomePlantsToAnalyze(SetOfPlants analyzedPlants, SetOfPlants poolOfPlantsToAnalyze)
        {
            var         availablePlants     = poolOfPlantsToAnalyze.OrderBy(p => CalculateScore(p)).ToList();
            SetOfPlants nextPlantsToAnalyze = new SetOfPlants();

            while (poolOfPlantsToAnalyze.Count() > 0 && nextPlantsToAnalyze.Count < Parameters.BatchSize)
            {
                var plant = poolOfPlantsToAnalyze.Last();
                poolOfPlantsToAnalyze.Remove(plant);
                availablePlants.RemoveAt(availablePlants.Count - 1);
                if (!analyzedPlants.Contains(plant))
                {
                    nextPlantsToAnalyze.Add(plant);
                }
            }
            return(nextPlantsToAnalyze);
        }
Example #2
0
        static void Main()
        {
            //var targetPlant = PlantFactory.Instance.GetRandomPlant();
            //Attention la fonction score est désormais codée en dur vis à vis de cette plante
            var targetPlant = new Plant(new Allele[] { Allele.Y, Allele.Y, Allele.Y, Allele.Y, Allele.G, Allele.G });

            var analyzedPlants = new SetOfPlants();

            for (int i = 0; i < Parameters.NbStartingPlants; i++)
            {
                Plant plant;
                do
                {
                    plant = PlantFactory.Instance.GetRandomPlant();
                } while (plant.IsSimilar(targetPlant));
                analyzedPlants.Add(plant);
            }

            Plant? bestPlantFound        = null;
            double bestCostFound         = int.MaxValue;
            var    poolOfPlantsToAnalyze = GenerateNewPlants(analyzedPlants);
            var    nextPlantsToAnalyze   = TakeSomePlantsToAnalyze(analyzedPlants, poolOfPlantsToAnalyze);

            while (nextPlantsToAnalyze.Count() != 0)
            {
                bool newBestPlantFound = false;
                if (bestPlantFound != null && bestPlantFound.ComputeCost() < bestCostFound)
                {
                    //Ce cas est faisable si on a amélioré les parents de bestplant
                    bestCostFound     = bestPlantFound.ComputeCost();
                    newBestPlantFound = true;
                }
                //TODO on recherche plusieurs fois dans poolofplantstoanalyze
                foreach (var plant in nextPlantsToAnalyze.Union(poolOfPlantsToAnalyze))
                {
                    if (plant.IsSimilar(targetPlant))
                    {
                        if (bestPlantFound == null || plant.ComputeCost() < bestCostFound)
                        {
                            Console.WriteLine("Nouvelle meilleure plante trouvée pour correspondre à la cible: " + targetPlant);
                            Console.WriteLine(plant.GenerateTree());

                            bestPlantFound    = plant;
                            bestCostFound     = plant.ComputeCost();
                            newBestPlantFound = true;
                        }
                    }
                }
                if (newBestPlantFound)
                {
                    nextPlantsToAnalyze.SetMaximumCost(bestCostFound);
                    analyzedPlants.SetMaximumCost(bestCostFound);
                    poolOfPlantsToAnalyze.SetMaximumCost(bestCostFound);
                }


                Console.WriteLine("Plantes analysées : " + analyzedPlants.Count());
                Console.WriteLine("Plantes à analyser : " + nextPlantsToAnalyze.Count());
                Console.WriteLine("Pool de plantes : " + poolOfPlantsToAnalyze.Count());

                poolOfPlantsToAnalyze.UnionWith(GenerateNewPlants(analyzedPlants, nextPlantsToAnalyze));
                analyzedPlants.UnionWith(nextPlantsToAnalyze);
                nextPlantsToAnalyze = TakeSomePlantsToAnalyze(analyzedPlants, poolOfPlantsToAnalyze);
            }
            Console.WriteLine();
            Console.WriteLine("Plus de nouvelles plantes!");

            Console.WriteLine("Meilleure plante=" + bestPlantFound);
            Console.WriteLine("Cout=" + bestCostFound);
            Console.WriteLine(bestPlantFound?.GenerateTree());
        }