public override List <IIndividual> InitializePopulation() { var population = new List <IIndividual>(); var numCities = Problem.CityIds.Count; for (var i = 0; i < Parameters.PopulationSize; i++) { var randomRoadTaken = new List <int>(numCities); var mutableListCityIds = new List <int>(Problem.CityIds); for (var j = 0; j < numCities; j++) { var randomIndex = RandomNumGenerator.Next(0, mutableListCityIds.Count); var randomCityId = mutableListCityIds[randomIndex]; mutableListCityIds.RemoveAt(randomIndex); randomRoadTaken.Add(randomCityId); } var individual = new Ttp1Individual(randomRoadTaken); population.Add(individual); } return(population); }
protected override IEnumerable <Tuple <IIndividual, IIndividual> > SelectForCrossing( List <IIndividual> currentPopulation) { var numPairs = currentPopulation.Count / 2; var resultingPairs = new List <Tuple <IIndividual, IIndividual> >(); for (var i = 0; i < numPairs; i++) { var firstIndiv = TournamentSelect(currentPopulation) as Ttp1Individual; var secondIndiv = TournamentSelect(currentPopulation) as Ttp1Individual; if (firstIndiv == null || secondIndiv == null) { Console.WriteLine("Wrong type of individuals population passed to TTP1. It need TTP2 population."); return(null); } while (secondIndiv.RoadTaken.Equals(firstIndiv.RoadTaken)) { secondIndiv = currentPopulation[RandomNumGenerator.Next(0, currentPopulation.Count)] as Ttp1Individual; if (secondIndiv != null) { continue; } Console.WriteLine("Wrong type of individuals population passed to TTP1. It need TTP2 population."); return(null); } resultingPairs.Add(new Tuple <IIndividual, IIndividual>(firstIndiv, secondIndiv)); } return(resultingPairs); }
protected override IIndividual MutateIndividual(IIndividual indiv, bool guaranteedMutation = false) { if (!guaranteedMutation && !ShouldMutate()) { return(indiv); } if (!(indiv is Ttp1Individual indivTtp1)) { Console.WriteLine("Wrong type of individuals population passed to TTP1. It need TTP2 population."); return(null); } var firstRandomSwapIndex = RandomNumGenerator.Next(0, indivTtp1.RoadTaken.Count); var secondRandomSwapIndex = RandomNumGenerator.Next(0, indivTtp1.RoadTaken.Count); while (firstRandomSwapIndex == secondRandomSwapIndex) { secondRandomSwapIndex = RandomNumGenerator.Next(0, indivTtp1.RoadTaken.Count); } var tmp = indivTtp1.RoadTaken[firstRandomSwapIndex]; indivTtp1.RoadTaken[firstRandomSwapIndex] = indivTtp1.RoadTaken[secondRandomSwapIndex]; indivTtp1.RoadTaken[secondRandomSwapIndex] = tmp; return(indiv); }
public RosenbrockParticle(int lowerRange, int higherRange, int posClampVelocity, int negClampVelocity, int numberOfTimeSteps) { this.lowerRange = lowerRange; this.higherRange = higherRange; this.posClampVelocity = posClampVelocity; this.negClampVelocity = negClampVelocity; this.numberOfTimeSteps = numberOfTimeSteps; RandomNumGenerator rand = new RandomNumGenerator(); //initializing genes and velocity values for (int i = 0; i < 10; i++) { double number = rand.Generate_Double(this.lowerRange, this.higherRange); this.genes.Add(number); this.velocity.Add(0); } this.numberOfGenes = this.genes.Count; //initializing pbest list with the initial genes values for (int i = 0; i < this.numberOfGenes; i++) { this.pbest.Add(this.genes[i]); } //initializing bestFitness this.bestFitness = this.CalculateFitness(); }
//method to update linear increasing velocity public void Update_LinearIncreasingVelocity(Particle gbest, double wt) { double r1, r2, fi1, fi2; for (int i = 0; i < this.numberOfGenes; i++) { RandomNumGenerator generator = new RandomNumGenerator(); r1 = generator.Generate_Double(); r2 = generator.Generate_Double(); fi1 = 1.5 * r1 + 0.5; fi2 = 1.5 * r2 + 0.5; double factor1 = fi1 * (this.pbest[i] - this.genes[i]); double factor2 = fi2 * (gbest.genes[i] - this.genes[i]); double factor3 = this.velocity[i] * wt; this.velocity[i] = factor1 + factor2 + factor3; //complete equation for velocity update //this.velocity[i] = this.velocity[i] * wt + (fi1 * (this.pbest[i] - this.genes[i]) + fi2 * (gbest.genes[i] - this.genes[i])); //clamping positive velocity if it is out of range if (this.velocity[i] > this.posClampVelocity) { this.velocity[i] = this.posClampVelocity; } //clamping negative velocity if it is out of range if (this.velocity[i] < this.negClampVelocity) { this.velocity[i] = this.negClampVelocity; } } }
public void Update_LinearDecreasingVelocity(Particle gbest, double wt) { int c1 = 2; int c2 = 2; double r1, r2; for (int i = 0; i < this.numberOfGenes; i++) { RandomNumGenerator generator = new RandomNumGenerator(); r1 = generator.Generate_Double(); r2 = generator.Generate_Double(); this.velocity[i] = this.velocity[i] * wt + ((c1 * r1 * (this.pbest[i] - this.genes[i])) + (c2 * r2 * (gbest.genes[i] - this.genes[i]))); //clamping positive velocity if it is out of range if (this.velocity[i] > this.posClampVelocity) { this.velocity[i] = this.posClampVelocity; } //clamping negative velocity if it is out of range if (this.velocity[i] < this.negClampVelocity) { this.velocity[i] = this.negClampVelocity; } } }
private void Start() { generator = this.gameObject.GetComponent <RandomNumGenerator>(); rotation = this.gameObject.GetComponent <Rotation>(); defaultPositions = new Vector3[drops.Length]; for (int i = 0; i < drops.Length; i++) { defaultPositions[i] = drops[i].transform.localPosition; } numDrops = (int)numDropsSlider.value; }
private void btnUsePotion_Click(object sender, EventArgs e) { //Get elixir Elixir potion = (Elixir)comboBoxPotions.SelectedItem; player.CurrentPoints = (player.CurrentPoints + potion.AmountToHeal); if (player.CurrentPoints > player.MaximumPoints) { player.CurrentPoints = player.MaximumPoints; } //Remove elixir inventory foreach (Inventory item in player.Inventory) { if (item.Item.ID == potion.ID) { item.Quantity--; break; } } //Display message rtbMessages.Text += "You drink a " + potion.Name + Environment.NewLine; //Gremlin attacks int damageToPlayer = RandomNumGenerator.NumberBetween(0, currentMonster.MaximumDamage); rtbMessages.Text += "The " + currentMonster.Name + " did " + damageToPlayer.ToString() + " points of damage." + Environment.NewLine; player.CurrentPoints -= damageToPlayer; if (player.CurrentPoints <= 0) { rtbMessages.Text += "The " + currentMonster.Name + " killed you." + Environment.NewLine; //Move player back home MoveTo(Swamp.LocationByID(Swamp.LOCATION_ID_HOME)); } //Refresh data lblPoints.Text = player.CurrentPoints.ToString(); UpdateInventoryListInUI(); UpdatePotionListInUI(); }
public byte[] DrawNumbers() { byte[] allAvailableNumbers = CreateAllAvailableNumbers(allNumbersRange); byte[] drawnNumbers = new byte[selectedNumbersRange]; var selectionRange = allNumbersRange; for (int i = 0; i < selectedNumbersRange; i++) { byte randomIndex = RandomIndexGenerator((decimal)(selectionRange)); drawnNumbers[i] = allAvailableNumbers[randomIndex]; for (int j = randomIndex; j < allAvailableNumbers.Length - 1; j++) { allAvailableNumbers[j] = allAvailableNumbers[j + 1]; } selectionRange--; } RandomNumGenerator.Dispose(); return(drawnNumbers); }
private IIndividual CrossPmx(IIndividual parent1, IIndividual parent2) { var child = parent1.DeepCopy(); if (!(parent1 is Ttp1Individual parent1Ttp1) || !(parent2 is Ttp1Individual parent2Ttp1) || !(child is Ttp1Individual childTtp1)) { return(child); } if (parent1Ttp1.RoadTaken.Count != parent2Ttp1.RoadTaken.Count) { return(childTtp1); } var roadLength = parent1Ttp1.RoadTaken.Count; var road1 = parent1Ttp1.RoadTaken; var road2 = parent1Ttp1.RoadTaken; var firstChildFirstCutIndex = RandomNumGenerator.Next(0, roadLength); var firstChildSecondCutIndex = RandomNumGenerator.Next(firstChildFirstCutIndex, roadLength); var cutValuesFromIndiv1 = road1.GetRange(firstChildFirstCutIndex, firstChildSecondCutIndex - firstChildFirstCutIndex); var cutValuesFromIndiv2 = road2 .GetRange(firstChildFirstCutIndex, firstChildSecondCutIndex - firstChildFirstCutIndex) .Where(valueFromIndiv2 => !cutValuesFromIndiv1.Contains(valueFromIndiv2)).ToList(); foreach (var value in cutValuesFromIndiv2) { var currentValueFromIndiv2 = value; var valueAtTheSamePositionInIndiv1 = road1[road2.IndexOf(currentValueFromIndiv2)]; var indexOfThatValueInIndiv2 = road2.IndexOf(valueAtTheSamePositionInIndiv1); while (indexOfThatValueInIndiv2 >= firstChildFirstCutIndex || indexOfThatValueInIndiv2 < firstChildSecondCutIndex) { currentValueFromIndiv2 = road1[indexOfThatValueInIndiv2]; valueAtTheSamePositionInIndiv1 = road1[road2.IndexOf(currentValueFromIndiv2)]; indexOfThatValueInIndiv2 = road2.IndexOf(valueAtTheSamePositionInIndiv1); } childTtp1.RoadTaken[indexOfThatValueInIndiv2] = value; } var valuesLeftInIndiv2 = road2.Where(value => !cutValuesFromIndiv1.Contains(value) && !cutValuesFromIndiv2.Contains(value)).ToList(); var indexInIndiv1 = 0; foreach (var valueLeftInIndiv2 in valuesLeftInIndiv2) { var insertedValue = false; while (indexInIndiv1 < roadLength && !insertedValue) { var valueFromIndiv1 = childTtp1.RoadTaken[indexInIndiv1]; if (cutValuesFromIndiv1.Contains(valueFromIndiv1) || cutValuesFromIndiv2.Contains(valueFromIndiv1)) { indexInIndiv1++; continue; } childTtp1.RoadTaken[indexInIndiv1] = valueLeftInIndiv2; indexInIndiv1++; insertedValue = true; } } return(childTtp1); }
private void btnUseWeapon_Click(object sender, EventArgs e) { Weapon currentWeapon = (Weapon)comboBoxWeapons.SelectedItem; int damageToMonster = RandomNumGenerator.NumberBetween(currentWeapon.MinimumDamage, currentWeapon.MaximumDamage); currentMonster.CurrentPoints -= damageToMonster; rtbMessages.Text += "You hit the " + currentMonster.Name + " for " + damageToMonster.ToString() + " points." + Environment.NewLine; if (currentMonster.CurrentPoints <= 0) { //Gremlin is dead rtbMessages.Text += Environment.NewLine; rtbMessages.Text += "You defeated the " + currentMonster.Name + Environment.NewLine; player.ExperiencePoints += currentMonster.RewardExperiencePoints; rtbMessages.Text += "You receive " + currentMonster.RewardExperiencePoints.ToString() + " experience points" + Environment.NewLine; player.Gold += currentMonster.RewardGold; rtbMessages.Text += "You receive " + currentMonster.RewardGold.ToString() + " gold" + Environment.NewLine; List <Inventory> lootedItems = new List <Inventory>(); //Add items to the lootedItems list, comparing a random number to the drop percentage foreach (Treasure lootItem in currentMonster.TreasureChest) { if (RandomNumGenerator.NumberBetween(1, 100) <= lootItem.DropPercentage) { lootedItems.Add(new Inventory(lootItem.Item, 1)); } } //If no items were randomly selected, then add the default loot item(s). if (lootedItems.Count == 0) { foreach (Treasure lootItem in currentMonster.TreasureChest) { if (lootItem.IsDefaultItem) { lootedItems.Add(new Inventory(lootItem.Item, 1)); } } } // Add the looted items to the player's inventory foreach (Inventory inventoryItem in lootedItems) { player.AddItemToInventory(inventoryItem.Item); if (inventoryItem.Quantity == 1) { rtbMessages.Text += "You loot " + inventoryItem.Quantity.ToString() + " " + inventoryItem.Item.Name + Environment.NewLine; } else { rtbMessages.Text += "You loot " + inventoryItem.Quantity.ToString() + " " + inventoryItem.Item.NamePlural + Environment.NewLine; } } // Refresh player information and inventory controls lblPoints.Text = player.CurrentPoints.ToString(); lblGold.Text = player.Gold.ToString(); lblExperience.Text = player.ExperiencePoints.ToString(); lblLevel.Text = player.Level.ToString(); UpdateInventoryListInUI(); UpdateWeaponListInUI(); UpdatePotionListInUI(); // Add a blank line to the messages box, just for appearance. rtbMessages.Text += Environment.NewLine; //Move player to current location (to heal player and create a new monster to fight) MoveTo(player.CurrentLocation); } else { // Monster is still alive // Determine the amount of damage the monster does to the player int damageToPlayer = RandomNumGenerator.NumberBetween(0, currentMonster.MaximumDamage); // Display message rtbMessages.Text += "The " + currentMonster.Name + " did " + damageToPlayer.ToString() + " points of damage." + Environment.NewLine; // Subtract damage from player player.CurrentPoints -= damageToPlayer; // Refresh player data in UI lblPoints.Text = player.CurrentPoints.ToString(); if (player.CurrentPoints <= 0) { // Display message rtbMessages.Text += "The " + currentMonster.Name + " killed you." + Environment.NewLine; // Move player to "Home" MoveTo(Swamp.LocationByID(Swamp.LOCATION_ID_HOME)); } } }