Esempio n. 1
0
        private void startAlgorithm_Click(object sender, EventArgs e)
        {
            GC.Collect();
            List <int>    bests = new List <int>();
            List <double> times = new List <double>();

            Knapsack.startTemp = Double.Parse(startTemp.Text, culture);
            Knapsack.endTemp   = Double.Parse(endTemp.Text, culture);
            int       rptCount = Convert.ToInt32(repeat.Text);
            Stopwatch sw       = new Stopwatch();
            ReadFiles read     = new ReadFiles();
            Knapsack  process  = new Knapsack();
            Random    a        = new Random();
            string    path     = "";
            int       i        = 0;

            read.Read(i);
            for (int j = 0; j < rptCount; j++)
            {
                sw.Start();
                SimulatedAnnealing start  = new SimulatedAnnealing();
                bool[]             result = start.doIt();
                sw.Stop();
                path = "../../../../Datasets/test" + i;
                Console.WriteLine(path + " Try:" + (j + 1) + " " + process.CalculateFitness(result) + " " + sw.ElapsedMilliseconds + " ms");
                bests.Add(process.CalculateFitness(result));
                times.Add(sw.ElapsedMilliseconds);
                sw.Reset();
            }
            //ReadFiles.Write(bests, times, path);
            bests.Clear();
            times.Clear();
            Console.WriteLine("Process Completed...");
        }
Esempio n. 2
0
        public bool[] doIt()
        {
            double T = Knapsack.startTemp;

            bool[] bestSolution = new bool[knapsack.Length];
            Random rnd          = new Random();
            int    delta        = 0;

            while (T > Knapsack.endTemp)
            {
                bool[] temp = new bool[knapsack.Length];

                for (int i = 0; i < 10; i++)
                {
                    bool[] nextElement = process.FindNeighbor(knapsack, rnd.Next(1, Convert.ToInt32(knapsack.Length * 0.5)));
                    delta = process.CalculateFitness(knapsack) - process.CalculateFitness(nextElement);
                    if (delta < 0)
                    {
                        Array.Copy(nextElement, knapsack, knapsack.Length);
                    }
                    else
                    {
                        double prob = RandomP.NextDouble();
                        if (prob < Math.Exp(-1 * delta / T))
                        {
                            Array.Copy(nextElement, knapsack, knapsack.Length);
                        }
                    }
                    if (process.CalculateFitness(knapsack) > process.CalculateFitness(temp))
                    {
                        Array.Copy(knapsack, temp, knapsack.Length);
                    }
                }

                Array.Copy(temp, knapsack, knapsack.Length);
                T *= Knapsack.coolingRate;
                if (process.CalculateFitness(knapsack) > process.CalculateFitness(bestSolution))
                {
                    Array.Copy(knapsack, bestSolution, knapsack.Length);
                }
            }

            return(bestSolution);
        }