public double MeasureDiversity(SortList <AIPlayer> individuals) { int outputSize = individuals.Get(0).neuralNetwork.GetNumberOfOutputs(); double diversity = 0; for (int j = 0; j < runs; j++) { double[] randInputs = new double[individuals.Get(0).neuralNetwork.GetNumberOfInputs()]; for (int i = 0; i < randInputs.Length; i++) { randInputs[i] = RandomNum.RandomDouble(); } //We use a dictionary to keep track of the size of each specie we encounter Dictionary <int, int> counters = new Dictionary <int, int>(); for (int l = 0; l < individuals.Count; l++) { int specie = CalcSpecie(individuals.Get(l).GetOutputs(randInputs)); if (counters.Keys.Contains(specie)) { counters[specie]++; } else { counters.Add(specie, 1); } } //Use simpsons diversity diversity += CalcSimpsonsDiversity(counters); } return(diversity / runs); }
public void UpdateStatic() { Citizen[] list = Citizens.Get(); for (int i = 0; i < Citizens.Length; i++) { if (list[i].ID > Citizen.Index) { Citizen.Index = list[i].ID; } } }
public Person[] GetPossiblePersons() { SortList <Person> person = new SortList <Person>(); for (int i = 0; i < PossiblePersons.Count; i++) { person.Add(PossiblePersons[i], Position[i]); } return(person.Get()); }
public void TestSmallSingleList() { SortList <int> list = new SortList <int>(); for (int i = 0; i < 20; i++) { list.Clear(); for (int p = 0; p < i; p++) { list.Add(Utility.RandomNum.RandomInt(0, 1000)); } int last = Int32.MaxValue; for (int q = 0; q < i; q++) { Assert.IsTrue(list.Get(q) <= last); last = list.Get(q); } } }
public void TestBigSingleList() { SortList <int> list = new SortList <int>(); for (int i = 0; i < 1000; i++) { list.Add(Utility.RandomNum.RandomInt(0, 1000)); } Assert.AreEqual(1000, list.Count); int last = Int32.MaxValue; for (int i = 0; i < 1000; i++) { Assert.IsTrue(list.Get(i) <= last); last = list.Get(i); } list.Clear(); Assert.AreEqual(list.Count, 0); }
public void Merge(SortList <AIPlayer> individuals, List <AIPlayer> offspring, Simulation simulation) { //If having only a single parent, replace it if better foreach (AIPlayer o in offspring) { if (o.Parent2 == null) { for (int i = 0; i < individuals.Count; i++) { if (individuals.Get(i) == o.Parent1) { if (o.GetFitness() > individuals.Get(i).GetFitness()) { individuals.Remove(individuals.Get(i)); individuals.Add(o); } } } } else { //The offspring were made from 2 parents //If offspring is better than both parents, replace both parents and add a random AIPlayer bool betterThanParent1 = false; bool betterThanParent2 = false; for (int i = 0; i < individuals.Count; i++) { if (individuals.Get(i) == o.Parent1 && individuals.Get(i).GetFitness() < o.Parent1.GetFitness()) { betterThanParent1 = true; } if (individuals.Get(i) == o.Parent2 && individuals.Get(i).GetFitness() < o.Parent2.GetFitness()) { betterThanParent2 = true; } } //if better than both parents, remove both parents and add a random immigrant if (betterThanParent1 && betterThanParent2) { individuals.Remove(o.Parent1); individuals.Remove(o.Parent2); individuals.Add(o); AIPlayer randomImmagrant = new AIPlayer(simulation.NeuralNetworkMaker); randomImmagrant.CalcFitness(simulation.Game); individuals.Add(randomImmagrant); } } } individuals.Crop(individuals.Count / 2); while (individuals.Count < simulation.PopulationSize) { AIPlayer randomImmagrant = new AIPlayer(simulation.NeuralNetworkMaker); randomImmagrant.CalcFitness(simulation.Game); individuals.Add(randomImmagrant); } }
private void LoadParties() { int tmp = 0; foreach (Party party in Parties.Get()) { if (party.ID > tmp) { tmp = party.ID; } } Party.Index = tmp; }
public void RemoveProfession(Type type) { Profession pro = null; foreach (Profession profession in Professions.Get()) if (profession.GetType() == type) pro = profession; if (pro == null) return; pro.RemoveEffect(); Professions.Remove(pro.Importance); if (MainProfession == pro) { if (Professions.Length == 0) MainProfession = null; Profession[] un = Professions.Get(); List<Profession> list = un.ToList(); list.Reverse(); MainProfession = list[0]; } }
public void Update() { Current.Number++; //- Battles Log.Write("Checking Battles.."); foreach (Tile tile in Tiles.Get()) { tile.Update(); } //- Processes BuildProcess[] now_finished = EveryProcess.FindEveryUnefficient(Current.Number); if (now_finished.Length != 0) { Log.Write("Dealing with Finished Processes.."); foreach (BuildProcess process in now_finished) { process.Done(); } } EveryProcess = EveryProcess.RemoveEveryUnefficient(Current.Number); //- Events foreach (Event event_type in EventTypes) { if (event_type is PlayerEvent player_event) { foreach (Citizen person in Citizens.Get()) { if (person is Player player) { PlayerEvent local = Engine.CreateClass <PlayerEvent>(player_event.GetType()); local.Init(player); if (local.Condition()) { local.Fire(); Log.Write("Fire"); } } } } } Unit.HasMoved.Clear(); Engine.Map.ChangeMapMode(Engine.Map.CurrentMapMode); Engine.UpdateEverySubWindow(); }
//- Actions private void LoadTabActions() { img_action_header.Source = Images.IconQuestionmark; tree_actions.Items.Clear(); SortList <FPAction> legit = new SortList <FPAction>(); foreach (Type action_type in Engine.Game.EveryAction) { object[] args = new object[] { Engine.CurrentPerson, Person }; FPAction fp_action = Engine.CreateClass <FPAction>(action_type, args); if (fp_action.Condition()) { legit.Add(fp_action, fp_action.IndexPosition); } } foreach (FPAction action in legit.Get()) { AddAction(action); } }
public MilitaryRank[] GetRanks() => MilitaryRanks.Get();
//A weighted random selection of an individual based on the rank of each individual (least fitness has rank 1, greatest fitness has rank n) private AIPlayer SelectIndividualRankBased() { RankMethod rankMethod = new LinearRankMethod(); return(individuals.Get(rankMethod.GetRandomIndex(individuals.Count))); }