private static double LocalSearchStep(ConnectionNetwork network, List <double> startingVector, List <double> stepVector, int coordiante) { Console.WriteLine("Optimizing over coordinate {0}", coordiante); var best = -1; var bestObjV = bigNumber; for (int i = 0; i < candidate.Length; i++) { startingVector[coordiante] = startingVector[coordiante] + stepVector[coordiante] * candidate[i]; var temp = EstimateObjective(network, startingVector); startingVector[coordiante] = startingVector[coordiante] - stepVector[coordiante] * candidate[i]; if (temp < bestObjV) { bestObjV = temp; best = i; } } if (candidate[best] == 0) { stepVector[coordiante] = stepVector[coordiante] / acceleration; } else { startingVector[coordiante] = startingVector[coordiante] + stepVector[coordiante] * candidate[best]; stepVector[coordiante] = stepVector[coordiante] * candidate[best]; } Console.WriteLine("Best candidate: " + best.ToString() + ". Objective: " + bestObjV); return(bestObjV); }
private static double EstimateObjective(ConnectionNetwork network, List <double> startingVector) { var obj = new double[estimateRepetitions]; Parallel.For(0, estimateRepetitions, (i, state) => { Console.WriteLine("Simulation: {0}", i); obj[i] = ObjectiveFunction(network, startingVector); }); return(obj.Average()); }
public static double SimpleLocalSearch(ConnectionNetwork network) { List <double> startingVector = new List <double>() { 1.0 / 30000, 20, 0.2, 0.2, 0.2 }; List <double> stepVector = new List <double>(); foreach (var value in startingVector) { stepVector.Add(Math.Min(Math.Abs(value - 1), value) * 0.25); } return(SimpleLocalSearch(network, startingVector, stepVector)); }
static void Main(string[] args) { var diseaseNetwork = new ConnectionNetwork(Data.GenPopData(), Data.GenWaterData(), Data.GenAdminData()); var simulation = new DiseaseSimulation(diseaseNetwork); Console.WriteLine(diseaseNetwork.numberOfPeople); Console.WriteLine(diseaseNetwork.nodesByCoordAndCat.Count(x => x.Key.category == NodeCategory.People)); Console.WriteLine(diseaseNetwork.nodesByCoordAndCat.Count(x => x.Key.category == NodeCategory.WaterSource)); Console.WriteLine(diseaseNetwork.nodesByCoordAndCat.Count(x => x.Key.category == NodeCategory.River)); Console.WriteLine(diseaseNetwork.nodesByCoordAndCat.Values.Sum(x => x.successorNodes.Count)); //Intervention.CalculateR0(diseaseNetwork, simulation); //RunSimulations(); Console.WriteLine("Press any key to continue."); Console.ReadLine(); }
public static double SimpleLocalSearch(ConnectionNetwork network, List <double> startingVector, List <double> stepVector) { var prevBestObjV = bigNumber; var newBestObjV = bigNumber; var difference = bigNumber; var iter = 0; while (iter < maxIters) { Console.WriteLine("Iteration: {0}. Objective value: {1}", iter, newBestObjV); prevBestObjV = newBestObjV; for (int i = 0; i < startingVector.Count; i++) { newBestObjV = LocalSearchStep(network, startingVector, stepVector, i); } difference = Math.Abs(newBestObjV - prevBestObjV); iter++; } string pathDesktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); string filePath = pathDesktop + "\\OptimalParameters" + ".csv"; if (!System.IO.File.Exists(filePath)) { System.IO.File.Create(filePath).Close(); } string delimter = ";"; using (System.IO.TextWriter writer = System.IO.File.CreateText(filePath)) { foreach (var num in startingVector) { writer.WriteLine(string.Join(delimter, num)); } writer.WriteLine(string.Join(delimter, newBestObjV)); } return(newBestObjV); }
static void ManualCalibration(ConnectionNetwork diseaseNetwork, DiseaseSimulation simulation) { var numSimulations = 1; var tPop = diseaseNetwork.numberOfPeople; string input = "R"; while (input != "S") { if (input == "N") { Console.WriteLine("Input new parametes separated by spaces: distContProb, K, infProb, hwProb, wwProb"); var newParams = Console.ReadLine(); var tokens = newParams.Split(' '); var parameters = Array.ConvertAll(tokens, double.Parse).ToList(); simulation.SetSimulationParameters(parameters); } if (input == "C") { Console.WriteLine("Input new number of simulations: "); var n = Console.ReadLine(); numSimulations = Convert.ToInt32(n); } var results = new double[numSimulations]; for (int i = 0; i < numSimulations; i++) { Console.WriteLine("Running simulation: " + (i + 1)); results[i] = ParameterFitting.ObjectiveFunction(simulation); } Console.WriteLine("Objective values: " + results.Average()); Console.WriteLine("Run another simulation (R), input new parameters (N), change number of simulations (C) or stop (S)"); input = Console.ReadLine(); } }
static void RunSimulations() { var diseaseNetwork = new ConnectionNetwork(Data.GenPopData(), Data.GenWaterData(), Data.GenAdminData()); string file = Directory.GetCurrentDirectory(); file = Path.GetFullPath(Path.Combine(file, @"..\..\")) + "simResultsTimeUnscaled.csv"; string header = string.Join(",", Enumerable.Range(0, 496).Select(x => x.ToString()).ToArray()) + "\n"; for (int i = 0; i < 999; i++) { //Console.WriteLine(i); var simulation = new DiseaseSimulation(diseaseNetwork); var simResults = simulation.RunSimulation(); while (simResults[0].Last() < 1000) { simResults = simulation.RunSimulation(); } if (!File.Exists(file)) { File.WriteAllText(file, header); } Console.WriteLine(i.ToString() + ": " + simResults[0].Last().ToString()); //var line = string.Concat(simResults.Select(l => l.Last().ToString() + ",")); var line = string.Concat(simResults[0].Select(dInfectious => dInfectious.ToString() + ",")); line = line.Remove(line.Length - 1) + "\n"; File.AppendAllText(file, line); } }
public static double ObjectiveFunction(ConnectionNetwork network, List <double> parameters) { var simulation = new DiseaseSimulation(network); return(ObjectiveFunction(simulation, parameters)); }
public static double ObjectiveFunction(ConnectionNetwork network) { var simulation = new DiseaseSimulation(network); return(ObjectiveFunction(simulation)); }