public override Genome EvolveSpecie() { StartFinding(); BestCandidate = new Equation(EInfo, Randomizer); do { ResetSingle(BestCandidate); RandomCand.MakeRandomEquation(BestCandidate); BestCandidate.CalcTotalOffSet(); } while (!Tools.IsANumber(BestCandidate.OffSet)); Equation EvolvedEquation = new Equation(EInfo, Randomizer) { OffSet = float.NaN }; Equation OldEquation = new Equation(EInfo, Randomizer) { OffSet = float.NaN }; bool BestCandEvolved = false; _toCalc = EInfo.coordInfo.expectedResults.Length; while (_toCalc <= EInfo.coordInfo.expectedResults.Length) { int StuckCounter = 0; do { BestCandEvolved = GetNextGen(EvolvedEquation, OldEquation, _toCalc); StuckCounter = SetStuckCounter(StuckCounter, BestCandEvolved); UpdateInfo(); SpecEnviroment.CheckBestCandidate(this.SpecInfo.GetCopy()); //} while (StuckCounter <= EInfo.MaxStuckGens && BestCandidate.OffSet != 0); } while (StuckCounter <= EInfo.MaxStuckGens); break; //_toCalc++; //BestCandidate.OffSet = double.MaxValue; } return(this); }
public override void SimulateEnviroment() { ConcurrentQueue <Family> families = new ConcurrentQueue <Family>(); ConcurrentQueue <Equation> parents = new ConcurrentQueue <Equation>(); for (int i = 0; i < EInfo.SpeciesAmount; i++) { Family newFamily = new Family(EInfo, PARENT_COUNT); for (int y = 0; y < PARENT_COUNT; y++) { parents.Enqueue(newFamily.parents[y]); newFamily.parents[y] = null; } families.Enqueue(newFamily); } while (true) { ConcurrentQueue <Family> processedFamilies = new ConcurrentQueue <Family>(); foreach (Family family in families) { for (int i = 0; i < PARENT_COUNT; i++) { Equation parent = null; if (!parents.TryDequeue(out parent)) { throw new NullReferenceException("parent was null"); } family.parents[i] = parent; } } Exception error = null; Parallel.For(0, EInfo.SpeciesAmount, (z, loopState) => { try { Family family = null; if (!families.TryDequeue(out family)) { throw new NullReferenceException("family was null"); } Species[z].EvolveFamily(family); Species[z].BestCandidate.Cleanup(); if (Tools.IsANumber(family.children[family.children.Length - 1].OffSet)) { family.children[family.children.Length - 1].MakeClone(Species[z].BestCandidate); } family.MakeChildrenParents(); processedFamilies.Enqueue(family); for (int i = 0; i < PARENT_COUNT; i++) { if (family.parents[i].NumberOfAllOperators == 0) { } parents.Enqueue(family.parents[i]); family.parents[i] = null; } CheckBestCandidate(Species[z].SpecInfo.GetCopy()); } catch (Exception e) { error = e; loopState.Break(); } }); if (error != null) { ExceptionDispatchInfo.Capture(error).Throw(); } foreach (Family family in processedFamilies) { families.Enqueue(family); } List <Equation> sortedParents = parents.ToList().OrderBy(x => x.OffSet).ToList(); //const double SURVIVAL_RATE = 0.01; //for (int i = (int)((double)EInfo.SpeciesAmount * (1 - SURVIVAL_RATE)); i < EInfo.SpeciesAmount; i++) //{ // sortedParents[i].Cleanup(); // RandomCand.MakeValidRandomEquation(sortedParents[i]); //} parents = new ConcurrentQueue <Equation>(); sortedParents.ForEach(x => parents.Enqueue(x)); } }
/// <summary> /// resets a singl equation making it ready to create a new one /// </summary> /// <param name="Cand"></param> protected void ResetSingle(Equation Cand) { Cand.Cleanup(); }
private static void SmartChangeNumber(EvolutionInfo EInfo, Equation Eq, Equation BCand, Equation OCand, int[] Indexes) { foreach (int Index in Indexes) { #if DEBUG if (Index >= BCand.AllOperators.Length || Index < 0 || Index >= OCand.AllOperators.Length || Index < 0) { System.Diagnostics.Debugger.Break(); } #endif Operator BCandOper = BCand.AllOperators[Index]; Operator OCandOper = OCand.AllOperators[Index]; if (BCandOper.RandomNumber > OCandOper.RandomNumber) { Eq.AllOperators[Index].RandomNumber = Eq.Randomizer.Next(EInfo.NumberRangeMin, (int)BCandOper.RandomNumber + 1); } else { Eq.AllOperators[Index].RandomNumber = Eq.Randomizer.Next((int)BCandOper.RandomNumber, EInfo.NumberRangeMax); } Eq.AllOperators[Index].OperatorChanged(); } }
public static void SmartifyCandidates(EvolutionInfo EInfo, Equation[] Copys, Equation BCand, Equation OCand, int StartIndex, int Amount) { int[] Indexes = CanSmartChangeNumbers(BCand, OCand); if (Indexes != null) { SmartChangeNumbers(EInfo, Copys, BCand, OCand, Indexes, StartIndex, Amount); } else { StupidChangeNumbers(EInfo, Copys, StartIndex, Amount); } }
private static void SmartChangeNumbers(EvolutionInfo EInfo, Equation[] Copys, Equation BCand, Equation OCand, int[] Indexes, int StartIndex, int Amount) { for (int i = StartIndex; i < StartIndex + Amount; i++) { SmartChangeNumber(EInfo, Copys[i], BCand, OCand, Indexes); Copys[i].CalcTotalOffSet(); } }
public static void SmartifyCandidate(EvolutionInfo EInfo, Equation ToSmartify, Equation BCand, Equation OCand, int[] Indexes) { if (Indexes != null) { SmartChangeNumber(EInfo, ToSmartify, BCand, OCand, Indexes); } else { StupidChangeNumber(EInfo, ToSmartify); } }
public static void SmartifyCandidate(EvolutionInfo EInfo, Equation ToSmartify, Equation BCand, Equation OCand) { int[] Indexes = CanSmartChangeNumbers(BCand, OCand); SmartifyCandidate(EInfo, ToSmartify, BCand, OCand, Indexes); }
public static void MakeRandomEquation(Equation Cand) { Cand.MakeRandom(); }
//public readonly Vector<double>[] OperatorResults; public Operator(Equation OEq) : base(OEq.EInfo.MaxSize) { Eq = OEq; parenthesesResults = new float[OEq.EInfo.coordInfo.expectedResults.Length]; }
public static void CompressOperatorArray(Operator[] Operators, int NumberOfOperators, Equation Eq, bool isNotAllOperators) //Crap solution with the bool { int smallestFreeIndex = 0; while (Operators[smallestFreeIndex] != null) { smallestFreeIndex++; } int OperatorsToCompressLeft = NumberOfOperators; int OperatorToCompressIndex = 0; while (OperatorsToCompressLeft > 0) { if (Operators[OperatorToCompressIndex] != null) { OperatorsToCompressLeft--; if (isNotAllOperators) { Operators[OperatorToCompressIndex].Compress(Eq); } if (smallestFreeIndex < OperatorToCompressIndex) { Operators[smallestFreeIndex] = Operators[OperatorToCompressIndex]; Operators[OperatorToCompressIndex] = null; if (isNotAllOperators) { Operators[smallestFreeIndex].ContainedIndex = smallestFreeIndex; } else { Operators[smallestFreeIndex].AllOperatorsContainedIndex = smallestFreeIndex; } while (Operators[smallestFreeIndex] != null) { smallestFreeIndex++; } } } OperatorToCompressIndex++; } }