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


C# Solver.MakeSearchLog方法代码示例

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


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

示例1: Solve

  /**
   *
   * Golomb Ruler problem.
   *
   * This C# implementation is based on Charles Prud'homme's
   * or-tools/Java model:
   * http://code.google.com/p/or-tools/source/browse/trunk/com/google/ortools/constraintsolver/samples/GolombRuler.java
   *
   */
  private static void Solve(int m = 8)
  {
    Solver solver = new Solver("GolombRuler");


    //
    // Decision variables
    //
    IntVar[] ticks =  solver.MakeIntVarArray(m,
                                             0,
                                             ((m < 31) ? (1 << (m + 1)) - 1 : 9999),
                                             "ticks");

    IntVar[] diff = new IntVar[(m * m - m) / 2];


    //
    // Constraints
    //
    solver.Add(ticks[0] == 0);

    for(int i = 0; i < ticks.Length - 1; i++) {
      solver.Add(ticks[i] < ticks[i+1]);
    }


    for (int k = 0, i = 0; i < m - 1; i++) {
      for (int j = i + 1; j < m; j++, k++) {
        diff[k] = (ticks[j]-ticks[i]).Var();
        solver.Add(diff[k] >= (j - i) * (j - i + 1) / 2);
      }
    }

    solver.Add(diff.AllDifferent());

    // break symetries
    if (m > 2) {
      solver.Add(diff[0] < diff[diff.Length - 1]);
    }


    //
    // Optimization
    //
    OptimizeVar opt = ticks[m - 1].Minimize(1);


    //
    // Search
    //
    DecisionBuilder db = solver.MakePhase(ticks,
                                          Solver.CHOOSE_MIN_SIZE_LOWEST_MIN,
                                          Solver.ASSIGN_MIN_VALUE);

    // We just want the debug info for larger instances.
    if (m >= 11) {

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

    } else {

      solver.NewSearch(db, opt);
    }


    while (solver.NextSolution()) {
      Console.Write("opt: {0}  [ ", ticks[m-1].Value());
      for(int i = 0; i < m; i++) {
          Console.Write("{0} ", ticks[i].Value());
      }
      Console.WriteLine("]");
    }

    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:9thbit,项目名称:csplib,代码行数:91,代码来源:golomb_ruler.cs

示例2: BasicLsWithFilter

 private static void BasicLsWithFilter()
 {
   Console.WriteLine("BasicLsWithFilter");
   Solver solver = new Solver("BasicLs");
   IntVar[] vars = solver.MakeIntVarArray(4, 0, 4, "vars");
   IntVar sum_var = vars.Sum().Var();
   OptimizeVar obj = sum_var.Minimize(1);
   DecisionBuilder db = solver.MakePhase(vars,
                                         Solver.CHOOSE_FIRST_UNBOUND,
                                         Solver.ASSIGN_MAX_VALUE);
   MoveOneVar move_one_var = new MoveOneVar(vars);
   SumFilter filter = new SumFilter(vars);
   IntVarLocalSearchFilter[] filters =
       new IntVarLocalSearchFilter[] { filter };
   LocalSearchPhaseParameters ls_params =
       solver.MakeLocalSearchPhaseParameters(move_one_var, db, null, filters);
   DecisionBuilder ls = solver.MakeLocalSearchPhase(vars, db, ls_params);
   SolutionCollector collector = solver.MakeLastSolutionCollector();
   collector.AddObjective(sum_var);
   SearchMonitor log = solver.MakeSearchLog(1000, obj);
   solver.Solve(ls, collector, obj, log);
   Console.WriteLine("Objective value = {0}", collector.ObjectiveValue(0));
 }
开发者ID:RickOne16,项目名称:or-tools,代码行数:23,代码来源:csls_api.cs

示例3: 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

示例4: Solve


//.........这里部分代码省略.........
    for(int day = 0; day < num_days; day++) {
      IntVar[] nurses = new IntVar[num_nurses];
      for(int nurse = 0; nurse < num_nurses; nurse++) {
        nurses[nurse] = x[nurse, day];
      }
      IntVar[] stats = new IntVar[num_shifts];
      for (int shift = 0; shift < num_shifts; ++shift)
      {
        stats[shift] = day_stat[day, shift];
      }
      solver.Add(nurses.Distribute(stats));

      //
      // Some constraints for each day:
      //
      // Note: We have a strict requirements of
      //       the number of shifts.
      //       Using atleast constraints is harder
      //       in this model.
      //
      if (day % 7 == 5 || day % 7 == 6) {
        // special constraints for the weekends
        solver.Add(day_stat[day, day_shift] == 2 * nurse_multiplier);
        solver.Add(day_stat[day, night_shift] == nurse_multiplier);
        solver.Add(day_stat[day, off_shift] == 4 * nurse_multiplier);
      } else {
        // for workdays:

        // - exactly 3 on day shift
        solver.Add(day_stat[day, day_shift] == 3 * nurse_multiplier);
        // - exactly 2 on night
        solver.Add(day_stat[day, night_shift] == 2 * nurse_multiplier);
        // - exactly 2 off duty
        solver.Add(day_stat[day, off_shift] == 2 * nurse_multiplier);
      }
    }

    //
    // Search
    //
    DecisionBuilder db = solver.MakePhase(x_flat,
                                          Solver.CHOOSE_FIRST_UNBOUND,
                                          Solver.ASSIGN_MIN_VALUE);

    SearchMonitor log = solver.MakeSearchLog(1000000);

    solver.NewSearch(db, log);

    int num_solutions = 0;
    while (solver.NextSolution()) {
      num_solutions++;
      for(int i = 0; i < num_nurses; i++) {
        Console.Write("Nurse #{0,-2}: ", i);
        var occ = new Dictionary<int, int>();
        for(int j = 0; j < num_days; j++) {
          int v = (int)x[i,j].Value()-1;
          if (!occ.ContainsKey(v)) {
            occ[v] = 0;
          }
          occ[v]++;
          Console.Write(days[v] + " ");
        }

        Console.Write(" #workdays: {0,2}", nurse_stat[i].Value());
        foreach(int s in valid_shifts) {
          int v = 0;
          if (occ.ContainsKey(s-1)) {
            v = occ[s-1];
          }
          Console.Write("  {0}:{1}", days[s-1], v);
        }
        Console.WriteLine();

      }
      Console.WriteLine();

      Console.WriteLine("Statistics per day:\nDay      d n o");
      for(int j = 0; j < num_days; j++) {
        Console.Write("Day #{0,2}: ", j);
        foreach(int t in valid_shifts) {
          Console.Write(day_stat[j,t].Value() + " ");
        }
        Console.WriteLine();
      }
      Console.WriteLine();

      // We just show 2 solutions
      if (num_solutions > 1) {
        break;
      }
    }

    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,代码行数:101,代码来源:nurse_rostering_transition.cs


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