public async Task Optimise_Brute_Force_Async(IProgress <TaskAsyncProgress> progress) { Column col = column.Clone(); Calculations calc = new Calculations(col); double costRef = calc.GetCost()[3]; double carbonRef = calc.GetEmbodiedCarbon()[2]; bool[] fireMethods = new bool[] { true, false, false, false }; double Wcarb = DriversWeight[1]; double Wcost = DriversWeight[0]; double Fcarb = Drivers[1]; double Fcost = Drivers[0]; bool square = false; double T = T0; int t = 0; int LX0 = -1; int LX1 = -1; int LY0 = -1; int LY1 = -1; int NX0 = -1; int NX1 = -1; int NY0 = -1; int NY1 = -1; int phi0 = -1; int phi1 = -1; int CG0 = -1; int CG1 = -1; List <Column> possibleDesigns = new List <Column>(); if (Activations[0]) { LX0 = Convert.ToInt32(Mins[0]) / Convert.ToInt32(Incrs[0]); LX1 = Convert.ToInt32(Maxs[0]) / Convert.ToInt32(Incrs[0]); } if (Activations[1]) { LY0 = Convert.ToInt32(Mins[1]) / Convert.ToInt32(Incrs[1]); LY1 = Convert.ToInt32(Maxs[1]) / Convert.ToInt32(Incrs[1]); } if (Activations[2]) { NX0 = Convert.ToInt32(Mins[2]); NX1 = Convert.ToInt32(Maxs[2]); } if (Activations[3]) { NY0 = Convert.ToInt32(Mins[3]); NY1 = Convert.ToInt32(Maxs[3]); } if (Activations[8]) { phi0 = BarDiameters.IndexOf(Convert.ToInt32(Mins[8])); phi1 = BarDiameters.IndexOf(Convert.ToInt32(Maxs[8])); } if (Activations[10]) { CG0 = ConcreteGrades.IndexOf(ConcreteGrades.First(c => c.Name == Mins[10])); CG1 = ConcreteGrades.IndexOf(ConcreteGrades.First(c => c.Name == Maxs[10])); } if (Sizes.Count > 0) { for (int i = 0; i < Sizes.Count; i++) { for (int nx = NX0; nx <= NX1; nx++) { for (int ny = NY0; ny <= NY1; ny++) { for (int phi = phi0; phi <= phi1; phi++) { for (int cg = CG0; cg <= CG1; cg++) { Column c = column.Clone(); c.LX = Sizes[i].Item1; c.LY = Sizes[i].Item2; c.NRebarX = nx; c.NRebarY = ny; c.BarDiameter = BarDiameters[phi]; c.ConcreteGrade = ConcreteGrades[cg]; possibleDesigns.Add(c); } } } } } } else { double incr = Convert.ToDouble(Incrs[0]); for (int lx = LX0; lx <= LX1; lx++) { for (int ly = LY0; ly <= LY1; ly++) { for (int nx = NX0; nx <= NX1; nx++) { for (int ny = NY0; ny <= NY1; ny++) { for (int phi = phi0; phi <= phi1; phi++) { for (int cg = CG0; cg <= CG1; cg++) { Column c = column.Clone(); c.LX = incr * lx; c.LY = incr * ly; c.NRebarX = nx; c.NRebarY = ny; c.BarDiameter = BarDiameters[phi]; c.ConcreteGrade = ConcreteGrades[cg]; possibleDesigns.Add(c); } } } } } } } Column BestCol = possibleDesigns[0]; double fBest = Objective(col); //, carbonRef, costRef, Wcost, Fcost, Wcarb, Fcarb, fireMethods); for (int i = 0; i < possibleDesigns.Count; i++) { Column ColTemp = possibleDesigns[i]; // delta f var res = Objective(ColTemp); //, carbonRef, costRef, Wcost, Fcost, Wcarb, Fcarb, fireMethods); double fp = res; if (fp < fBest) { BestCol = ColTemp.Clone(); fBest = fp; } T = Alpha * T; t++; if (t % 4 == 0) { Console.WriteLine("iteration {0}", t); TaskAsyncProgress report = new TaskAsyncProgress() { ProgressPercentage = (int)(t * 1.0 / possibleDesigns.Count * 100), Col = BestCol, }; progress.Report(report); } } }
public async Task Optimise_Simulated_Annealing_Async(IProgress <TaskAsyncProgress> progress) { Column col = column.Clone(); double Wcarb = DriversWeight[1]; double Wcost = DriversWeight[0]; double Fcarb = Drivers[1]; double Fcost = Drivers[0]; bool square = false; double T = T0; int t = 0; double f = fBest; // while (t < Maxiter) { int NN = Activations.Count(); // update of X Column ColTemp = col.Clone(); ColTemp.SetChecksToFalse(); ColTemp.DiagramDisc = Convert.ToInt32(15 + 15 * t * 1.0 / (Maxiter - 1)); // linear increase of the interaction diagram discretisation (for speed purposes) List <int> eps = NormalRand(NN, Variance); if (Activations[0]) { ColTemp.LX = Math.Max(Convert.ToDouble(Mins[0]), Math.Min(col.LX + eps[0] * Convert.ToDouble(Incrs[0]), Convert.ToDouble(Maxs[0]))); } if (Activations[1]) { ColTemp.LY = square ? ColTemp.LX : Math.Max(Convert.ToDouble(Mins[1]), Math.Min(col.LY + eps[1] * Convert.ToDouble(Incrs[1]), Convert.ToDouble(Maxs[1]))); } if (Activations[2]) { ColTemp.NRebarX = Math.Max(Convert.ToInt32(Mins[2]), Math.Min(col.NRebarX + eps[2], Convert.ToInt32(Maxs[2]))); } if (Activations[3]) { ColTemp.NRebarY = Math.Max(Convert.ToInt32(Mins[3]), Math.Min(col.NRebarY + eps[3], Convert.ToInt32(Maxs[3]))); } if (Activations[4]) { ColTemp.Diameter = Math.Max(Convert.ToDouble(Mins[4]), Math.Min(col.Diameter + eps[4] * Convert.ToDouble(Incrs[4]), Convert.ToDouble(Maxs[4]))); } if (Activations[5]) { ColTemp.NRebarCirc = Math.Max(Convert.ToInt32(Mins[5]), Math.Min(col.NRebarCirc + eps[5], Convert.ToInt32(Maxs[5]))); } if (Activations[6]) { ColTemp.Radius = Math.Max(Convert.ToDouble(Mins[6]), Math.Min(col.Radius + eps[6] * Convert.ToDouble(Incrs[6]), Convert.ToDouble(Maxs[6]))); } if (Activations[7]) { ColTemp.Edges = Math.Max(Convert.ToInt32(Mins[7]), Math.Min(col.Edges + eps[7] * Convert.ToInt32(Incrs[7]), Convert.ToInt32(Maxs[7]))); } if (Activations[8]) { int minidx = BarDiameters.IndexOf(Convert.ToInt32(Mins[8])); int maxidx = BarDiameters.IndexOf(Convert.ToInt32(Maxs[8])); int idx = BarDiameters.IndexOf(col.BarDiameter); ColTemp.BarDiameter = BarDiameters[Math.Max(minidx, Math.Min(idx + eps[8], maxidx))]; } if (Activations[9]) { int minidx = LinkDiameters.IndexOf(Convert.ToInt32(Mins[9])); int maxidx = LinkDiameters.IndexOf(Convert.ToInt32(Maxs[9])); int idx = LinkDiameters.IndexOf(col.BarDiameter); ColTemp.LinkDiameter = LinkDiameters[Math.Max(minidx, Math.Min(idx + eps[9], maxidx))]; } if (Activations[10]) { List <string> grades = ConcreteGrades.Select(x => x.Name).ToList(); int minidx = grades.IndexOf(Mins[10]); int maxidx = grades.IndexOf(Maxs[10]); int idx = grades.IndexOf(col.ConcreteGrade.Name); ColTemp.ConcreteGrade = ConcreteGrades[Math.Max(minidx, Math.Min(idx + eps[10], maxidx))]; } // we check there has been a change in the column properties compared to last iteration (on a limited number of prop at the moment) bool b1 = col.LX == ColTemp.LX; bool b2 = col.LY == ColTemp.LY; bool b3 = col.NRebarX == ColTemp.NRebarX; bool b4 = col.NRebarY == ColTemp.NRebarY; bool b5 = col.BarDiameter == ColTemp.BarDiameter; bool b6 = col.ConcreteGrade.Name == ColTemp.ConcreteGrade.Name; bool b7 = col.NRebarCirc == ColTemp.NRebarCirc; if (b1 && b2 && b3 && b4 && b5 && b6 && b7) { continue; } // delta f var res = Objective(ColTemp); //, CarbonRef, CostRef, Wcost, Fcost, Wcarb, Fcarb, fireMethods); double fp = res; double Df = fp - f; if (Df < 0) { col = ColTemp; f += Df; } else { Random rand = new Random(); double r = rand.NextDouble(); double p = Math.Exp(-Df / T); if (p > r) { col = ColTemp; f += Df; } } T = Alpha * T; t++; if (fp <= fBest) { Console.WriteLine("old obj : {0}", BestCol.Cost); Console.WriteLine("new obj : {0}", ColTemp.Cost); Calculations calc0 = new Calculations(BestCol); Console.WriteLine("old carbon : {0}", calc0.GetEmbodiedCarbon()[2]); calc0 = new Calculations(ColTemp); Console.WriteLine("new carbon : {0}", calc0.GetEmbodiedCarbon()[2]); BestCol = ColTemp.Clone(); fBest = fp; Console.WriteLine("iteration {0}", t); TaskAsyncProgress report = new TaskAsyncProgress() { ProgressPercentage = (int)(t * 1.0 / Maxiter * 100), Col = BestCol, Updated = true, }; progress.Report(report); } else if (t % 4 == 0) { Console.WriteLine("iteration {0}", t); TaskAsyncProgress report = new TaskAsyncProgress() { ProgressPercentage = (int)(t * 1.0 / Maxiter * 100), Col = BestCol, Updated = false, }; progress.Report(report); } } //return col; //return column; }