private static void Main() { Parameters.Verbosity = VerbosityLevels.AboveNormal; // this next line is to set the Debug statements from OOOT to the Console. Debug.Listeners.Add(new TextWriterTraceListener(Console.Out)); /* In this example, we first present how the details of an optimzation * problem can be saved to an XML-file so that it can be read in * and solved as opposed to defining all the details in an imperative * (code line by code line) way. In the first function, the xml file * name "test1.xml" is created. */ makeAndSaveProblemDefinition(); /* now we create a series of different optimization methods and test * them on the problem. The problem is now opened from the file and * the details are stored in an object of class "Problem Definition".*/ var stream = new FileStream(filename, FileMode.Open); ProblemDefinition probTest1 = ProblemDefinition.OpenprobFromXml(stream); abstractOptMethod opty; /***********Gradient Based Optimization with Steepest Descent****************/ SearchIO.output("***********Gradient Based Optimization with Steepest Descent****************"); opty = new GradientBasedOptimization(); opty.Add(probTest1); abstractSearchDirection searchDirMethod = new SteepestDescent(); opty.Add(searchDirMethod); //abstractLineSearch lineSearchMethod = new ArithmeticMean(0.0001, 1, 100); //abstractLineSearch lineSearchMethod = new DSCPowell(0.0001, 1, 100); abstractLineSearch lineSearchMethod = new GoldenSection(0.0001, 1); opty.Add(lineSearchMethod); opty.Add(new squaredExteriorPenalty(opty, 10)); /* since this is not a population-based optimization method, we need to remove the MaxSpan criteria. */ opty.ConvergenceMethods.RemoveAll(a => a is MaxSpanInPopulationConvergence); double[] xStar; var timer = Stopwatch.StartNew(); var fStar = opty.Run(out xStar); printResults(opty, xStar, fStar, timer); /***********Gradient Based Optimization with Fletcher-Reeves****************/ SearchIO.output("***********Gradient Based Optimization with Fletcher-Reeves****************"); /* we don't need to reset (invoke the constructor) for GradientBasedOptimization since we are only * change the seaach direction method. */ searchDirMethod = new FletcherReevesDirection(); /* you could also try the remaining 3 search direction methods. */ //searchDirMethod = new CyclicCoordinates(); //searchDirMethod = new BFGSDirection(); //searchDirMethod = new PowellMethod(0.001, 6); opty.Add(searchDirMethod); timer = Stopwatch.StartNew(); opty.ResetFunctionEvaluationDatabase(); fStar = opty.Run(out xStar); printResults(opty, xStar, fStar, timer); /******************Generalized Reduced Gradient***********************/ SearchIO.output("******************Generalized Reduced Gradient***********************"); opty = new GeneralizedReducedGradientActiveSet(); opty.Add(probTest1); opty.Add(new squaredExteriorPenalty(opty, 10)); opty.ConvergenceMethods.RemoveAll(a => a is MaxSpanInPopulationConvergence); timer = Stopwatch.StartNew(); fStar = opty.Run(out xStar); printResults(opty, xStar, fStar, timer); /* GRG is the ONLY one here that handles constraints explicity. It find the * optimal very quickly and accurately. However, many of the other show a * better value of f*, this is because they are using an imperfect penalty * function (new squaredExteriorPenalty(opty, 10)). While it seems that GRG * includes it as well, it is only used in the the line search method. */ /******************Random Hill Climbing ***********************/ SearchIO.output("******************Random Hill Climbing ***********************"); opty = new HillClimbing(); opty.Add(probTest1); opty.Add(new squaredExteriorPenalty(opty, 8)); opty.Add(new RandomNeighborGenerator(probTest1.SpaceDescriptor)); opty.Add(new KeepSingleBest(optimize.minimize)); opty.ConvergenceMethods.RemoveAll(a => a is MaxSpanInPopulationConvergence); /* the deltaX convergence needs to be removed as well, since RHC will end many iterations * at the same point it started. */ opty.ConvergenceMethods.RemoveAll(a => a is DeltaXConvergence); timer = Stopwatch.StartNew(); fStar = opty.Run(out xStar); printResults(opty, xStar, fStar, timer); /******************Exhaustive Hill Climbing ***********************/ SearchIO.output("******************Exhaustive Hill Climbing ***********************"); /* Everything else about the Random Hill Climbing stays the same. */ opty.Add(new ExhaustiveNeighborGenerator(probTest1.SpaceDescriptor)); timer = Stopwatch.StartNew(); fStar = opty.Run(out xStar); printResults(opty, xStar, fStar, timer); /******************Simulated Annealing***********************/ SearchIO.output("******************Simulated Annealing***********************"); opty = new SimulatedAnnealing(optimize.minimize); opty.Add(probTest1); opty.Add(new squaredExteriorPenalty(opty, 10)); opty.Add(new RandomNeighborGenerator(probTest1.SpaceDescriptor, 100)); opty.Add(new SACoolingSangiovanniVincentelli(100)); opty.ConvergenceMethods.RemoveAll(a => a is MaxSpanInPopulationConvergence); /* the deltaX convergence needs to be removed as well, since RHC will end many iterations * at the same point it started. */ opty.ConvergenceMethods.RemoveAll(a => a is DeltaXConvergence); timer = Stopwatch.StartNew(); fStar = opty.Run(out xStar); printResults(opty, xStar, fStar, timer); /******************Exhaustive Search ***********************/ // SearchIO.output("******************Exhaustive Search ***********************"); //opty = new ExhaustiveSearch(probTest1.SpaceDescriptor, optimize.minimize); //opty.Add(probTest1); /* No convergence criteria is needed as the process concludes when all * states have been visited but for this problem that is 4 trillion states.*/ //opty.ConvergenceMethods.Clear(); /* if you DID KNOW the best, you can include a criteria like...*/ //opty.ConvergenceMethods.Add(new ToKnownBestXConvergence(new[] { 3.0, 3.0 }, 0.0000001)); //timer = Stopwatch.StartNew(); //fStar = opty.Run(out xStar); /* you probably will never see this process complete. Even with the added * convergence criteria (which is not factored into the estimated time of * completion), you are probably looking at 1 to 2 years. */ //printResults(opty, xStar, fStar, timer); }
public void evalGT(candidate c) { current = c; reorderNodes(c); Boolean found = false; /* recall that gearcount is found in reorderNodes, Albert! */ stateVars = new double[gearcount + 1, 10]; #region Set up optMethod //NelderMead optMethod = // new NelderMead(.001, 10, true); //GradientBasedUnconstrained optMethod = // new GradientBasedUnconstrained(10); GradientBasedOptimization optMethod = new GradientBasedOptimization(10); //SequentialQuadraticProgramming optMethod = new SequentialQuadraticProgramming(true); //GeneralizedReducedGradientActiveSet optMethod = // new GeneralizedReducedGradientActiveSet(true); optMethod.Add(new ArithmeticMean(optMethod, 0.001, 2, 200)); //optMethod.Add(new GoldenSection(optMethod, 0.001,200, int.MaxValue)); //optMethod.Add(new BFGSDirection()); optMethod.Add(new FletcherReevesDirection()); optMethod.Add(new convergenceBasic(BasicConvergenceTypes.OrBetweenSetConditions, 200, 0.0001, double.NaN, double.NaN, int.MaxValue)); //optMethod.Add(new convergenceBasic(BasicConvergenceTypes.AndBetweenSetConditions, 20, 0.01, double.NaN, double.NaN, int.MaxValue)); optMethod.Add(new squaredExteriorPenalty(optMethod, 10.0)); //optMethod.Add(new linearExteriorPenaltySum(optMethod, 10.0)); DiscreteSpaceDescriptor dsd = new DiscreteSpaceDescriptor(optMethod, 4 * gearcount); optMethod.Add(dsd); #endregion for (int i = 0; i < gearcount; i++) { foreach (GearFamily gf in gearFamilies) if (c.graph.nodes[i].localLabels.Contains(gf.label)) { for (int j = 0; j < 3; j++) { if (c.graph.nodes[i].localVariables.Count < j + 1) c.graph.nodes[i].localVariables.Add(double.NaN); } c.graph.nodes[i].localVariables[0] = gf.Sfb; c.graph.nodes[i].localVariables[1] = gf.Sfc; c.graph.nodes[i].localVariables[2] = gf.density; dsd.addLinkedVariableValues(new int[] { 4 * i, 4 * i + 1, 4 * i + 2 }, gf.gears); } } SearchIO.output("The parametric space is " + dsd.SizeOfSpace.ToString(), 3); //setup constraints for optimization //slot 1 - number of teeth //slot 2 - pitch //slot 3 - face Width //slot 4 - location variable #region Constraint Building Region double[] x = new double[4 * gearcount]; double[] xStar = new double[4 * gearcount]; //double fStar = double.PositiveInfinity; double fStar = 0.0; double ftemp = 1000; double weightstar = 100; double lowestmass = 100; double massstar = 100; double mass = 100; double[,] stateVarsStar = new double[gearcount + 1, 10]; outputSpeedConstraint oSC = new outputSpeedConstraint(stateVars, this); optMethod.Add(oSC); stressConstraint sc = new stressConstraint(stateVars, c, this); optMethod.Add(sc); boundingboxConstraint bbc = new boundingboxConstraint(stateVars, c, this); optMethod.Add(bbc); outputLocationConstraint olc = new outputLocationConstraint(stateVars, c, this); optMethod.Add(olc); List<samePitch> samePitches = new List<samePitch>(); for (int i = 0; i < gearcount; i++) { if ((c.graph.nodes[i].localLabels.Contains("contact")) || (c.graph.nodes[i].localLabels.Contains("bevelcontact")) || (c.graph.nodes[i].localLabels.Contains("wormcontact"))) { samePitches.Add(new samePitch(((i - 1) * 4) + 1, ((i * 4) + 1))); } } f = new totalObjFunction(this, current, stateVars); optMethod.Add(f); #endregion int numVars = dsd.linkedSpace.Count; int[] VarIndices = new int[numVars]; for (int i = 0; i < numVars; i++) VarIndices[i] = 0; int[] VarMaxes = new int[numVars]; for (int i = 0; i < numVars; i++) VarMaxes[i] = dsd.linkedSpace[i].GetLength(0); int currentI = 0; VarIndices[currentI]--; //this is an unavoidable hack to start at all zeroes. do { VarIndices[currentI]++; if (VarIndices[currentI] == VarMaxes[currentI]) { VarIndices[currentI] = 0; currentI++; if ((currentI > numVars - 3) && (currentI < numVars)) SearchIO.output("Index " + currentI.ToString() + " changed to " + VarIndices[currentI].ToString(), 4); } else { currentI = 0; x = dsd.GetDesignVector(null, VarIndices); //fStar = f.calculate(x); Boolean Feasible = true; foreach (samePitch sp in samePitches) if (!sp.feasible(x)) Feasible = false; if (Feasible && oSC.feasible(x)) { if (sc.feasible(x)) { //run optMethod here double[] xTuned; mass = 0; double fTuned = optMethod.run(x, out xTuned); for (int k = 0; k < gearcount; k++) { mass += stateVars[k, 2]; } found = false; if (fTuned< ftemp) { ftemp = fTuned; } found = isCurrentTheGoal(stateVars, udg); if (found == true) { if (mass < massstar) { fStar = fTuned; xStar = (double[])xTuned.Clone(); massstar = mass; stateVarsStar = (double[,])stateVars.Clone(); } } } } } } while (currentI < numVars); SearchIO.output("final report f = " + c.f0, 3); if (gearcount > 2) { if (massstar == 100) fStar = ftemp+50; } c.f0 = fStar; c.f2 = massstar; c.f1 = calcInefficiency(stateVarsStar); string outputString = ""; string outputString2 = ""; double p = 0; for (int i = 0; i < gearcount; i++) //var order //x, y, z, vx, vy, vz, face width, diameter, number of teeth, type ID number {//output for gear visualizer! outputString += "gear," + (i + 1) + "," + stateVarsStar[i, 3] + "," + stateVarsStar[i, 4] + "," + stateVarsStar[i, 5] + "," + stateVarsStar[i, 6] + "," + stateVarsStar[i, 7] + "," + stateVarsStar[i, 8] + "," + xStar[i * 4 + 2] + "," + (xStar[i * 4] / xStar[i * 4 + 1]) + "," + xStar[i * 4] + "," + stateVarsStar[i, 9] + "\n"; if (i != 0) { if ((stateVarsStar[i, 3] - stateVarsStar[(i - 1), 3] == 0) && (stateVarsStar[i, 4] - stateVarsStar[(i - 1), 4] == 0)) { outputString2 += "rod," + (p) + "," + i + "," + (i - 1);//(stateVarsStar[i, 5] - stateVarsStar[(i - 1), 5]); p += 1; } } for (int j = 1; j < 9; j++) { if (c.graph.nodes[i].localVariables.Count <= j) c.graph.nodes[i].localVariables.Add(double.NaN); } c.graph.nodes[i].localVariables[3] = stateVarsStar[i, 3]; c.graph.nodes[i].localVariables[4] = stateVarsStar[i, 4]; c.graph.nodes[i].localVariables[5] = stateVarsStar[i, 5]; c.graph.nodes[i].localVariables[6] = stateVarsStar[i, 6]; c.graph.nodes[i].localVariables[7] = stateVarsStar[i, 7]; c.graph.nodes[i].localVariables[8] = stateVarsStar[i, 8]; } for (int j = 1; j < 9; j++) { if (c.graph.nodes[gearcount].localVariables.Count <= j) c.graph.nodes[gearcount].localVariables.Add(double.NaN); } c.graph.nodes[gearcount].localVariables[3] = stateVarsStar[gearcount, 3]; c.graph.nodes[gearcount].localVariables[4] = stateVarsStar[gearcount, 4]; c.graph.nodes[gearcount].localVariables[5] = stateVarsStar[gearcount, 5]; c.graph.nodes[gearcount].localVariables[6] = stateVarsStar[gearcount, 6]; c.graph.nodes[gearcount].localVariables[7] = stateVarsStar[gearcount, 7]; c.graph.nodes[gearcount].localVariables[8] = stateVarsStar[gearcount, 8]; string filename = Program.settings.outputDirectory + "visualizerOutput1.txt"; FileStream fs = new FileStream(filename, FileMode.Create); StreamWriter outputWriter = new StreamWriter(fs); outputWriter.WriteLine(outputString); outputWriter.WriteLine(outputString2); outputWriter.Close(); fs.Close(); }
public void evalGT(candidate c) { current = c; reorderNodes(c); Boolean found = false; /* recall that gearcount is found in reorderNodes, Albert! */ stateVars = new double[gearcount + 1, 10]; #region Set up optMethod //NelderMead optMethod = // new NelderMead(.001, 10, true); //GradientBasedUnconstrained optMethod = // new GradientBasedUnconstrained(10); GradientBasedOptimization optMethod = new GradientBasedOptimization(10); //SequentialQuadraticProgramming optMethod = new SequentialQuadraticProgramming(true); //GeneralizedReducedGradientActiveSet optMethod = // new GeneralizedReducedGradientActiveSet(true); optMethod.Add(new ArithmeticMean(optMethod, 0.001, 2, 200)); //optMethod.Add(new GoldenSection(optMethod, 0.001,200, int.MaxValue)); //optMethod.Add(new BFGSDirection()); optMethod.Add(new FletcherReevesDirection()); optMethod.Add(new convergenceBasic(BasicConvergenceTypes.OrBetweenSetConditions, 200, 0.0001, double.NaN, double.NaN, int.MaxValue)); //optMethod.Add(new convergenceBasic(BasicConvergenceTypes.AndBetweenSetConditions, 20, 0.01, double.NaN, double.NaN, int.MaxValue)); optMethod.Add(new squaredExteriorPenalty(optMethod, 10.0)); //optMethod.Add(new linearExteriorPenaltySum(optMethod, 10.0)); DiscreteSpaceDescriptor dsd = new DiscreteSpaceDescriptor(optMethod, 4 * gearcount); optMethod.Add(dsd); #endregion for (int i = 0; i < gearcount; i++) { foreach (GearFamily gf in gearFamilies) { if (c.graph.nodes[i].localLabels.Contains(gf.label)) { for (int j = 0; j < 3; j++) { if (c.graph.nodes[i].localVariables.Count < j + 1) { c.graph.nodes[i].localVariables.Add(double.NaN); } } c.graph.nodes[i].localVariables[0] = gf.Sfb; c.graph.nodes[i].localVariables[1] = gf.Sfc; c.graph.nodes[i].localVariables[2] = gf.density; dsd.addLinkedVariableValues(new int[] { 4 * i, 4 * i + 1, 4 * i + 2 }, gf.gears); } } } SearchIO.output("The parametric space is " + dsd.SizeOfSpace.ToString(), 3); //setup constraints for optimization //slot 1 - number of teeth //slot 2 - pitch //slot 3 - face Width //slot 4 - location variable #region Constraint Building Region double[] x = new double[4 * gearcount]; double[] xStar = new double[4 * gearcount]; //double fStar = double.PositiveInfinity; double fStar = 0.0; double ftemp = 1000; double weightstar = 100; double lowestmass = 100; double massstar = 100; double mass = 100; double[,] stateVarsStar = new double[gearcount + 1, 10]; outputSpeedConstraint oSC = new outputSpeedConstraint(stateVars, this); optMethod.Add(oSC); stressConstraint sc = new stressConstraint(stateVars, c, this); optMethod.Add(sc); boundingboxConstraint bbc = new boundingboxConstraint(stateVars, c, this); optMethod.Add(bbc); outputLocationConstraint olc = new outputLocationConstraint(stateVars, c, this); optMethod.Add(olc); List <samePitch> samePitches = new List <samePitch>(); for (int i = 0; i < gearcount; i++) { if ((c.graph.nodes[i].localLabels.Contains("contact")) || (c.graph.nodes[i].localLabels.Contains("bevelcontact")) || (c.graph.nodes[i].localLabels.Contains("wormcontact"))) { samePitches.Add(new samePitch(((i - 1) * 4) + 1, ((i * 4) + 1))); } } f = new totalObjFunction(this, current, stateVars); optMethod.Add(f); #endregion int numVars = dsd.linkedSpace.Count; int[] VarIndices = new int[numVars]; for (int i = 0; i < numVars; i++) { VarIndices[i] = 0; } int[] VarMaxes = new int[numVars]; for (int i = 0; i < numVars; i++) { VarMaxes[i] = dsd.linkedSpace[i].GetLength(0); } int currentI = 0; VarIndices[currentI]--; //this is an unavoidable hack to start at all zeroes. do { VarIndices[currentI]++; if (VarIndices[currentI] == VarMaxes[currentI]) { VarIndices[currentI] = 0; currentI++; if ((currentI > numVars - 3) && (currentI < numVars)) { SearchIO.output("Index " + currentI.ToString() + " changed to " + VarIndices[currentI].ToString(), 4); } } else { currentI = 0; x = dsd.GetDesignVector(null, VarIndices); //fStar = f.calculate(x); Boolean Feasible = true; foreach (samePitch sp in samePitches) { if (!sp.feasible(x)) { Feasible = false; } } if (Feasible && oSC.feasible(x)) { if (sc.feasible(x)) { //run optMethod here double[] xTuned; mass = 0; double fTuned = optMethod.run(x, out xTuned); for (int k = 0; k < gearcount; k++) { mass += stateVars[k, 2]; } found = false; if (fTuned < ftemp) { ftemp = fTuned; } found = isCurrentTheGoal(stateVars, udg); if (found == true) { if (mass < massstar) { fStar = fTuned; xStar = (double[])xTuned.Clone(); massstar = mass; stateVarsStar = (double[, ])stateVars.Clone(); } } } } } } while (currentI < numVars); SearchIO.output("final report f = " + c.f0, 3); if (gearcount > 2) { if (massstar == 100) { fStar = ftemp + 50; } } c.f0 = fStar; c.f2 = massstar; c.f1 = calcInefficiency(stateVarsStar); string outputString = ""; string outputString2 = ""; double p = 0; for (int i = 0; i < gearcount; i++) //var order //x, y, z, vx, vy, vz, face width, diameter, number of teeth, type ID number {//output for gear visualizer! outputString += "gear," + (i + 1) + "," + stateVarsStar[i, 3] + "," + stateVarsStar[i, 4] + "," + stateVarsStar[i, 5] + "," + stateVarsStar[i, 6] + "," + stateVarsStar[i, 7] + "," + stateVarsStar[i, 8] + "," + xStar[i * 4 + 2] + "," + (xStar[i * 4] / xStar[i * 4 + 1]) + "," + xStar[i * 4] + "," + stateVarsStar[i, 9] + "\n"; if (i != 0) { if ((stateVarsStar[i, 3] - stateVarsStar[(i - 1), 3] == 0) && (stateVarsStar[i, 4] - stateVarsStar[(i - 1), 4] == 0)) { outputString2 += "rod," + (p) + "," + i + "," + (i - 1);//(stateVarsStar[i, 5] - stateVarsStar[(i - 1), 5]); p += 1; } } for (int j = 1; j < 9; j++) { if (c.graph.nodes[i].localVariables.Count <= j) { c.graph.nodes[i].localVariables.Add(double.NaN); } } c.graph.nodes[i].localVariables[3] = stateVarsStar[i, 3]; c.graph.nodes[i].localVariables[4] = stateVarsStar[i, 4]; c.graph.nodes[i].localVariables[5] = stateVarsStar[i, 5]; c.graph.nodes[i].localVariables[6] = stateVarsStar[i, 6]; c.graph.nodes[i].localVariables[7] = stateVarsStar[i, 7]; c.graph.nodes[i].localVariables[8] = stateVarsStar[i, 8]; } for (int j = 1; j < 9; j++) { if (c.graph.nodes[gearcount].localVariables.Count <= j) { c.graph.nodes[gearcount].localVariables.Add(double.NaN); } } c.graph.nodes[gearcount].localVariables[3] = stateVarsStar[gearcount, 3]; c.graph.nodes[gearcount].localVariables[4] = stateVarsStar[gearcount, 4]; c.graph.nodes[gearcount].localVariables[5] = stateVarsStar[gearcount, 5]; c.graph.nodes[gearcount].localVariables[6] = stateVarsStar[gearcount, 6]; c.graph.nodes[gearcount].localVariables[7] = stateVarsStar[gearcount, 7]; c.graph.nodes[gearcount].localVariables[8] = stateVarsStar[gearcount, 8]; string filename = Program.settings.outputDirectory + "visualizerOutput1.txt"; FileStream fs = new FileStream(filename, FileMode.Create); StreamWriter outputWriter = new StreamWriter(fs); outputWriter.WriteLine(outputString); outputWriter.WriteLine(outputString2); outputWriter.Close(); fs.Close(); }