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


C# Solver.NumConstraints方法代码示例

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


在下文中一共展示了Solver.NumConstraints方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

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