Ejemplo n.º 1
0
        public static void InputToFile(string filePath, KnapsackAlgorithmInput[] inputs, bool append)
        {
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(filePath, append))
            {
                foreach (KnapsackAlgorithmInput input in inputs)
                {
                    file.Write($"n{input.TypesOfItems}");
                    file.Write($"c{input.Capacity}");
                    file.Write("b");
                    foreach (int weight in input.Weights)
                    {
                        file.Write($"w{weight}");
                    }
                    file.Write("b");
                    foreach (int price in input.Prices)
                    {
                        file.Write($"p{price}");
                    }
                    file.Write("b");

                    KnapsackSolution sol = Algorithm.DynamicProgrammingAlgorithm(input);

                    file.WriteLine($"res{sol.FinalPrice}");
                }
            }
        }
Ejemplo n.º 2
0
        }//*/

        public static KnapsackSolution FptasAlgorithm(KnapsackAlgorithmInput test)
        {
            var startTime = System.Diagnostics.Stopwatch.StartNew();

            int[] weightsCopy = (int[])test.Weights.Clone();
            int[] pricesCopy1 = (int[])test.Prices.Clone();
            int[] pricesCopy2 = (int[])test.Prices.Clone();
            Array.Sort(pricesCopy1);

            double K = pricesCopy1[pricesCopy1.Length - 1] * EParam / test.TypesOfItems;

            for (int i = 0; i < test.TypesOfItems; i++)
            {
                pricesCopy2[i] = (int)(pricesCopy2[i] / K);
            }

            KnapsackAlgorithmInput newInput = new KnapsackAlgorithmInput(test.TypesOfItems, pricesCopy2,
                                                                         test.Weights, test.Capacity, test.PriceBorder, test.WeightBorder);

            KnapsackSolution sol = DynamicProgrammingAlgorithm(newInput);

            startTime.Stop();
            return(new KnapsackSolution(startTime.Elapsed, (int)(sol.FinalPrice * K), test.TypesOfItems, sol.FinalWeight,
                                        test.Capacity, test.MaxPrice, test, sol.Weights, false));
        }
Ejemplo n.º 3
0
 private void LoadSolBtn_Click(object sender, RoutedEventArgs e)
 {
     if (modeBtn.Content.ToString() == "Off")
     {
         OpenFileDialog openFileDialog = new OpenFileDialog();
         openFileDialog.Filter = "Text file (*.ksl)|*.ksl";
         if (openFileDialog.ShowDialog() == true)
         {
             BinaryFormatter formatter = new BinaryFormatter();
             try
             {
                 using (FileStream fs = new FileStream(openFileDialog.FileName,
                                                       FileMode.OpenOrCreate))
                 {
                     latestSolution  = (KnapsackSolution)formatter.Deserialize(fs);
                     textField.Text += latestSolution;
                 }
             }
             catch (Exception)
             {
                 MessageBox.Show("Looks like this is not a real solution file", "Error",
                                 MessageBoxButton.OK, MessageBoxImage.Error);
             }
         }
     }
     else
     {
         OpenFileDialog openFileDialog = new OpenFileDialog();
         openFileDialog.Filter = "Text file (*.ksls)|*.ksls";
         if (openFileDialog.ShowDialog() == true)
         {
             BinaryFormatter formatter = new BinaryFormatter();
             try
             {
                 using (FileStream fs = new FileStream(openFileDialog.FileName,
                                                       FileMode.OpenOrCreate))
                 {
                     latestSolutions = (KnapsackSolutions)formatter.Deserialize(fs);
                     textField.Text += latestSolutions;
                 }
             }
             catch (Exception)
             {
                 MessageBox.Show("Looks like this is not a real solutions file", "Error",
                                 MessageBoxButton.OK, MessageBoxImage.Error);
             }
         }
     }
 }
Ejemplo n.º 4
0
        private async void StartBtn_Click(object sender, RoutedEventArgs e)
        {
            if (input == null && inputs == null)
            {
                MessageBox.Show("Your have no input!", "Error",
                                MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else
            {
                if (input != null)
                {
                    if (algorithm != null)
                    {
                        textField.Text        += input;
                        startBtn.IsEnabled     = false;
                        stopBtn.IsEnabled      = true;
                        saveInputBtn.IsEnabled = false;
                        saveSolBtn.IsEnabled   = false;

                        latestSolution = algorithm(input);
                        latestInput    = new KnapsackAlgorithmInput(input);

                        textField.Text        += latestSolution;
                        startBtn.IsEnabled     = true;
                        stopBtn.IsEnabled      = false;
                        saveInputBtn.IsEnabled = true;
                        saveSolBtn.IsEnabled   = true;
                    }
                    else
                    {
                        MessageBox.Show("Your haven't chosen an algorithm!", "Error",
                                        MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }
                else if (inputs != null)
                {
                    if (algorithm != null)
                    {
                        textField.Text        += inputs;
                        startBtn.IsEnabled     = false;
                        stopBtn.IsEnabled      = true;
                        saveInputBtn.IsEnabled = false;
                        saveSolBtn.IsEnabled   = false;

                        latestSolutions = Algorithm.BigTest(inputs.Inputs, algorithm);
                        latestInputs    = new KnapsackAlgorithmInputs(inputs);

                        textField.Text        += latestSolutions;
                        startBtn.IsEnabled     = true;
                        stopBtn.IsEnabled      = false;
                        saveInputBtn.IsEnabled = true;
                        saveSolBtn.IsEnabled   = true;
                    }
                    else
                    {
                        MessageBox.Show("Your haven't chosen an algorithm!", "Error",
                                        MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }
            }
        }
Ejemplo n.º 5
0
 public void SolveInput()
 {
     latestSolution = algorithm(input);
     latestInput    = new KnapsackAlgorithmInput(input);
 }
Ejemplo n.º 6
0
 private void StopBtn_Click(object sender, RoutedEventArgs e)
 {
     algorithmWindow.Close();
     latestSolution = null;
 }
Ejemplo n.º 7
0
        private void ManualBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (int.TryParse(strCapacity.Trim(), out manualInputCapacity) && manualInputCapacity > 1)
                {
                    try
                    {
                        manualInputWeights = strWeights.Trim().Split(' ').
                                             Where(x => !string.IsNullOrWhiteSpace(x)).
                                             Select(x => int.Parse(x)).ToArray();

                        manualInputPrices = strPrices.Trim().Split(' ').
                                            Where(x => !string.IsNullOrWhiteSpace(x)).
                                            Select(x => int.Parse(x)).ToArray();

                        if (manualInputPrices.Length != manualInputWeights.Length)
                        {
                            throw new Exception();
                        }
                        else
                        {
                            if (algorithm != null)
                            {
                                textField.Text        += input;
                                startBtn.IsEnabled     = false;
                                stopBtn.IsEnabled      = true;
                                saveInputBtn.IsEnabled = false;
                                saveSolBtn.IsEnabled   = false;

                                input = new KnapsackAlgorithmInput(manualInputPrices.Length,
                                                                   manualInputPrices, manualInputWeights, manualInputCapacity, 0);

                                latestSolution = algorithm(input);
                                //textField.VerticalOffset = textField.;
                                latestInput = new KnapsackAlgorithmInput(input);

                                textField.Text        += latestSolution;
                                startBtn.IsEnabled     = true;
                                stopBtn.IsEnabled      = false;
                                saveInputBtn.IsEnabled = true;
                                saveSolBtn.IsEnabled   = true;
                            }
                            else
                            {
                                MessageBox.Show("Your haven't chosen an algorithm!", "Error",
                                                MessageBoxButton.OK, MessageBoxImage.Error);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Something wrong with your weights and prices",
                                        "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                    finally
                    {
                    }
                }
                else
                {
                    MessageBox.Show("Something wrong with your capacity, remember it should be more than 1",
                                    "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Something wrong with your manual input",
                                "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }