本文整理汇总了C#中Solver.Objective方法的典型用法代码示例。如果您正苦于以下问题:C# Solver.Objective方法的具体用法?C# Solver.Objective怎么用?C# Solver.Objective使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Solver
的用法示例。
在下文中一共展示了Solver.Objective方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Solve
/**
*
* Volsay problem.
*
* From the OPL model volsay.mod.
*
*
* Also see http://www.hakank.org/or-tools/volsay.py
*
*/
private static void Solve()
{
Solver solver = new Solver("Volsay", Solver.CLP_LINEAR_PROGRAMMING);
//
// Variables
//
Variable Gas = solver.MakeNumVar(0, 100000, "Gas");
Variable Chloride = solver.MakeNumVar(0, 100000, "Cloride");
Constraint c1 = solver.Add(Gas + Chloride <= 50);
Constraint c2 = solver.Add(3 * Gas + 4 * Chloride <= 180);
solver.Maximize(40 * Gas + 50 * Chloride);
int resultStatus = solver.Solve();
if (resultStatus != Solver.OPTIMAL) {
Console.WriteLine("The problem don't have an optimal solution.");
return;
}
Console.WriteLine("Objective: {0}", solver.Objective().Value());
Console.WriteLine("Gas : {0} ReducedCost: {1}",
Gas.SolutionValue(),
Gas.ReducedCost());
Console.WriteLine("Chloride : {0} ReducedCost: {1}",
Chloride.SolutionValue(),
Chloride.ReducedCost());
Console.WriteLine("c1 : DualValue: {0} Activity: {1}",
c1.DualValue(),
c1.Activity());
Console.WriteLine("c2 : DualValue: {0} Activity: {1}",
c2.DualValue(),
c2.Activity());
Console.WriteLine("\nWallTime: " + solver.WallTime());
Console.WriteLine("Iterations: " + solver.Iterations());
}
示例2: TestObjective
static void TestObjective()
{
Console.WriteLine("Running TestObjective");
Solver solver = new Solver("TestObjective", Solver.CLP_LINEAR_PROGRAMMING);
Variable x = solver.MakeNumVar(0.0, 100.0, "x");
Variable y = solver.MakeNumVar(0.0, 100.0, "y");
solver.Maximize(x);
CheckDoubleEq(0.0, solver.Objective().Offset(), "test1");
CheckDoubleEq(1.0, solver.Objective().GetCoefficient(x), "test2");
Check(solver.Objective().Maximization(), "test3");
solver.Minimize(-x - 2 * y + 3);
CheckDoubleEq(3.0, solver.Objective().Offset(), "test4");
CheckDoubleEq(-1.0, solver.Objective().GetCoefficient(x), "test5");
CheckDoubleEq(-2.0, solver.Objective().GetCoefficient(y), "test6");
Check(solver.Objective().Minimization(), "test7");
}
示例3: Solve
/**
*
* Volsay problem.
*
* From the OPL model volsay.mod.
* This version use arrays and matrices
*
*
* Also see
* http://www.hakank.org/or-tools/volsay2.cs
* http://www.hakank.org/or-tools/volsay3.py
*
*/
private static void Solve()
{
Solver solver = new Solver("Volsay3",
Solver.CLP_LINEAR_PROGRAMMING);
int num_products = 2;
IEnumerable<int> PRODUCTS = Enumerable.Range(0, num_products);
String[] products = {"Gas", "Chloride"};
String[] components = {"nitrogen", "hydrogen", "chlorine"};
int[,] demand = { {1,3,0}, {1,4,1}};
int[] profit = {30,40};
int[] stock = {50,180,40};
//
// Variables
//
Variable[] production = new Variable[num_products];
foreach(int p in PRODUCTS) {
production[p] = solver.MakeNumVar(0, 100000, products[p]);
}
//
// Constraints
//
int c_len = components.Length;
Constraint[] cons = new Constraint[c_len];
for(int c = 0; c < c_len; c++) {
cons[c] = solver.Add( (from p in PRODUCTS
select (demand[p,c]*production[p])).
ToArray().Sum() <= stock[c]);
}
//
// Objective
//
solver.Maximize( (from p in PRODUCTS
select (profit[p]*production[p])).
ToArray().Sum()
);
if (solver.Solve() != Solver.OPTIMAL) {
Console.WriteLine("The problem don't have an optimal solution.");
return;
}
Console.WriteLine("Objective: {0}", solver.Objective().Value());
foreach(int p in PRODUCTS) {
Console.WriteLine("{0,-10}: {1} ReducedCost: {2}",
products[p],
production[p].SolutionValue(),
production[p].ReducedCost());
}
for(int c = 0; c < c_len; c++) {
Console.WriteLine("Constraint {0} DualValue {1} Activity: {2} lb: {3} ub: {4}",
c,
cons[c].DualValue(),
cons[c].Activity(),
cons[c].Lb(),
cons[c].Ub());
}
Console.WriteLine("\nWallTime: " + solver.WallTime());
Console.WriteLine("Iterations: " + solver.Iterations());
}