Beispiel #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...");
        }
Beispiel #2
0
        public SimulatedAnnealing()
        {
            process = new Knapsack();
            Random rnd = new Random();
            Dictionary <int, double> ratios = new Dictionary <int, double>();

            for (int i = 0; i < Knapsack.weights.Count; i++)
            {
                ratios[i] = Knapsack.values[i] / Convert.ToDouble(Knapsack.weights[i]);
            }
            knapsack = new bool[Knapsack.weights.Count];
            int sum = 0;

            foreach (KeyValuePair <int, double> item in ratios.OrderByDescending(key => key.Value))
            {
                if (sum + Knapsack.weights[item.Key] <= Knapsack.knapsize)
                {
                    sum += Knapsack.weights[item.Key];
                    knapsack[item.Key] = true;
                }
                else
                {
                    break;
                }
            }
        }