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}"); } } }
}//*/ 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)); }
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); } } } }
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); } } } }
public void SolveInput() { latestSolution = algorithm(input); latestInput = new KnapsackAlgorithmInput(input); }
private void StopBtn_Click(object sender, RoutedEventArgs e) { algorithmWindow.Close(); latestSolution = null; }
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); } }