zebra.cs 5.88 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
//
// Copyright 2012 Hakan Kjellerstrand
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Linq;
using Google.OrTools.ConstraintSolver;

public class NQueens
{
  /**
   *
   * Solves the Zebra problem.
   *
   * This is a port of the or-tools/Python model zebra.py
   *
   *
   * """
   * This is the zebra problem as invented by Lewis Caroll.
   *
   * There are five houses.
   * The Englishman lives in the red house.
   * The Spaniard owns the dog.
   * Coffee is drunk in the green house.
   * The Ukrainian drinks tea.
   * The green house is immediately to the right of the ivory house.
   * The Old Gold smoker owns snails.
   * Kools are smoked in the yellow house.
   * Milk is drunk in the middle house.
   * The Norwegian lives in the first house.
   * The man who smokes Chesterfields lives in the house next to the man
   *   with the fox.
   * Kools are smoked in the house next to the house where the horse is kept.
   * The Lucky Strike smoker drinks orange juice.
   * The Japanese smokes Parliaments.
   * The Norwegian lives next to the blue house.
   *
   * Who owns a zebra and who drinks water?
   * """
   *
   */
  private static void Solve()
  {
    Solver solver = new Solver("Zebra");

    int n = 5;

    //
    // Decision variables
    //

    // Colors
    IntVar red           = solver.MakeIntVar(1, n, "red");
    IntVar green         = solver.MakeIntVar(1, n, "green");
    IntVar yellow        = solver.MakeIntVar(1, n, "yellow");
    IntVar blue          = solver.MakeIntVar(1, n, "blue");
    IntVar ivory         = solver.MakeIntVar(1, n, "ivory");

    // Nationality
    IntVar englishman    = solver.MakeIntVar(1, n, "englishman");
    IntVar spaniard      = solver.MakeIntVar(1, n, "spaniard");
    IntVar japanese      = solver.MakeIntVar(1, n, "japanese");
    IntVar ukrainian     = solver.MakeIntVar(1, n, "ukrainian");
    IntVar norwegian     = solver.MakeIntVar(1, n, "norwegian");

    // Animal
    IntVar dog           = solver.MakeIntVar(1, n, "dog");
    IntVar snails        = solver.MakeIntVar(1, n, "snails");
    IntVar fox           = solver.MakeIntVar(1, n, "fox");
    IntVar zebra         = solver.MakeIntVar(1, n, "zebra");
    IntVar horse         = solver.MakeIntVar(1, n, "horse");

    // Drink
    IntVar tea           = solver.MakeIntVar(1, n, "tea");
    IntVar coffee        = solver.MakeIntVar(1, n, "coffee");
    IntVar water         = solver.MakeIntVar(1, n, "water");
    IntVar milk          = solver.MakeIntVar(1, n, "milk");
    IntVar fruit_juice   = solver.MakeIntVar(1, n, "fruit juice");

    // Smoke
    IntVar old_gold      = solver.MakeIntVar(1, n, "old gold");
    IntVar kools         = solver.MakeIntVar(1, n, "kools");
    IntVar chesterfields = solver.MakeIntVar(1, n, "chesterfields");
    IntVar lucky_strike  = solver.MakeIntVar(1, n, "lucky strike");
    IntVar parliaments   = solver.MakeIntVar(1, n, "parliaments");


    // for search
    IntVar[] all_vars =
      {parliaments, kools, chesterfields, lucky_strike, old_gold,
       englishman, spaniard, japanese, ukrainian, norwegian,
       dog, snails, fox, zebra, horse,
       tea, coffee, water, milk, fruit_juice,
       red, green, yellow, blue, ivory};

    //
    // Constraints
    //

    // Alldifferents
    solver.Add(new IntVar[]
        {red, green, yellow, blue, ivory}.AllDifferent());
    solver.Add(new IntVar[]
        {englishman, spaniard, japanese, ukrainian, norwegian}.AllDifferent());
    solver.Add(new IntVar[]
        {dog, snails, fox, zebra, horse}.AllDifferent());
    solver.Add(new IntVar[]
        {tea, coffee, water, milk, fruit_juice}.AllDifferent());
    solver.Add(new IntVar[]
        {parliaments, kools, chesterfields, lucky_strike, old_gold}.AllDifferent());

    //
    // The clues
    //
    solver.Add(englishman == red);
    solver.Add(spaniard == dog);
    solver.Add(coffee == green);
    solver.Add(ukrainian == tea);
    solver.Add(green == ivory + 1);
    solver.Add(old_gold == snails);
    solver.Add(kools == yellow);
    solver.Add(milk == 3);
    solver.Add(norwegian == 1);
    solver.Add((fox - chesterfields).Abs() == 1);
    solver.Add((horse - kools).Abs() == 1);
    solver.Add(lucky_strike == fruit_juice);
    solver.Add(japanese == parliaments);
    solver.Add((norwegian - blue).Abs() == 1);


    //
    // Search
    //
    DecisionBuilder db = solver.MakePhase(all_vars,
                                          Solver.INT_VAR_DEFAULT,
                                          Solver.INT_VALUE_DEFAULT);

    solver.NewSearch(db);

    IntVar[] p = {englishman, spaniard, japanese, ukrainian, norwegian};
    int[] ix = {0,1,2,3,4};
    while (solver.NextSolution()) {
      int water_drinker = (from i in ix
                           where p[i].Value() == water.Value()
                           select i).First();
      int zebra_owner = (from i in ix
                         where p[i].Value() == zebra.Value()
                         select i).First();
      Console.WriteLine("The {0} drinks water.", p[water_drinker].ToString());
      Console.WriteLine("The {0} owns the zebra", p[zebra_owner].ToString());

    }

    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();

  }

  public static void Main(String[] args)
  {
    Solve();
  }
}