public static IList <Tuple <Ore, int> > MiningPlanToEarnSpecifiedGoldAndGems(double hpc, double goldNeeded, double gemsNeeded) { IList <Ore> localOreList = BuildFeasibleOreList(hpc); SolverContext context = SolverContext.GetContext(); Model model = context.CreateModel(); Set items = new Set(Domain.Any, "items"); Decision mine = new Decision(Domain.IntegerNonnegative, "mine", items); model.AddDecision(mine); Parameter value = new Parameter(Domain.RealNonnegative, "value", items); value.SetBinding(localOreList, "Value", "Name"); Parameter clicks = new Parameter(Domain.IntegerNonnegative, "clicks", items); clicks.SetBinding(localOreList, "Clicks", "Name"); model.AddParameters(value, clicks); model.AddConstraint("knap_value", Model.Sum(Model.ForEach(items, t => mine[t] * value[t])) >= goldNeeded); model.AddConstraint("knap_gems", Model.Sum(Model.ForEach(items, t => mine[t])) >= gemsNeeded); model.AddGoal("knap_time", GoalKind.Minimize, Model.Sum(Model.ForEach(items, t => mine[t] * clicks[t]))); var report = context.CheckModel(); Console.Write(report); Solution solution = context.Solve(new SimplexDirective()); //Report report = solution.GetReport(); List <Tuple <Ore, int> > retval = new List <Tuple <Ore, int> >(); for (int i = 0; i < localOreList.Count; i++) { int temp = (int)mine.GetDouble(i); retval.Add(Tuple.Create(localOreList[i], temp)); } return(retval); }
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); }
private void FindSolution(IEnumerable <Node> nodes, IEnumerable <Link> links, List <SolverWorker> workers) { SolverContext context = SolverContext.GetContext(); Model model = context.CreateModel(); var nodeSet = new Set(0, nodes.Count(), 1); var workerSet = new Set(0, workers.Count(), 1); //-------------Parameters-------------- var weights = new Parameter(Domain.IntegerNonnegative, "weights", nodeSet); weights.SetBinding(nodes, "Weight", "ID"); var dependencies = new Parameter(Domain.IntegerRange(0, 1), "dependencies", nodeSet, nodeSet); dependencies.SetBinding(links, "isDependent", "Source", "Parent"); model.AddParameters(weights, dependencies); //-------------Decisions-------------- var startTimes = new Decision(Domain.IntegerNonnegative, "starts", nodeSet); var finishTimes = new Decision(Domain.IntegerNonnegative, "finishes", nodeSet); var makespan = new Decision(Domain.IntegerNonnegative, "makespan"); var allocation = new Decision(Domain.IntegerRange(0, 1), "allocation", nodeSet, workerSet); model.AddDecisions(startTimes, finishTimes, makespan, allocation); //-------------Constraints-------------- model.AddConstraint("FinishTime", Model.ForEach(nodeSet, (node) => startTimes[node] + weights[node] == finishTimes[node])); //model.AddConstraint("OneAtATime", Model.ForEach(nodeSet, (n) => // Model.ForEachWhere(nodeSet, (n2) => Model.Or(finishTimes[n] < startTimes[n2], startTimes[n] > finishTimes[n2]), (n2) => n != n2))); model.AddConstraint("Allocatee", Model.ForEach(nodeSet, (n) => Model.Sum(Model.ForEach(workerSet, (w) => allocation[n, w])) == 1)); //model.AddConstraint("Allocatee", Model.ForEach(nodeSet, (n) => Model.ExactlyMofN(1,allocation[n]))); model.AddConstraint("OneAtATime", Model.ForEach(workerSet, (w) => Model.ForEach(nodeSet, (n) => Model.ForEachWhere(nodeSet, (n2) => Model.Implies(Model.And(allocation[n, w] == 1, allocation[n2, w] == 1), Model.Or(finishTimes[n] <= startTimes[n2], startTimes[n] >= finishTimes[n2])), (n2) => n != n2)))); model.AddConstraint("PrecedenceConstraints", Model.ForEach(nodeSet, task => Model.ForEach(nodeSet, parent => Model.Implies(dependencies[task, parent] == 1, startTimes[task] >= finishTimes[parent])))); //model.AddConstraint("ProjectFinish", Model.ForEach(nodeSet, (n) => makespan >= finishTimes[n])); model.AddConstraint("ProjectFinish", makespan == Model.Max(Model.ForEach(nodeSet, (n) => finishTimes[n]))); model.AddGoal("MinMakeSpan", GoalKind.Minimize, makespan); context.CheckModel(); //using (StreamWriter sw = new StreamWriter("Stadium.oml")) { // context.SaveModel(FileFormat.OML, sw); ; //} Solution solution = context.Solve(); Report report = solution.GetReport(); Console.WriteLine(@"===== report ====="); Console.Write("{0}", report); Console.ReadLine(); context.ClearModel(); }