private void Start() { entityManager = World.Active.EntityManager; Entity earthEntityPrefab = GameObjectConversionUtility.ConvertGameObjectHierarchy(earthPrefab, World.Active); Entity leafEntityPrefab = GameObjectConversionUtility.ConvertGameObjectHierarchy(leafPrefab, World.Active); Entity antEntityPrefab = GameObjectConversionUtility.ConvertGameObjectHierarchy(wormPrefab, World.Active); RandomSelection[] entityPrefabs = new RandomSelection[] { new RandomSelection(earthEntityPrefab, .3f), new RandomSelection(leafEntityPrefab, .7f), // new RandomSelection(antEntityPrefab, .1f) }; float3 startPoint = new float3(-(width / 2f) * cellScale, -(height / 2f) * cellScale, 0); //Create ants int antHomeRaidus = 5; int homeCenterX = UnityEngine.Random.Range(0, width); int homeCenterY = UnityEngine.Random.Range(0, height); for (int x = homeCenterX - antHomeRaidus; x < homeCenterX + antHomeRaidus; x++) { for (int y = homeCenterY - antHomeRaidus; y < homeCenterY + antHomeRaidus; y++) { if (Mathf.Sqrt(Mathf.Pow(x - homeCenterX, 2) + Mathf.Pow(y - homeCenterY, 2)) < antHomeRaidus) { float3 position = new float3(x * cellScale + startPoint.x, y * cellScale + startPoint.y, 0); Entity antEntityinstance = entityManager.Instantiate(antEntityPrefab); entityManager.SetComponentData(antEntityinstance, new GridPosition { Value = new int2(x, y) }); entityManager.SetComponentData(antEntityinstance, new Translation { Value = position }); } } } //Generate grid for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { float3 position = new float3(x * cellScale + startPoint.x, y * cellScale + startPoint.y, 0); Entity randomPrefab = GetRandomValue(entityPrefabs); Entity instance = entityManager.Instantiate(randomPrefab); entityManager.SetComponentData(instance, new GridPosition { Value = new int2(x, y) }); entityManager.SetComponentData(instance, new Translation { Value = position }); } } }
public void MudarOrdem() //Altera a ordem do surgimento das salas e dos Tiros { RandomSelection rs = gameObject.AddComponent <RandomSelection>() as RandomSelection; //Instanciar a classe "RandomSelection" em um objeto "RandomSelection" chamado rs rs.ListaRandomSemRepeticao(RandomSelection.ordemSalas, 2); //Cria a ordem em que as salas irão aparecer rs.ListaRandomSemRepeticao(RandomSelection.ordemTiros, 3); //Cria a ordem em que as tiros irão aparecer }
private CultureObject GetCultureToSpawn() { List <IFaction> factionsAtWar = new List <IFaction>(); foreach (IFaction faction in Campaign.Current.Factions) { if (Hero.MainHero.Clan.IsAtWarWith(faction) && !faction.IsBanditFaction) { factionsAtWar.Add(faction); } } if (factionsAtWar.Count == 0) { // The player isn't at war with anyone, we'll spawn bandits. List <Settlement> hideouts = Settlement.FindAll((s) => { return(s.IsHideout()); }).ToList(); Settlement closestHideout = hideouts.MinBy((s) => { return(MobileParty.MainParty.GetPosition().DistanceSquared(s.GetPosition())); }); return(closestHideout.Culture); } else { // Pick one of the factions to spawn prisoners of return(RandomSelection <IFaction> .GetRandomElement(factionsAtWar).Culture); } }
protected static void RandomSelection_LoadRandomness(RandomSelection __instance) { foreach (RandomListInfo info in RogueLibs.Instance.RandomLists) { __instance.CreateRandomList(info.name, info.category, info.objectType); } foreach (CustomItem item in RogueLibs.Instance.Items) { foreach (KeyValuePair <string, int> pair in item.SpawnDictionary) { RandomList list = null; foreach (RandomList l in __instance.randomListTable.Values) { if (l.rName == pair.Key) { list = l; break; } } if (list != null) { __instance.CreateRandomElement(list, item.Id, pair.Value); } } } }
private void Start() { for (int i = 0; i < enemyColumns; i++) { RandomSelection randomSelection = new RandomSelection(i, enemyData.spawnBigProbability); randomSelections.Add(randomSelection); } }
//Spin public void requestSpin() { m_model.lastWinner = RandomSelection.SelectWinner(m_model.m_chancesToWin); requestChangeCurrentCredits(-1 * m_model.CurrentBetAmount); calculateImagesForLastSpin(); calculatePayoutForLastSpin(); }
private void calculateImagesForLastSpin() { switch (m_model.lastWinner) { case 0: //no winner - no 3 of kind //Iffy way of doing this for (int i = 0; i < 3; i++) { switch (RandomSelection.GetRandomInt(1, 5)) { case 1: m_model.m_imagesToDisplay[i] = m_model.m_slotImages.ImageOne; break; case 2: m_model.m_imagesToDisplay[i] = m_model.m_slotImages.ImageTwo; break; case 3: m_model.m_imagesToDisplay[i] = m_model.m_slotImages.ImageThree; break; case 4: m_model.m_imagesToDisplay[i] = m_model.m_slotImages.ImageFour; break; } //All 3 match, reroll the last one. Should be ok to compare since they are references if (m_model.m_imagesToDisplay[0].Equals(m_model.m_imagesToDisplay[1]) && m_model.m_imagesToDisplay[0].Equals(m_model.m_imagesToDisplay[2])) { i--; } } break; case 1: //Image 1 won - 3 of kind m_model.m_imagesToDisplay[0] = m_model.m_slotImages.ImageOne; m_model.m_imagesToDisplay[1] = m_model.m_slotImages.ImageOne; m_model.m_imagesToDisplay[2] = m_model.m_slotImages.ImageOne; break; case 2: //Image 2 won - 3 of kind m_model.m_imagesToDisplay[0] = m_model.m_slotImages.ImageTwo; m_model.m_imagesToDisplay[1] = m_model.m_slotImages.ImageTwo; m_model.m_imagesToDisplay[2] = m_model.m_slotImages.ImageTwo; break; case 3: //Image 3 won - 3 of kind m_model.m_imagesToDisplay[0] = m_model.m_slotImages.ImageThree; m_model.m_imagesToDisplay[1] = m_model.m_slotImages.ImageThree; m_model.m_imagesToDisplay[2] = m_model.m_slotImages.ImageThree; break; case 4: //Image 4 won - 3 of kind m_model.m_imagesToDisplay[0] = m_model.m_slotImages.ImageFour; m_model.m_imagesToDisplay[1] = m_model.m_slotImages.ImageFour; m_model.m_imagesToDisplay[2] = m_model.m_slotImages.ImageFour; break; } }
public EvaluationTrain(GettingStarted gettingStarted) { _randomSelection = new RandomSelection(gettingStarted.EarlyPopulation); _rouletteWeelSelection = new RouletteWeel2Selection(); _crossOver = new CrossOver(gettingStarted.Items, gettingStarted.KnapsakCapacity); }
public override int GeneticAlgorithm(MainGraph graph, ProblemData problemData) { algorithmInfo = new AlgorithmInfo(); adjacencyList = AdjacencyList.GenerateList(graph); cost = Cost.GanerateCostArray(graph, problemData); problem = problemData; var crossover = GeneticMethod.ChosenCrossoverMethod(crossoverMethod, CrossoverProbability, dotCrossover); var mutation = GeneticMethod.ChosenMutationMethod(mutationMethod, MutationProbability, dotMutation); var selection = GeneticMethod.ChosenSelectionMethod(selectionMethod, CountTour, CountSelected); ReductionForClassicGA reduction = new ReductionForClassicGA(); Chromosome bestChromosome = null; Population startPopulation = new Population(PopulationSize, cost); var population = startPopulation; int stepGA = 0; double MediumFitness = Solution.MediumFitnessPopulation(population); RandomSelection randomSelection = new RandomSelection(); FitnessCalculation(population.populationList); stopwatch = new Stopwatch(); stopwatch.Start(); while (stepGA < IterateSize) { List <Chromosome> midPopulation = selection.Selection(population); List <Chromosome> parentPopulation = randomSelection.Selection(midPopulation, selection.indexSelectChrom); List <Chromosome> childList = crossover.Crossover(parentPopulation); if (childList.Count == 0) { continue; } mutation.Mutation(childList); FitnessCalculation(childList); reduction.Reduction(childList, randomSelection.indexTwoParant, population.populationList); double tempMediumFitness = Solution.MediumFitnessPopulation(population); double absFitness = Math.Abs(tempMediumFitness - MediumFitness); MediumFitness = tempMediumFitness; if (absFitness >= 0 && absFitness <= 1) { bestChromosome = population.BestChromosome(); var worstChromosome = population.WorstChromosome(); if (bestChromosome.fitness - worstChromosome.fitness <= 1 && bestChromosome.fitness - worstChromosome.fitness >= 0) { if (Solution.isAnswerTrue(bestChromosome, cost, problemData)) { stopwatch.Stop(); algorithmInfo.Time = stopwatch.Elapsed; algorithmInfo.BestFx = bestChromosome.fitness; algorithmInfo.Steps = stepGA; break; } } } stepGA++; } stopwatch.Stop(); if (stepGA == IterateSize) { bool answer = false; bestChromosome = population.BestChromosome(); while (population.populationList.Count != 0) { if (Solution.isAnswerTrue(bestChromosome, cost, problemData)) { algorithmInfo.Time = stopwatch.Elapsed; algorithmInfo.BestFx = bestChromosome.fitness; algorithmInfo.Steps = stepGA; answer = true; break; } else { population.populationList.Remove(bestChromosome); if (population.populationList.Count == 0) { break; } bestChromosome = population.BestChromosome(); } } if (!answer) { bestChromosome = null; } } return(Solution.Answer(cost, bestChromosome, problemData, graph)); }
/// <summary> /// Реализация генетического алгоритма "Genitor". /// </summary> /// <param name="graph">Граф.</param> /// <param name="problemData">Параметры задачи</param> public override int GeneticAlgorithm(MainGraph graph, ProblemData problemData) { this.algorithmInfo = new AlgorithmInfo(); // Инициализация основных структур. adjacencyList = AdjacencyList.GenerateList(graph); cost = Cost.GanerateCostArray(graph, problemData); ProblemData problem = problemData; var crossover = GeneticMethod.ChosenCrossoverMethod(crossoverMethod, 100, dotCrossover); var mutation = GeneticMethod.ChosenMutationMethod(mutationMethod, MutationProbability, dotMutation); var selection = GeneticMethod.ChosenSelectionMethod(selectionMethod, CountTour, CountSelected); Chromosome bestChromosome = null; double MediumFitness = 0; Population startPopulation = new Population(PopulationSize, cost); var population = startPopulation; int stepGA = 0; // вычесление пригодности хромосом. for (int i = 0; i < PopulationSize; i++) { population.populationList[i].fitness = Fitness.FunctionTrue(cost, problemData, population.populationList[i]); } RandomSelection randomSelection = new RandomSelection(); MediumFitness = Solution.MediumFitnessPopulation(population); stopwatch = new Stopwatch(); stopwatch.Start(); while (stepGA < IterateSize) { List <Chromosome> midPopulation = selection.Selection(population); List <Chromosome> parentPopulation = randomSelection.Selection(midPopulation); Chromosome child = crossover.Crossover(parentPopulation[0], parentPopulation[1]); if (mutation != null) { mutation.Mutation(child); } // вычесление ранга хромосомы. //population.Sort(); Ranking2(population); // пригодность потомка child.fitness = Fitness.FunctionTrue(cost, problemData, child); // поиск самой худщей хромосомы Chromosome bedChrom = null; bedChrom = population.populationList.First(); // замена худшей хромосомы на потомка int index = population.populationList.IndexOf(bedChrom); population.populationList.Insert(index, child); population.populationList.RemoveAt(index + 1); double tempMediumFitness = Solution.MediumFitnessPopulation(population); double absFitness = Math.Abs(tempMediumFitness - MediumFitness); MediumFitness = tempMediumFitness; if (absFitness >= 0 && absFitness <= 1) { bestChromosome = population.BestChromosome(); var worstChromosome = population.WorstChromosome(); if (bestChromosome.fitness - worstChromosome.fitness <= 1 && bestChromosome.fitness - worstChromosome.fitness >= 0) { if (Solution.isAnswerTrue(bestChromosome, cost, problemData)) { stopwatch.Stop(); algorithmInfo.Time = stopwatch.Elapsed; algorithmInfo.BestFx = bestChromosome.fitness; algorithmInfo.Steps = stepGA; break; } } } stepGA++; } stopwatch.Stop(); if (stepGA == IterateSize) { bool answer = false; bestChromosome = population.BestChromosome(); while (population.populationList.Count != 0) { if (Solution.isAnswerTrue(bestChromosome, cost, problemData)) { algorithmInfo.Time = stopwatch.Elapsed; algorithmInfo.BestFx = bestChromosome.fitness; algorithmInfo.Steps = stepGA; answer = true; break; } else { population.populationList.Remove(bestChromosome); if (population.populationList.Count == 0) { break; } bestChromosome = population.BestChromosome(); } } if (!answer) { bestChromosome = null; } } return(Solution.Answer(cost, bestChromosome, problemData, graph)); }