Beispiel #1
0
        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;
            }
        }
Beispiel #2
0
 // Changes operators via roulette-operators function
 private void ChangeOperators()
 {
     if (mDiversity < CRITICAL_DIVERSITY || mDiversity == mPreDiversity)
     {
         mSelOperator = mSelOperators[1];
     }
     else
     {
         mSelOperator = mSelOperators[0];
     }
     mCurrentOperators[0] = OperatorsRoulette(mCrossRoulette);
     mCurrentOperators[1] = OperatorsRoulette(mMutRoulette);
     mCrossOperator       = mCrossOperators[mCurrentOperators[0]];
     mMutOperator         = mMutOperators[mCurrentOperators[1]];
 }