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


C# Solver.MakeModuloConstraint方法代码示例

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


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

示例1: Solve

  /**
   *
   * This model was inspired by David Curran's
   * blog post "The Fairest Way to Pick a Team "
   * http://liveatthewitchtrials.blogspot.se/2012/06/fairest-way-to-pick-team.html
   * """
   * What is the best way to pick a team? As kids we would always strictly alternate 
   * between teams so team 1 had first team 2 the second pick and then team 1 again etc.
   * 
   * Most things you can measure about people are on a bell curve. A small number of 
   * people are bad, most are in the middle and a few are good. There are a few good 
   * known metrics of ability. None are perfect, there is no one number that can sum up 
   * ability. The simpler the sport the more one metric can tell you, in cycling VO2 max is 
   * a very good indicator. Whereas in soccer VO2 max, kicking speed, vertical leap, number 
   * of keep me ups you can do etc could all measure some part of football ability.
   * 
   * So say there was one good metric for a task and teams were picked based on this. 
   * Is the standard strict alteration, where Team 1 picks then Team 2 alternating, fair? 
   * Fair here meaning both teams end up with a similar quality. 
   * """
   *
   * Model by Hakan Kjellerstrand ([email protected])
   * See other or-tools/C# models at http://www.hakank.org/or-tools/#csharp
   *
   */
  private static void Solve(int n = 10, int max = 10, int sols_to_show = 0)
  {

    Solver solver = new Solver("PickingTeams");

    Console.WriteLine("n: " + n);
    Console.WriteLine("max: " + max);
    Console.WriteLine("sols_to_show: " + sols_to_show);

    if (n % 2 != 0) {
      Console.WriteLine("The number of people (n) must be divisible by 2.");
      System.Environment.Exit(1);
    }

    //
    // Data
    //

    int num_teams = 2;

    // Randomize data:
    int seed = (int)DateTime.Now.Ticks;
    Random generator = new Random(seed);
    int[] s = new int[n];
    for(int i = 0; i < n; i++) {
      s[i] = 1 + generator.Next(max);
      if (n <= 100) {
        Console.Write(s[i] + " ");
      }
    }
    Console.WriteLine();
    Console.WriteLine("\n" + n + " numbers generated\n");

    // int[] s = {35, 52, 17, 26, 90, 55, 57, 54, 41, 9, 75, 24, 17, 23, 62, 74, 100, 67, 40, 48, 7, 6, 44, 19, 16, 14, 2, 66, 70, 2, 43, 45, 76, 53, 90, 12, 88, 96, 30, 30, 36, 93, 74, 1, 52, 45, 38, 7, 24, 96, 17, 21, 12, 12, 23, 90, 77, 64, 37, 79, 67, 62, 24, 11, 74, 82, 51, 17, 72, 18, 37, 94, 43, 44, 32, 86, 94, 33, 97, 27, 38, 38, 29, 92, 35, 82, 22, 66, 80, 8, 62, 72, 25, 13, 94, 42, 51, 31, 69, 66};
    // n = s.Length;

    int the_sum = s.Sum();
    int half_sum = (int)the_sum / 2;
    Console.WriteLine("sum: " + the_sum + " half_sum: " + half_sum);


    IEnumerable<int> NRange = Enumerable.Range(0, n);
    IEnumerable<int> Sets = Enumerable.Range(0, num_teams);

    //
    // Decision variables
    //

    // To which team (s) do x[i] belong?
    IntVar[] x = solver.MakeIntVarArray(n, 0, 1, "x");

    IntVar diff = (
             (from k in NRange select (s[k]*(x[k] == 0)) ).ToArray().Sum() -
             (from k in NRange select (s[k]*(x[k] == 1)) ).ToArray().Sum()
             ).Abs().Var();
      
    Console.WriteLine("diff.Max(): " + diff.Max());


    //
    // Constraints
    //

    // Ensure that there are the same number of people in each team
    int n2 = (int)n / 2;
    solver.Add((from k in NRange select (x[k]==0)).ToArray().Sum() == n2 );
    // solver.Add((from k in NRange select (x[k]==1)).ToArray().Sum() == n2 );

    // symmetry breaking: assign first number to team 0
    solver.Add(x[0] == 0);

    // Odd sum must yield odd diff, even sum yield even diff
    IntVar even_odd = solver.MakeIntConst(the_sum % 2);
    solver.Add(solver.MakeModuloConstraint(diff, 2, even_odd));

//.........这里部分代码省略.........
开发者ID:HanumathRao,项目名称:hakank,代码行数:101,代码来源:picking_teams.cs

示例2: Solve


//.........这里部分代码省略.........
    //
    // New: Number of members in each team
    //
    // Note that we restrict the possible values to +/- team_diff.
    int n2 = (int)n / num_teams;
    Console.WriteLine("n2: " + n2);
    IntVar[] team_num = solver.MakeIntVarArray(num_teams, n2-team_diff, n2+team_diff, "team_num");
    for(int team = 0; team < num_teams; team++) {
      team_num[team] = (from k in NRange select (x[k] == team) ).ToArray().Sum().Var();
      
    }

    // New: We send all the IntVar arrays to the solver
    IntVar[] all = x.Concat(team_sum).Concat(team_num).ToArray();


    //
    // Constraints
    //

    // New: Ensure that there are the (about) same number of people in each team.
    for(int t = 0; t < num_teams; t++) {
      solver.Add((((from k in NRange select (x[k]==t)).ToArray().Sum()-n2)).Abs() <= team_diff);
    }

    // The teams_sums should add up to the total sum
    solver.Add(team_sum.Sum()  == the_sum);

    // symmetry breaking: assign first member to team 0
    solver.Add(x[0] == 0);

    // Odd sum must yield odd diff, even sum yield even diff
    IntVar even_odd = solver.MakeIntConst(the_sum % 2);
    solver.Add(solver.MakeModuloConstraint(diff, 2, even_odd));


    //
    // Search
    //

    DecisionBuilder db = solver.MakePhase(all,
                                          // Solver.INT_VAR_DEFAULT,
                                          // Solver.INT_VAR_SIMPLE,
                                          // Solver.CHOOSE_FIRST_UNBOUND,
                                          // Solver.CHOOSE_RANDOM,
                                          Solver.CHOOSE_MIN_SIZE_LOWEST_MIN,
                                          // Solver.CHOOSE_MIN_SIZE_HIGHEST_MIN,
                                          // Solver.CHOOSE_MIN_SIZE_LOWEST_MAX,
                                          // Solver.CHOOSE_MIN_SIZE_HIGHEST_MAX,
                                          // Solver.CHOOSE_PATH,


                                          // Solver.INT_VALUE_DEFAULT
                                          // Solver.INT_VALUE_SIMPLE
                                          // Solver.ASSIGN_MIN_VALUE
                                          // Solver.ASSIGN_MAX_VALUE
                                          Solver.ASSIGN_RANDOM_VALUE
                                          // Solver.ASSIGN_CENTER_VALUE
                                          );


    /*
    DefaultPhaseParameters parameters = new DefaultPhaseParameters();
    
    parameters.heuristic_period = 20;
    // parameters.heuristic_period = 10;
开发者ID:HanumathRao,项目名称:hakank,代码行数:67,代码来源:picking_teams2.cs


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