コード例 #1
0
# Estimate an upper bound to the ruler length
MAX_LENGTH = (ORDER - 1)**2

#-----------------------------------------------------------------------------
# Build the model
#-----------------------------------------------------------------------------

# Create model
mdl = CpoModel()

# Create array of variables corresponding to position rule marks
marks = mdl.integer_var_list(ORDER, 0, MAX_LENGTH, "M")

# Create marks distances that should be all different
dist = [marks[i] - marks[j] for i in range(1, ORDER) for j in range(0, i)]
mdl.add(mdl.all_diff(dist))

# Avoid symmetric solutions by ordering marks
mdl.add(marks[0] == 0)
for i in range(1, ORDER):
    mdl.add(marks[i] > marks[i - 1])

# Avoid mirror solution
mdl.add((marks[1] - marks[0]) < (marks[ORDER - 1] - marks[ORDER - 2]))

# Minimize ruler size (position of the last mark)
mdl.add(mdl.minimize(marks[ORDER - 1]))

#-----------------------------------------------------------------------------
# Solve the model and display the result
#-----------------------------------------------------------------------------
コード例 #2
0
from docplex.cp.model import CpoModel

mdl = CpoModel(name='buses')
nbbus40 = mdl.integer_var(0, 1000, name='nbBus40')
nbbus30 = mdl.integer_var(0, 1000, name='nbBus30')
nbbus50 = mdl.integer_var(0, 1000, name='nbBus50')
mdl.add(nbbus40 * 40 + nbbus30 * 30 + 50 * nbbus50 >= 120)

mdl.add(mdl.all_diff(nbbus50, nbbus40, nbbus30))

mdl.minimize(nbbus50 * 500 + nbbus40 * 400 + nbbus30 * 300)

msol = mdl.solve()

print(msol[nbbus40], " buses 40 seats")
print(msol[nbbus30], " buses 30 seats")
print(msol[nbbus50], " buses 50 seats")
"""

which gives

0  buses 40 seats
1  buses 30 seats
2  buses 50 seats

"""
コード例 #3
0
from sys import stdout

# Set model parameters
NB_QUEEN = 8  # Number of queens

# Create a CPO model
model = CpoModel()

# Create column index of each queen
# Arguments: size, domain_min, domain_max, name
# That is, create NB_QUEEN integer variables that take on a value between 0 and NB_QUEEN
# The value indicates the row of that ith column with a queen
x = model.integer_var_list(NB_QUEEN, 0, NB_QUEEN - 1, "X")

# One queen per row
model.add(model.all_diff(x))

# Queens cannot be the same number of columns apart as they are rows apart
for i in range(NB_QUEEN):
    for j in range(NB_QUEEN):
        if i != j:
            model.add(model.abs(i - j) != model.abs(x[i] - x[j]))

# Solve model
print("Solving model....")
msol = model.solve(TimeLimit=10)

# Print solution
if msol:  # If the model ran successfully, it returns True
    stdout.write("Solution:")
    for v in x:
コード例 #4
0

#-----------------------------------------------------------------------------
# Build the model
#-----------------------------------------------------------------------------

# Create CPO model
mdl = CpoModel()

# Create grid of variables
GRNG = range(SQUARE_SIZE)
grid = [[mdl.integer_var(min=0, max=SQUARE_SIZE - 1, name="C_{}_{}".format(l, c)) for l in GRNG] for c in GRNG]

# Add alldiff constraints for lines
for l in GRNG:
    mdl.add(mdl.all_diff([grid[l][c] for c in GRNG]))

# Add alldiff constraints for columns
for c in GRNG:
    mdl.add(mdl.all_diff([grid[l][c] for l in GRNG]))

# Add alldiff constraints for diagonals
mdl.add(mdl.all_diff([grid[l][l] for l in GRNG]))
mdl.add(mdl.all_diff([grid[l][SQUARE_SIZE - l - 1] for l in GRNG]))

# Force first line to natural sequence
for c in GRNG:
    mdl.add(grid[0][c] == c)


#-----------------------------------------------------------------------------
コード例 #5
0
# Create CPO model
mdl = CpoModel()

# Create grid of variables
GRNG = range(CUBE_SIZE)
grid = [[[
    mdl.integer_var(min=0,
                    max=CUBE_SIZE - 1,
                    name="C_{}_{}_{}".format(x, y, z)) for x in GRNG
] for y in GRNG] for z in GRNG]

# Add constraints for each slice on direction x
for x in GRNG:
    # Add alldiff constraints for lines
    for l in GRNG:
        mdl.add(mdl.all_diff([grid[x][l][c] for c in GRNG]))

    # Add alldiff constraints for columns
    for c in GRNG:
        mdl.add(mdl.all_diff([grid[x][l][c] for l in GRNG]))

    # Add alldiff constraints for diagonals
    if CONSTRAIN_DIAGONALS:
        mdl.add(mdl.all_diff([grid[x][l][l] for l in GRNG]))
        mdl.add(mdl.all_diff([grid[x][l][CUBE_SIZE - l - 1] for l in GRNG]))

# Add constraints for each slice on direction y
for y in GRNG:
    # Add alldiff constraints for lines
    for l in GRNG:
        mdl.add(mdl.all_diff([grid[l][y][c] for c in GRNG]))
コード例 #6
0
ファイル: sudoku.py プロジェクト: satishkrr/vldb2018-sherlock
#-----------------------------------------------------------------------------

# Create CPO model
mdl = CpoModel()

# Grid range
GRNG = range(9)

# Create grid of variables
grid = [[
    mdl.integer_var(min=1, max=9, name="C" + str(l) + str(c)) for l in GRNG
] for c in GRNG]

# Add alldiff constraints for lines
for l in GRNG:
    mdl.add(mdl.all_diff([grid[l][c] for c in GRNG]))

# Add alldiff constraints for columns
for c in GRNG:
    mdl.add(mdl.all_diff([grid[l][c] for l in GRNG]))

# Add alldiff constraints for sub-squares
ssrng = range(0, 9, 3)
for sl in ssrng:
    for sc in ssrng:
        mdl.add(
            mdl.all_diff([
                grid[l][c] for l in range(sl, sl + 3)
                for c in range(sc, sc + 3)
            ]))
コード例 #7
0
ファイル: n_queen.py プロジェクト: arani-mohammad/docplex
# Set model parameters
NB_QUEEN = 8

#-----------------------------------------------------------------------------
# Build the model
#-----------------------------------------------------------------------------

# Create model
mdl = CpoModel()

# Create column index of each queen
x = mdl.integer_var_list(NB_QUEEN, 0, NB_QUEEN - 1, "X")

# One queen per raw
mdl.add(mdl.all_diff(x))

# One queen per diagonal xi - xj != i - j
mdl.add(mdl.all_diff(x[i] + i for i in range(NB_QUEEN)))

# One queen per diagonal xi - xj != j - i
mdl.add(mdl.all_diff(x[i] - i for i in range(NB_QUEEN)))

#-----------------------------------------------------------------------------
# Solve the model and display the result
#-----------------------------------------------------------------------------

# Solve model
print("Solving model....")
msol = mdl.solve(TimeLimit=10)
コード例 #8
0
Please refer to documentation for appropriate setup of solving configuration.
"""

from docplex.cp.model import CpoModel

#-----------------------------------------------------------------------------
# Build the model
#-----------------------------------------------------------------------------

# Create model
mdl = CpoModel()

# Create model variables
V = mdl.integer_var_list(size=9, min=1, max=9, name="V")
mdl.add(mdl.all_diff(V))
        
# Create constraint
mdl.add(V[0] + 13 * V[1] / V[2] + V[3] + 12 * V[4] - V[5] - 11 + V[6] * V[7] / V[8] - 10 == 66)


#-----------------------------------------------------------------------------
# Solve the model and display the result
#-----------------------------------------------------------------------------

# Solve model
print("Solving model....")
msol = mdl.solve(TimeLimit=10)

print("Solution: ")
msol.print_solution()