public void SetSettings(Settings settings) { FileStream stream = null; BinaryWriter writer = null; try { stream = new FileStream(filePath, FileMode.Create); writer = new BinaryWriter(stream); writer.Write(settings.StartingTemperature); writer.Write(settings.EndingTemperature); writer.Write(settings.NumIterations); writer.Write(settings.NumIterationsMultiplier); writer.Write(settings.CoolingCoefficient); writer.Write((int)settings.Operation); writer.Write(settings.PointsPerIterationBlock); currentSettings = settings; } catch { throw; } finally { if (stream != null) stream.Close(); if (writer != null) writer.Close(); } }
public void SolveAsync(InputData input, Settings settings) { if (worker.IsBusy == true) { throw new SolverBusyException(); } this.input = input; this.settings = settings; SolverBegin(this, new SolverBeginEventArgs { StartingTemperature = settings.StartingTemperature, EndingTemperature = settings.EndingTemperature }); worker.RunWorkerAsync(); }
public OutputData Solve(InputData input, Settings settings, BackgroundWorker worker) { Random rand = new Random(); OutputData result = new OutputData(); //for (int i = 0; i < 200; i++) //{ // IterationBlock block = new IterationBlock // { // ProgressionCount = rand.Next(10), // RegressionCount = rand.Next(10) // }; // for (int j = 0; j < 10; j++) // { // block.Values.Add(new Iteration(j) // { // Cost = Math.Sin(i * (double)j * 10) // }); // } // worker.ReportProgress(0, block); // result.Iterations.Add(block); // if (worker.CancellationPending == true) // { // return null; // } // System.Threading.Thread.Sleep(50); //} //result.TotalCost = 10; //result.Solution = new List<int> { 4, 5, 6, 3, 2, 1, 0, 6, 5, 4, 3, 3, 4, 234, 234, 32, 34, 4, 43, 4 }; return result; }
public Settings GetSettings() { //if (currentSettings != null) // return currentSettings; Settings settings = null; try { using (FileStream stream = new FileStream(filePath, FileMode.Open)) { using (BinaryReader reader = new BinaryReader(stream)) { settings = ReadSettings(reader); currentSettings = settings; } } } catch { return defaultSettings; } return settings; }
public OutputData Solve(InputData input, Settings settings, BackgroundWorker worker) { // TODO // Gdy problem zadany bez stacji (fuel cap = 0), to coś sie psuje OutputData result = new OutputData(); Operation operation = null; switch (settings.Operation) { case OperationType.Operation1: operation = Operation1; break; case OperationType.Operation2: operation = Operation2; break; } List<double> iterationBlockCosts = new List<double>(); double currentCost = 0; double newCost = 0; double subtract = 0; int numIterations = settings.NumIterations; double temperature = settings.StartingTemperature; List<int> currentSolution = null; List<int> currentSolutionWithPetrolPlaces = new List<int>(); if (input.FuelCapacity > 0) { int counter = 0; do { if (counter > Math.Pow(input.NumPlaces, 2)) { result.State = OutputState.NoSolution; return result; } currentSolution = GetStartingSolution(input); currentSolution.CopyListTo(currentSolutionWithPetrolPlaces); counter++; } while (!PutPetrolPlaces(input, currentSolutionWithPetrolPlaces)); } else { currentSolution = GetStartingSolution(input); currentSolution.CopyListTo(currentSolutionWithPetrolPlaces); } List<int> newSolution = new List<int>(new int[currentSolution.Count]); List<int> newSolutionWithPetrolPlaces = new List<int>(); currentSolution.CopyListTo(newSolution); int currentSolutionCountMinusOne = currentSolutionWithPetrolPlaces.Count() - 1; for (int i = 0; i < currentSolutionCountMinusOne; i++) { //CurrentCost = CurrentCost + input.Places[CurrentSolution[i]][CurrentSolution[i + 1]]; currentCost += input.Places[currentSolutionWithPetrolPlaces[i]][currentSolutionWithPetrolPlaces[i + 1]]; } while (temperature > settings.EndingTemperature) { IterationBlock block = new IterationBlock(); double minCost = currentCost, maxCost = currentCost; for (int i = 0; i < numIterations; i++) { if (input.FuelCapacity > 0) { do { operation(currentSolution, newSolution); newSolution.CopyListTo(newSolutionWithPetrolPlaces); } while (!PutPetrolPlaces(input, newSolutionWithPetrolPlaces)); } else { operation(currentSolution, newSolution); newSolution.CopyListTo(newSolutionWithPetrolPlaces); } newCost = 0; int newSolutionCountMinusOne = newSolutionWithPetrolPlaces.Count() - 1; for (int j = 0; j < newSolutionCountMinusOne; j++) { newCost += input.Places[newSolutionWithPetrolPlaces[j]][newSolutionWithPetrolPlaces[j + 1]]; } subtract = newCost - currentCost; double randomik = rand.NextDouble(); double zmienna = Math.Exp((-1) * (subtract / temperature)); if (subtract <= 0 || subtract > 0 && zmienna > randomik) { currentCost = newCost; if (currentCost > maxCost) maxCost = currentCost; else if (currentCost < minCost) minCost = currentCost; newSolution.CopyListTo(currentSolution); newSolutionWithPetrolPlaces.CopyListTo(currentSolutionWithPetrolPlaces); if (subtract <= 0) block.ProgressionCount++; else block.RegressionCount++; } //block.Values.Add(currentCost); iterationBlockCosts.Add(currentCost); if (iterationBlockCosts.Count == (int)(numIterations / settings.PointsPerIterationBlock)) { block.Values.Add(iterationBlockCosts.ReduceCollectionToValue(x => x.Average())); iterationBlockCosts.Clear(); } if (worker.CancellationPending == true) { result.State = OutputState.Cancelled; return result; } } temperature *= settings.CoolingCoefficient; block.CurrentTemperature = temperature; numIterations = (int)((double)numIterations * settings.NumIterationsMultiplier); //block.Values = iterationBlockCosts.ReduceCollection( // settings.PointsPerIterationBlock, // x => x.Average()).ToList(); block.Minimum = minCost; block.Maximum = maxCost; iterationBlockCosts.Clear(); worker.ReportProgress(0, block); result.Iterations.Add(block); } result.Solution = currentSolutionWithPetrolPlaces; result.TotalCost = currentCost; result.State = OutputState.Done; return result; }
private static Settings ReadSettings(BinaryReader reader) { Settings settings = new Settings(); settings.StartingTemperature = reader.ReadDouble(); settings.EndingTemperature = reader.ReadDouble(); settings.NumIterations = reader.ReadInt32(); settings.NumIterationsMultiplier = reader.ReadDouble(); settings.CoolingCoefficient = reader.ReadDouble(); settings.Operation = (OperationType)reader.ReadInt32(); settings.PointsPerIterationBlock = reader.ReadInt32(); return settings; }