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


C# IntVar.AllowedAssignments方法代码示例

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


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

示例1: Solve


//.........这里部分代码省略.........
                        {22, 0, 15, 0, 0},
                        {23, 0, 1, 14, 11},
                        {0, 25, 0, 0, 12}};
      puzzle = puzzle3;

    } else if (model == 4) {
      // problem 2 (Practice)
      int[,] puzzle4 = {{0, 0, 0, 0, 14},
                        {0, 18, 12, 0, 0},
                        {0, 0, 17, 4, 5},
                        {0, 0, 7, 0, 0},
                        {9, 8, 25, 1, 0}};
      puzzle = puzzle4;

    } else if (model == 5) {
      // problem 3 (Beginner)
      int[,] puzzle5 = {{0, 26, 0, 0, 0, 18},
                        {0, 0, 27, 0, 0, 19},
                        {31, 23, 0, 0, 14, 0},
                        {0, 33, 8, 0, 15, 1},
                        {0, 0, 0, 5, 0, 0},
                        {35, 36, 0, 10, 0, 0}};
      puzzle = puzzle5;

    } else if (model == 6) {
      // Problem 15 (Intermediate)
      int[,] puzzle6 = {{64, 0, 0, 0, 0, 0, 0, 0},
                        {1, 63, 0, 59, 15, 57, 53, 0},
                        {0, 4, 0, 14, 0, 0, 0, 0},
                        {3, 0, 11, 0, 20, 19, 0, 50},
                        {0, 0, 0, 0, 22, 0, 48, 40},
                        {9, 0, 0, 32, 23, 0, 0, 41},
                        {27, 0, 0, 0, 36, 0, 46, 0},
                        {28, 30, 0, 35, 0, 0, 0, 0}};
      puzzle = puzzle6;
    }

    int r = puzzle.GetLength(0);
    int c = puzzle.GetLength(1);

    Console.WriteLine();
    Console.WriteLine("----- Solving problem {0} -----", model);
    Console.WriteLine();

    PrintMatrix(puzzle);

    //
    // Decision variables
    //
    IntVar[] positions = solver.MakeIntVarArray(r*c, 0, r * c - 1, "p");


    //
    // Constraints
    //
    solver.Add(positions.AllDifferent());

    //
    // Fill in the clues
    //
    for(int i = 0; i < r; i++) {
      for(int j = 0; j < c; j++) {
        if (puzzle[i,j] > 0) {
          solver.Add(positions[puzzle[i,j] - 1] == i * c + j);
        }
      }
    }

    // Consecutive numbers much touch each other in the grid.
    // We use an allowed assignment constraint to model it.
    IntTupleSet close_tuples = BuildPairs(r, c);
    for(int k = 1; k < r * c - 1; k++) {
      IntVar[] tmp = new IntVar[] {positions[k], positions[k + 1]};
      solver.Add(tmp.AllowedAssignments(close_tuples));
    }


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

    solver.NewSearch(db);

    int num_solution = 0;
    while (solver.NextSolution()) {
      num_solution++;
      PrintOneSolution(positions, r, c, num_solution);
    }

    Console.WriteLine("\nSolutions: " + solver.Solutions());
    Console.WriteLine("WallTime: " + solver.WallTime() + "ms ");
    Console.WriteLine("Failures: " + solver.Failures());
    Console.WriteLine("Branches: " + solver.Branches());

    solver.EndSearch();

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

示例2: Solve

    /**
       *
       * Traffic lights problem.
       *
       * CSPLib problem 16
       * http://www.csplib.org/Problems/prob016
       * """
       * Specification:
       * Consider a four way traffic junction with eight traffic lights. Four of the traffic
       * lights are for the vehicles and can be represented by the variables V1 to V4 with domains
       * {r,ry,g,y} (for red, red-yellow, green and yellow). The other four traffic lights are
       * for the pedestrians and can be represented by the variables P1 to P4 with domains {r,g}.
       *
       * The constraints on these variables can be modelled by quaternary constraints on
       * (Vi, Pi, Vj, Pj ) for 1<=i<=4, j=(1+i)mod 4 which allow just the tuples
       * {(r,r,g,g), (ry,r,y,r), (g,g,r,r), (y,r,ry,r)}.
       *
       * It would be interesting to consider other types of junction (e.g. five roads
       * intersecting) as well as modelling the evolution over time of the traffic light sequence.
       * ...
       *
       * Results
       * Only 2^2 out of the 2^12 possible assignments are solutions.
       *
       * (V1,P1,V2,P2,V3,P3,V4,P4) =
       * {(r,r,g,g,r,r,g,g), (ry,r,y,r,ry,r,y,r), (g,g,r,r,g,g,r,r), (y,r,ry,r,y,r,ry,r)}
       * [(1,1,3,3,1,1,3,3), ( 2,1,4,1, 2,1,4,1), (3,3,1,1,3,3,1,1), (4,1, 2,1,4,1, 2,1)}
       * The problem has relative few constraints, but each is very
       * tight. Local propagation appears to be rather ineffective on this
       * problem.
       *
       * """
       * Note: In this model we use only the constraint
       *  solver.AllowedAssignments().
       *
       *
       * See http://www.hakank.org/or-tools/traffic_lights.py
       *
       */
    private static void Solve()
    {
        Solver solver = new Solver("TrafficLights");

        //
        // data
        //
        int n = 4;

        int r = 0;
        int ry = 1;
        int g = 2;
        int y = 3;

        string[] lights = {"r", "ry", "g", "y"};

        // The allowed combinations
        IntTupleSet allowed = new IntTupleSet(4);
        allowed.InsertAll(new int[,] {{r,r,g,g},
                                  {ry,r,y,r},
                                  {g,g,r,r},
                                  {y,r,ry,r}});
        //
        // Decision variables
        //
        IntVar[] V = solver.MakeIntVarArray(n, 0, n-1, "V");
        IntVar[] P = solver.MakeIntVarArray(n, 0, n-1, "P");

        // for search
        IntVar[] VP = new IntVar[2 * n];
        for(int i = 0; i < n; i++) {
          VP[i] = V[i];
          VP[i+n] = P[i];
        }

        //
        // Constraints
        //
        for(int i = 0; i < n; i++) {
          int j = (1+i) % n;
          IntVar[] tmp = new IntVar[] {V[i],P[i],V[j],P[j]};
          solver.Add(tmp.AllowedAssignments(allowed));
        }

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

        solver.NewSearch(db);

        while (solver.NextSolution()) {
          for(int i = 0; i < n; i++) {
        Console.Write("{0,2} {1,2} ",
                      lights[V[i].Value()],
                      lights[P[i].Value()]);
          }
          Console.WriteLine();
        }
//.........这里部分代码省略.........
开发者ID:JustinKennethPearson,项目名称:csplib,代码行数:101,代码来源:traffic_lights.cs


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