How to use the ortools.constraint_solver.pywrapcp.Solver function in ortools

To help you get started, we’ve selected a few ortools examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github google / or-tools / examples / python / alphametic.py View on Github external
def main(problem_str="SEND+MORE=MONEY", base=10):

  # Create the solver.
  solver = pywrapcp.Solver("Send most money")

  # data
  print("\nproblem:", problem_str)

  # convert to array.
  problem = re.split("[\s+=]", problem_str)

  p_len = len(problem)
  print("base:", base)

  # create the lookup table: list of (digit : ix)
  a = sorted(set("".join(problem)))
  n = len(a)
  lookup = dict(list(zip(a, list(range(n)))))

  # length of each number
github google / or-tools / examples / python / grocery.py View on Github external
def main():

  # Create the solver.
  solver = pywrapcp.Solver("Grocery")

  #
  # data
  #
  n = 4
  c = 711

  #
  # declare variables
  #
  item = [solver.IntVar(0, c, "item[%i]" % i) for i in range(n)]

  #
  # constraints
  #
  solver.Add(solver.Sum(item) == c)
github google / or-tools / examples / python / mr_smith.py View on Github external
def main():

  # Create the solver.
  solver = pywrapcp.Solver('Mr Smith problem')

  #
  # data
  #
  n = 5

  #
  # declare variables
  #
  x = [solver.IntVar(0, 1, 'x[%i]' % i) for i in range(n)]
  Mr_Smith, Mrs_Smith, Matt, John, Tim = x

  #
  # constraints
  #
github google / or-tools / examples / python / bus_schedule.py View on Github external
def main(num_buses_check=0):

  # Create the solver.
  solver = pywrapcp.Solver("Bus scheduling")

  # data
  time_slots = 6
  demands = [8, 10, 7, 12, 4, 4]
  max_num = sum(demands)

  # declare variables
  x = [solver.IntVar(0, max_num, "x%i" % i) for i in range(time_slots)]
  num_buses = solver.IntVar(0, max_num, "num_buses")

  #
  # constraints
  #
  solver.Add(num_buses == solver.Sum(x))

  # Meet the demands for this and the next time slot
github google / or-tools / examples / python / send_more_money_any_base.py View on Github external
def main(base=10):

  # Create the solver.
  solver = pywrapcp.Solver('Send most money')

  # data
  print('base:', base)

  # declare variables
  s = solver.IntVar(0, base - 1, 's')
  e = solver.IntVar(0, base - 1, 'e')
  n = solver.IntVar(0, base - 1, 'n')
  d = solver.IntVar(0, base - 1, 'd')
  m = solver.IntVar(0, base - 1, 'm')
  o = solver.IntVar(0, base - 1, 'o')
  r = solver.IntVar(0, base - 1, 'r')
  y = solver.IntVar(0, base - 1, 'y')

  x = [s, e, n, d, m, o, r, y]
github google / or-tools / examples / python / crypto.py View on Github external
def main():

  # Create the solver.
  solver = pywrapcp.Solver("Crypto problem")

  #
  # data
  #
  num_letters = 26

  BALLET = 45
  CELLO = 43
  CONCERT = 74
  FLUTE = 30
  FUGUE = 50
  GLEE = 66
  JAZZ = 58
  LYRE = 47
  OBOE = 53
  OPERA = 65
github google / or-tools / examples / python / set_covering.py View on Github external
def main(unused_argv):

  # Create the solver.
  solver = pywrapcp.Solver("Set covering")

  #
  # data
  #
  min_distance = 15
  num_cities = 6

  distance = [[0, 10, 20, 30, 30, 20], [10, 0, 25, 35, 20, 10],
              [20, 25, 0, 15, 30, 20], [30, 35, 15, 0, 15, 25],
              [30, 20, 30, 15, 0, 14], [20, 10, 20, 25, 14, 0]]

  #
  # declare variables
  #
  x = [solver.IntVar(0, 1, "x[%i]" % i) for i in range(num_cities)]
github google / or-tools / examples / python / minesweeper.py View on Github external
def main(game="", r="", c=""):

  # Create the solver.
  solver = pywrapcp.Solver("Minesweeper")

  #
  # data
  #

  # Set default problem
  if game == "":
    game = default_game
    r = default_r
    c = default_c
  else:
    print("rows:", r, " cols:", c)

  #
  # Default problem from "Some Minesweeper Configurations",page 3
  # (same as problem instance minesweeper_config3.txt)
github google / or-tools / examples / python / quasigroup_completion.py View on Github external
def main(puzzle="", n=0):

  # Create the solver.
  solver = pywrapcp.Solver("Quasigroup completion")

  #
  # data
  #

  if puzzle == "":
    puzzle = default_puzzle
    n = default_n

  print("Problem:")
  print_game(puzzle, n, n)

  # declare variables
  x = {}
  for i in range(n):
    for j in range(n):
github google / or-tools / examples / python / hidato_table.py View on Github external
def Solve(model):
    """Solve the given model."""
    # Create the solver.
    solver = pywrapcp.Solver('hidato-table')

    #
    # models, a 0 indicates an open cell which number is not yet known.
    #
    #
    puzzle = None
    if model == 1:
        # Simple problem
        puzzle = [[6, 0, 9], [0, 2, 8], [1, 0, 0]]

    elif model == 2:
        puzzle = [[0, 44, 41, 0, 0, 0, 0], [0, 43, 0, 28, 29, 0, 0],
                  [0, 1, 0, 0, 0, 33, 0], [0, 2, 25, 4, 34, 0, 36],
                  [49, 16, 0, 23, 0, 0, 0], [0, 19, 0, 0, 12, 7, 0],
                  [0, 0, 0, 14, 0, 0, 0]]