static void LoadModel(SolverContext context, string fileName) { string ext = Path.GetExtension(fileName).ToLower(); if (ext == ".mps") { context.LoadModel(FileFormat.MPS, Path.GetFullPath(fileName)); } else if (ext == ".smps") { context.LoadModel(FileFormat.SMPS, Path.GetFullPath(fileName)); } else if (ext == ".oml") { context.LoadModel(FileFormat.OML, Path.GetFullPath(fileName)); } else { throw new NotSupportedException("This file format hasn't been supported."); } }
static void Main(string[] args) { { SolverContext context = SolverContext.GetContext(); // Load a model from file if (args.Length > 0) { using (TextReader streamReader = new StreamReader(args[0])) { context.LoadModel(FileFormat.MPS, streamReader); } // Select the Mosek interior point optimizer. MosekInteriorPointMethodDirective d = new MosekInteriorPointMethodDirective(); // Mosek specific parameters may optionally be set. // d[mosek.dparam.optimizer_max_time] = 100.0; // Optionally write log information to console using two lines below System.Diagnostics.ConsoleTraceListener listener = new System.Diagnostics.ConsoleTraceListener(); d.AddListener(listener); // Solve the problem Solution sol = context.Solve(d); // Print solution Report report = sol.GetReport(); } else { Console.WriteLine("Usage: MosekMsfExample filename"); } Console.WriteLine("Please press any key."); Console.ReadKey(); } }
private Dictionary <String, double> solve(List <BinaryOption> variables, List <double> results, List <List <BinaryOption> > configurations, List <Interaction> interactions) { Dictionary <String, double> optionInfluences = new Dictionary <string, double>(); foreach (BinaryOption elem in variables) { optionInfluences.Add(elem.Name, 0); } if (interactions != null) { foreach (Interaction inter in interactions) { optionInfluences.Add(inter.Name, 0); } } //Building the OML model StringBuilder omlModel = new StringBuilder(); omlModel.Append("Model["); String decisions = generateDecisionOML(variables, results); omlModel.Append(decisions + ","); String constraints = generateConstraintOML(variables, configurations, interactions, results); omlModel.Append(constraints + ","); String goal = generateGoalOML(results.Count); omlModel.Append(goal + "]"); Console.WriteLine(omlModel); SolverContext context = SolverContext.GetContext(); context.ClearModel(); //String model = "Model[Decisions[Reals[0,Infinity], f1,f2,y1,y2,y3],Constraints[f1+y1==1,f1+f2+y2==4,f2+y3==3],Goals[Minimize[y1+y2+y3]]]"; //String model = "Model[Decisions[Reals[0,Infinity],fp1,fn1,fp2,fn2,yp1,yn1,yp2,yn2,yp3,yn3],Constraints[fp1-fn1+yp1-yn1==1,fp1-fn1+fp2-fn2+yp2-yn2==0,fp2-fn2+yp3-yn3==-1],Goals[Minimize[yp1+yn1+yp2+yn2+yp3+yn3]]]"; //Console.WriteLine(omlModel.ToString()); context.LoadModel(FileFormat.OML, new StringReader(omlModel.ToString())); //List<double> erglist = new List<double>(); //Solve the optimization problem if (context.CheckModel().IsValid) { Solution solution = context.Solve(); StringBuilder sb = new StringBuilder(); foreach (Decision d in solution.Decisions) { sb.Append(d.ToString() + "; "); //Constructing feature Values string option = d.Name.Substring(0, d.Name.Length - 2); if (d.Name.EndsWith("_p")) { optionInfluences[option] += d.ToDouble(); } else if (d.Name.EndsWith("_n")) { optionInfluences[option] -= d.ToDouble(); } } /*if (this.withStandardDeviation && this.standardDeviation != 0) * { * foreach (BinaryOption elem in variables) * { * if(featureValues[elem.Name] == 0) * continue; * int configNb = 0; * foreach (List<BinaryOption> config in configurations) * { * bool derivativeRequired = true; * foreach(Element parent in elem.getDerivativeParents()) * { * if(config.Contains(parent) == false) * { * derivativeRequired = false; * break; * } * } * if(derivativeRequired) * { * double equationValue = results[configNb]; * if (Math.Abs(featureValues[elem.Name]) < Math.Abs(equationValue * this.standardDeviation / 100)) * { * featureValues[elem.Name] = 0; * break; * } * } * configNb++; * } * } * }*/ } return(optionInfluences); }
static void ValidateZ3(string fileName, Z3BaseDirective directive) { SolverContext context = SolverContext.GetContext(); try { LoadModel(context, fileName); if (context.CurrentModel.Goals.Any()) { var msfDirective = (directive is Z3MILPDirective) ? (Directive) new MixedIntegerProgrammingDirective() { TimeLimit = 10000 } : (Directive) new Directive() { TimeLimit = 10000 }; var sol1 = context.Solve(msfDirective); Console.WriteLine("Solved the model using MSF."); Console.Write("{0}", sol1.GetReport()); var expectedGoals = sol1.Goals.Select(x => x.ToDouble()); context.ClearModel(); context.LoadModel(FileFormat.OML, Path.GetFullPath(fileName)); directive.SMT2LogFile = Path.ChangeExtension(fileName, ".smt2"); var sol2 = context.Solve(directive); //Console.Write("{0}", sol2.GetReport()); var actualGoals = sol2.Goals.Select(x => x.ToDouble()); Console.WriteLine("Solved the model using Z3."); var goalPairs = expectedGoals.Zip(actualGoals, (expected, actual) => new { expected, actual }).ToArray(); bool validated = goalPairs.All(p => Math.Abs(p.expected - p.actual) <= 0.0001); if (validated) { Console.WriteLine("INFO: Two solvers give approximately the same results."); } else { Console.Error.WriteLine("ERROR: Discrepancy found between results."); if (!validated && File.Exists(directive.SMT2LogFile)) { var sb = new StringBuilder(); for (int i = 0; i < goalPairs.Length; i++) { sb.AppendFormat("\n(echo \"Goal {0}: actual |-> {1:0.0000}, expected |-> {2:0.0000}\")", i + 1, goalPairs[i].actual, goalPairs[i].expected); } Console.Error.WriteLine(sb.ToString()); File.AppendAllText(directive.SMT2LogFile, sb.ToString()); } } } else { Console.WriteLine("Ignoring this instance without having any goal."); } } catch (Exception e) { Console.WriteLine("Skipping unsolvable instance in {0} with error message '{1}'.", fileName, e.Message); } finally { context.ClearModel(); } }