当前位置: 首页>>代码示例>>C#>>正文


C# Solver.MakeNumVar方法代码示例

本文整理汇总了C#中Solver.MakeNumVar方法的典型用法代码示例。如果您正苦于以下问题:C# Solver.MakeNumVar方法的具体用法?C# Solver.MakeNumVar怎么用?C# Solver.MakeNumVar使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Solver的用法示例。


在下文中一共展示了Solver.MakeNumVar方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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());

  }
开发者ID:HanumathRao,项目名称:hakank,代码行数:58,代码来源:volsay.cs

示例2: TestVarOperator

 static void TestVarOperator()
 {
   Console.WriteLine("Running TestVarOperator");
   Solver solver = new Solver("TestVarOperator",
                              Solver.CLP_LINEAR_PROGRAMMING);
   Variable x = solver.MakeNumVar(0.0, 100.0, "x");
   Constraint ct1 = solver.Add(x >= 1);
   Constraint ct2 = solver.Add(x <= 1);
   Constraint ct3 = solver.Add(x == 1);
   Constraint ct4 = solver.Add(1 >= x);
   Constraint ct5 = solver.Add(1 <= x);
   Constraint ct6 = solver.Add(1 == x);
   CheckDoubleEq(ct1.GetCoefficient(x), 1.0, "test1");
   CheckDoubleEq(ct2.GetCoefficient(x), 1.0, "test2");
   CheckDoubleEq(ct3.GetCoefficient(x), 1.0, "test3");
   CheckDoubleEq(ct4.GetCoefficient(x), 1.0, "test4");
   CheckDoubleEq(ct5.GetCoefficient(x), 1.0, "test5");
   CheckDoubleEq(ct6.GetCoefficient(x), 1.0, "test6");
   CheckDoubleEq(ct1.Lb(), 1.0, "test7");
   CheckDoubleEq(ct1.Ub(), double.PositiveInfinity, "test8");
   CheckDoubleEq(ct2.Lb(), double.NegativeInfinity, "test9");
   CheckDoubleEq(ct2.Ub(), 1.0, "test10");
   CheckDoubleEq(ct3.Lb(), 1.0, "test11");
   CheckDoubleEq(ct3.Ub(), 1.0, "test12");
   CheckDoubleEq(ct4.Lb(), double.NegativeInfinity, "test13");
   CheckDoubleEq(ct4.Ub(), 1.0, "test14");
   CheckDoubleEq(ct5.Lb(), 1.0, "test15");
   CheckDoubleEq(ct5.Ub(), double.PositiveInfinity, "test16");
   CheckDoubleEq(ct6.Lb(), 1.0, "test17");
   CheckDoubleEq(ct6.Ub(), 1.0, "test18");
 }
开发者ID:RickOne16,项目名称:or-tools,代码行数:31,代码来源:testlp.cs

示例3: TestVarAddition

 static void TestVarAddition()
 {
   Console.WriteLine("Running TestVarAddition");
   Solver solver = new Solver("TestVarAddition",
                              Solver.CLP_LINEAR_PROGRAMMING);
   Variable x = solver.MakeNumVar(0.0, 100.0, "x");
   Variable y = solver.MakeNumVar(0.0, 100.0, "y");
   Constraint ct1 = solver.Add(x + y == 1);
   CheckDoubleEq(ct1.GetCoefficient(x), 1.0, "test1");
   CheckDoubleEq(ct1.GetCoefficient(y), 1.0, "test2");
   Constraint ct2 = solver.Add(x + x == 1);
   CheckDoubleEq(ct2.GetCoefficient(x), 2.0, "test3");
   Constraint ct3 = solver.Add(x + (y + x) == 1);
   CheckDoubleEq(ct3.GetCoefficient(x), 2.0, "test4");
   CheckDoubleEq(ct3.GetCoefficient(y), 1.0, "test5");
   Constraint ct4 = solver.Add(x + (y + x + 3) == 1);
   CheckDoubleEq(ct4.GetCoefficient(x), 2.0, "test4");
   CheckDoubleEq(ct4.GetCoefficient(y), 1.0, "test5");
   CheckDoubleEq(ct4.Lb(), -2.0, "test6");
   CheckDoubleEq(ct4.Ub(), -2.0, "test7");
 }
开发者ID:RickOne16,项目名称:or-tools,代码行数:21,代码来源:testlp.cs

示例4: 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());

  }
开发者ID:HanumathRao,项目名称:hakank,代码行数:87,代码来源:volsay3.cs

示例5: TestVarMultiplication

 static void TestVarMultiplication()
 {
   Console.WriteLine("Running TestVarMultiplication");
   Solver solver = new Solver("TestVarMultiplication",
                                  Solver.CLP_LINEAR_PROGRAMMING);
   Variable x = solver.MakeNumVar(0.0, 100.0, "x");
   Variable y = solver.MakeNumVar(0.0, 100.0, "y");
   Constraint ct1 = solver.Add(3 * x == 1);
   CheckDoubleEq(ct1.GetCoefficient(x), 3.0, "test1");
   Constraint ct2 = solver.Add(x * 3 == 1);
   CheckDoubleEq(ct2.GetCoefficient(x), 3.0, "test2");
   Constraint ct3 = solver.Add(x + (2 * y + 3 * x) == 1);
   CheckDoubleEq(ct3.GetCoefficient(x), 4.0, "test3");
   CheckDoubleEq(ct3.GetCoefficient(y), 2.0, "test4");
   Constraint ct4 = solver.Add(x + 5 * (y + x + 3) == 1);
   CheckDoubleEq(ct4.GetCoefficient(x), 6.0, "test5");
   CheckDoubleEq(ct4.GetCoefficient(y), 5.0, "test6");
   CheckDoubleEq(ct4.Lb(), -14.0, "test7");
   CheckDoubleEq(ct4.Ub(), -14.0, "test8");
   Constraint ct5 = solver.Add(x + (2 * y + x + 3) * 3 == 1);
   CheckDoubleEq(ct5.GetCoefficient(x), 4.0, "test9");
   CheckDoubleEq(ct5.GetCoefficient(y), 6.0, "test10");
   CheckDoubleEq(ct5.Lb(), -8.0, "test11");
   CheckDoubleEq(ct5.Ub(), -8.0, "test12");
 }
开发者ID:RickOne16,项目名称:or-tools,代码行数:25,代码来源:testlp.cs

示例6: 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");
 }
开发者ID:RickOne16,项目名称:or-tools,代码行数:16,代码来源:testlp.cs

示例7: TestInequalities

 static void TestInequalities()
 {
   Console.WriteLine("Running TestInequalities");
   Solver solver = new Solver("TestInequalities",
                              Solver.CLP_LINEAR_PROGRAMMING);
   Variable x = solver.MakeNumVar(0.0, 100.0, "x");
   Variable y = solver.MakeNumVar(0.0, 100.0, "y");
   Constraint ct1 = solver.Add(2 * (x + 3) + 5 * (y + x -1) >= 3);
   CheckDoubleEq(ct1.GetCoefficient(x), 7.0, "test1");
   CheckDoubleEq(ct1.GetCoefficient(y), 5.0, "test2");
   CheckDoubleEq(ct1.Lb(), 2.0, "test3");
   CheckDoubleEq(ct1.Ub(), double.PositiveInfinity, "test4");
   Constraint ct2 = solver.Add(2 * (x + 3) + 5 * (y + x -1) <= 3);
   CheckDoubleEq(ct2.GetCoefficient(x), 7.0, "test5");
   CheckDoubleEq(ct2.GetCoefficient(y), 5.0, "test6");
   CheckDoubleEq(ct2.Lb(), double.NegativeInfinity, "test7");
   CheckDoubleEq(ct2.Ub(), 2.0, "test8");
   Constraint ct3 = solver.Add(2 * (x + 3) + 5 * (y + x -1) >= 3 - x - y);
   CheckDoubleEq(ct3.GetCoefficient(x), 8.0, "test9");
   CheckDoubleEq(ct3.GetCoefficient(y), 6.0, "test10");
   CheckDoubleEq(ct3.Lb(), 2.0, "test11");
   CheckDoubleEq(ct3.Ub(), double.PositiveInfinity, "test12");
   Constraint ct4 = solver.Add(2 * (x + 3) + 5 * (y + x -1) <= -x - y + 3);
   CheckDoubleEq(ct4.GetCoefficient(x), 8.0, "test13");
   CheckDoubleEq(ct4.GetCoefficient(y), 6.0, "test14");
   CheckDoubleEq(ct4.Lb(), double.NegativeInfinity, "test15");
   CheckDoubleEq(ct4.Ub(), 2.0, "test16");
 }
开发者ID:RickOne16,项目名称:or-tools,代码行数:28,代码来源:testlp.cs

示例8: TestBinaryOperations

 static void TestBinaryOperations()
 {
   Console.WriteLine("Running TestBinaryOperations");
   Solver solver = new Solver("TestBinaryOperations",
                              Solver.CLP_LINEAR_PROGRAMMING);
   Variable x = solver.MakeNumVar(0.0, 100.0, "x");
   Variable y = solver.MakeNumVar(0.0, 100.0, "y");
   Constraint ct1 = solver.Add(x == y);
   CheckDoubleEq(ct1.GetCoefficient(x), 1.0, "test1");
   CheckDoubleEq(ct1.GetCoefficient(y), -1.0, "test2");
   Constraint ct2 = solver.Add(x == 3 * y + 5);
   CheckDoubleEq(ct2.GetCoefficient(x), 1.0, "test3");
   CheckDoubleEq(ct2.GetCoefficient(y), -3.0, "test4");
   CheckDoubleEq(ct2.Lb(), 5.0, "test5");
   CheckDoubleEq(ct2.Ub(), 5.0, "test6");
   Constraint ct3 = solver.Add(2 * x - 9 == y);
   CheckDoubleEq(ct3.GetCoefficient(x), 2.0, "test7");
   CheckDoubleEq(ct3.GetCoefficient(y), -1.0, "test8");
   CheckDoubleEq(ct3.Lb(), 9.0, "test9");
   CheckDoubleEq(ct3.Ub(), 9.0, "test10");
   Check(x == x, "test11");
   Check(!(x == y), "test12");
   Check(!(x != x), "test13");
   Check((x != y), "test14");
 }
开发者ID:RickOne16,项目名称:or-tools,代码行数:25,代码来源:testlp.cs

示例9: Solve

  /**
   *
   * Volsay problem.
   *
   * From the OPL model volsay.mod.
   *
   * 
   *
   * Also see 
   *  http://www.hakank.org/or-tools/volsay.cs
   *  http://www.hakank.org/or-tools/volsay2.py 
   *
   */
  private static void Solve()
  {

    Solver solver = new Solver("Volsay2",
                               Solver.CLP_LINEAR_PROGRAMMING);


    int num_products = 2;
    IEnumerable<int> PRODUCTS = Enumerable.Range(0, num_products);
    int Gas = 0;
    int Chloride = 1;
    String[] products = {"Gas", "Chloride"};

    //
    // Variables
    //
    Variable[] production = new Variable[num_products];
    foreach(int p in PRODUCTS) {
      production[p] = solver.MakeNumVar(0, 100000, products[p]);
    }

    int num_constraints = 2;
    IEnumerable<int> CONSTRAINTS = Enumerable.Range(0, num_constraints);
    Constraint[] cons = new Constraint[num_constraints];
    cons[0] = solver.Add(production[Gas] + production[Chloride] <= 50);
    cons[1] = solver.Add(3 * production[Gas] + 4 * production[Chloride] <= 180);


    solver.Maximize(40 * production[Gas] + 50 * production[Chloride]);

    Console.WriteLine("NumConstraints: {0}", solver.NumConstraints());

    int resultStatus = solver.Solve();

    if (resultStatus != Solver.OPTIMAL) {
      Console.WriteLine("The problem don't have an optimal solution.");
      return;
    }

    foreach(int p in PRODUCTS) {
      Console.WriteLine("{0,-10}: {1} ReducedCost: {2}", 
                        products[p],
                        production[p].SolutionValue(), 
                        production[p].ReducedCost());
    }


    foreach(int c in CONSTRAINTS) {
      Console.WriteLine("Constraint {0} DualValue {1} Activity: {2} lb: {3} ub: {4}", 
                        c.ToString(),
                        cons[c].DualValue(), 
                        cons[c].Activity(),
                        cons[c].Lb(),
                        cons[c].Ub()
                        );
    }



    Console.WriteLine("\nWallTime: " + solver.WallTime());
    Console.WriteLine("Iterations: " + solver.Iterations());

  }
开发者ID:RickOne16,项目名称:or-tools,代码行数:76,代码来源:volsay2.cs


注:本文中的Solver.MakeNumVar方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。