本文整理汇总了C#中Solver.ObjectiveValue方法的典型用法代码示例。如果您正苦于以下问题:C# Solver.ObjectiveValue方法的具体用法?C# Solver.ObjectiveValue怎么用?C# Solver.ObjectiveValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Solver
的用法示例。
在下文中一共展示了Solver.ObjectiveValue方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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.ObjectiveValue());
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: 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.ObjectiveValue());
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());
}