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


C# IntVar.ScalProd方法代码示例

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


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

示例1: Solve

  /**
   *
   * This is a port of Charles Prud'homme's Java model
   * Partition.java
   * """
   * Partition n numbers into two groups, so that
   * - the sum of the first group equals the sum of the second,
   * - and the sum of the squares of the first group equals the sum of
   *   the squares of the second
   * """
   *
   */
  private static void Solve(int m)
  {

    Solver solver = new Solver("Partition");


    //
    // Decision variables
    //
    IntVar[] x = solver.MakeIntVarArray(m, 1, 2 * m, "x");
    IntVar[] y = solver.MakeIntVarArray(m, 1, 2 * m, "y");


    //
    // Constraints
    //
       // break symmetries
    for (int i = 0; i < m - 1; i++) {
      solver.Add(x[i] < x[i + 1]);
      solver.Add(y[i] < y[i + 1]);
    }
    solver.Add(x[0] < y[0]);

    IntVar[] xy = new IntVar[2 * m];
    for (int i = m - 1; i >= 0; i--) {
      xy[i] = x[i];
      xy[m + i] = y[i];
    }

    solver.Add(xy.AllDifferent());

    int[] coeffs = new int[2 * m];
    for (int i = m - 1; i >= 0; i--) {
      coeffs[i] = 1;
      coeffs[m + i] = -1;
    }
    solver.Add(xy.ScalProd(coeffs) == 0);

    IntVar[] sxy, sx, sy;
    sxy = new IntVar[2 * m];
    sx = new IntVar[m];
    sy = new IntVar[m];
    for (int i = m - 1; i >= 0; i--) {
      sx[i] = x[i].Square().Var();
      sxy[i] = sx[i];
      sy[i] = y[i].Square().Var();
      sxy[m + i] = sy[i];
    }
    solver.Add(sxy.ScalProd(coeffs) == 0);

    solver.Add(x.Sum() == 2 * m * (2 * m + 1) / 4);
    solver.Add(y.Sum() == 2 * m * (2 * m + 1) / 4);
    solver.Add(sx.Sum() == 2 * m * (2 * m + 1) * (4 * m + 1) / 12);
    solver.Add(sy.Sum() == 2 * m * (2 * m + 1) * (4 * m + 1) / 12);

    //
    // Search
    //
    DecisionBuilder db = solver.MakeDefaultPhase(xy);

    SearchMonitor log = solver.MakeSearchLog(10000);
    solver.NewSearch(db, log);

    while (solver.NextSolution()) {
      for(int i = 0; i < m; i++) {
        Console.Write("[" + xy[i].Value() + "] ");
      }
      Console.WriteLine();
      for(int i = 0; i < m; i++) {
        Console.Write("[" + xy[m+i].Value() + "] ");
      }
      Console.WriteLine("\n");
    }

    Console.WriteLine("\nSolutions: {0}", solver.Solutions());
    Console.WriteLine("WallTime: {0}ms", solver.WallTime());
    Console.WriteLine("Failures: {0}", solver.Failures());
    Console.WriteLine("Branches: {0} ", solver.Branches());

    solver.EndSearch();

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


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