public Algorithm(int iterationNumber, int populationCapacity, string filename) { // Initializing of File logging mOutputFileName = filename + "_output.txt"; if (!Directory.Exists("output")) { Directory.CreateDirectory("output"); } sw = new StreamWriter(new FileStream("output/" + mOutputFileName, FileMode.Create, FileAccess.Write)); mFileName = filename; try { mMWrapper = new SalesmanMatrixWrapper(filename + ".txt"); } catch (WrongMatrixException e) { Console.WriteLine(e.Message); mState = AlgorithmState.WrongMatrix; return; } catch (AggregateException ae) { foreach (var e in ae.Flatten().InnerExceptions) { Console.WriteLine(e.Message); } mState = AlgorithmState.WrongMatrix; return; } mLocker = new object(); mCrossMutGate = new AutoResetEvent(false); mRandomizer = new Random(); mIterationNumber = iterationNumber; mPopulationCapacity = populationCapacity; mSelOperator = new RouletteSelection(); mGenOperator = new RouletteGeneration(); // Generating start population: mMainPopulation = mGenOperator.GeneratePop(mMWrapper, mPopulationCapacity); mBufferPopulation = new Person[2 * populationCapacity]; // ... for statistics and something else SetBestPerson(); SetAveFintess(); SetDiversity(); for (int i = 0; i < mCrossRoulette.Length; i++) { mCrossRoulette[i] = mMWrapper.FitnessFunction(mBestPerson); mMutRoulette[i] = mAveFit; } }
public Person Run() { if (!ok) { Console.WriteLine("Not OK!"); return(null); } int[] solution = new int[mMWrapper.Matrix.GetLength(1)]; List <int> leftCities = new List <int>(); solution[0] = new Random().Next(mMWrapper.Matrix.GetLength(1)); for (int i = 0; i < mMWrapper.Matrix.GetLength(1); i++) { leftCities.Add(i); } leftCities.Remove(solution[0]); for (int i = 1; i < mMWrapper.Matrix.GetLength(1); i++) { double buffer = double.MaxValue; int currentNeighbour = 0; foreach (int city in leftCities) { if (mMWrapper.Matrix[city, solution[i - 1]] < buffer) { currentNeighbour = city; buffer = mMWrapper.Matrix[city, solution[i - 1]]; } } solution[i] = currentNeighbour; leftCities.Remove(currentNeighbour); } Console.WriteLine(mMWrapper.FitnessFunction(new Person(solution))); return(new Person(solution)); }
private Person CrossAddPoints(Person p1, Person p2) { var pRet = mCrossOperator.CrossingOver(p1, p2); var retv = mMWrapper.FitnessFunction(pRet); var v1 = mMWrapper.FitnessFunction(p1); var v2 = mMWrapper.FitnessFunction(p2); var points = (v1 + v2) / 2 - retv; for (int iter = 0; iter < mCrossRoulette.Length; iter++) { if (iter != mCurrentOperators[0]) { mCrossRoulette[iter] -= points; if (mCrossRoulette[iter] < 0) { mCrossRoulette[iter] = 0; } } } mCrossRoulette[mCurrentOperators[0]] += points; return(pRet); }
public Person Run() { if (!ok) { Console.WriteLine("Not OK!"); return(null); } int N = mMWrapper.Matrix.GetLength(0); double[,] xMatrix = new double[N, N]; for (int i = 0; i < N; i++) { xMatrix[i, i] = double.MaxValue; for (int j = 0; j < N; j++) { if (i == j) { continue; } double penance = -1 * (mMWrapper.Matrix[i, j]); for (int k = 0; k < i; k++) { penance += mMWrapper.Matrix[k, j]; } for (int k = i + 1; k < N; k++) { penance += mMWrapper.Matrix[k, j]; } for (int k = 0; k < j; k++) { penance += mMWrapper.Matrix[i, k]; } for (int k = j + 1; k < N; k++) { penance += mMWrapper.Matrix[i, k]; } xMatrix[i, j] = penance; } } List <int> takenI = new List <int>(), takenJ = new List <int>(); for (int k = 0; k < N; k++) { int iToTake = 0, jToTake = 0; double minPenance = double.MaxValue; for (int i = 0; i < N; i++) { if (takenI.Contains(i)) { continue; } for (int j = 0; j < N; j++) { if (takenJ.Contains(j)) { continue; } if (xMatrix[i, j] < minPenance) { iToTake = i; jToTake = j; minPenance = xMatrix[i, j]; } } } takenI.Add(iToTake); takenJ.Add(jToTake); } int[] retTrans = new int[N]; int indexOf = 0; for (var aldrkh = 0; aldrkh < N; aldrkh++) { retTrans[aldrkh] = takenJ[indexOf]; indexOf = takenI.IndexOf(retTrans[aldrkh]); Console.Write($"{retTrans[aldrkh]} "); } Console.Write(mMWrapper.FitnessFunction(new Person(retTrans))); return(new Person(retTrans)); }