Как расширить и распечатать неглобальные ограничения в DOcplex? - PullRequest
0 голосов
/ 29 ноября 2018

Я недавно мигрировал из AMPL / CPLEX-CP в Python / DOcplex.CP-MP.В AMPL можно отобразить расширенную версию ограничений с помощью команды «раскрыть _constraint_name», что полезно при отладке модели.

Есть ли такая функциональность в DOcplex?Например:

В DOcplex можно как-то сделать это для глобальных ограничений, таких как all_diff, следующим образом:

import docplex.cp.model as cp

NB_QUEEN = 8
mdl = cp.CpoModel()
x = cp.integer_var_list(NB_QUEEN, 0, NB_QUEEN - 1, "X")

# add the constraints to the model
mdl.add(mdl.all_diff(x[i] + i for i in range(NB_QUEEN)))

# print the constraints
test_constraint = mdl.all_diff(x[i] - i for i in range(NB_QUEEN))
print(test_constraint)

, которая будет печатать:

alldiff([X_0 - 0, X_1 - 1, X_2 - 2, X_3 - 3, X_4 - 4, X_5 - 5, X_6 - 6, X_7 - 7])

IsЕсть ли способ распечатать расширенную версию следующих ограничений?

 mdl.add(mdl.sum( X[i]+j for j in R1) ==1 for i in R2)

Простой вызов print () для указанных выше ограничений не подходит.

Спасибо.

РЕДАКТИРОВАТЬ

Благодаря Алексу Флейшеру я сделал небольшой рабочий пример, который показывает, как печатать ограничение из набора ограничений.

x = mdl.integer_var_list(5, name='X')
ct = ( [1 == sum( x[i]+j for i in range(0, 5) ) for j in range(0,5) ])
print(ct[2])

, которыйпечатает

0 + (X_0 + 2) + (X_1 + 2) + (X_2 + 2) + (X_3 + 2) + (X_4 + 2) == 1

Если вы хотите напечатать все ограничения в наборе, они могут сделать

for j in range(0, 5)
    print(c[j])

1 Ответ

0 голосов
/ 30 ноября 2018

да, вы можете отображать такие ограничения, но в вашем случае у вас есть набор ограничений.Позвольте мне привести небольшой пример из примера сталелитейного завода в CPLEX_Studio128 \ python \ examples \ cp \ basic

from docplex.cp.model import CpoModel
from collections import namedtuple


#-----------------------------------------------------------------------------
# Initialize the problem data
#-----------------------------------------------------------------------------

# List of coils to produce (orders)
Order = namedtuple("Order", ['id', 'weight', 'color'])
ORDERS = (
            Order( 1, 22, 5),
            Order( 2,  9, 3),
            Order( 3,  9, 4),
            Order( 4,  8, 5),
            Order( 5,  8, 7),
            Order( 6,  6, 3),
            Order( 7,  5, 6),
            Order( 8,  3, 0),
            Order( 9,  3, 2),
            Order(10,  3, 3),
            Order(11,  2, 1),
            Order(12,  2, 5)
         )

# Max number of different colors of coils produced by a single slab
MAX_COLOR_PER_SLAB = 2

# List of available slab weights.
AVAILABLE_SLAB_WEIGHTS = [11, 13, 16, 17, 19, 20, 23, 24, 25,
                          26, 27, 28, 29, 30, 33, 34, 40, 43, 45]


#-----------------------------------------------------------------------------
# Prepare the data for modeling
#-----------------------------------------------------------------------------

# Upper bound for the number of slabs to use
MAX_SLABS = len(ORDERS)

# Build a set of all colors
allcolors = set(o.color for o in ORDERS)

# The heaviest slab
max_slab_weight = max(AVAILABLE_SLAB_WEIGHTS)

# Minimum loss incurred for a given slab usage.
# loss[v] = loss when smallest slab is used to produce a total weight of v
loss = [0] + [min([sw - use for sw in AVAILABLE_SLAB_WEIGHTS if sw >= use]) for use in range(1, max_slab_weight + 1)]


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

# Create model 
mdl = CpoModel()

total_loss=mdl.integer_var(0,10000000,"total loss")


# Index of the slab used to produce each coil order
production_slab = mdl.integer_var_list(len(ORDERS), 0, MAX_SLABS - 1, "production_slab")

# Usage of each slab
slab_use = mdl.integer_var_list(MAX_SLABS, 0, max_slab_weight, "slab_use")

# The orders are allocated to the slabs with capacity
mdl.add(mdl.pack(slab_use, production_slab, [o.weight for o in ORDERS]))

# Constrain max number of colors produced by each slab
for s in range(MAX_SLABS):
   su = 0
   for c in allcolors:
       lo = False
       for i, o in enumerate(ORDERS):
           if o.color == c:
               lo |= (production_slab[i] == s)
       su += lo
   mdl.add(su <= MAX_COLOR_PER_SLAB)

# Minimize the total loss
ct=(total_loss == sum([mdl.element(slab_use[s], loss) for s in range(MAX_SLABS)]))

mdl.add(ct)
print("ct=",ct)

mdl.add(mdl.minimize(total_loss))

# Set search strategy
mdl.set_search_phases([mdl.search_phase(production_slab)])


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

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

# Print solution
if msol:
    print("Solution: ")
    for s in set(msol[ps] for ps in production_slab):
        # Determine orders using this slab
        lordrs = [o for i, o in enumerate(ORDERS) if msol[production_slab[i]] == s]
        # Compute display attributes
        used_weight = msol[slab_use[s]]          # Weight used in the slab
        loss_weight = loss[used_weight]          # Loss weight
        colors = set(o.color for o in lordrs)    # List of colors
        loids = [o.id for o in lordrs]           # List of order irs
        print("Slab weight={}, used={}, loss={}, colors={}, orders={}"
              .format(used_weight + loss_weight, used_weight, loss_weight, colors, loids))
else:
    print("No solution found")

, который дает

ct= "total loss" == 0 + element(slab_use0, [0, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 0, 2, 1, 0, 0, 1, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 0, 0, 5, 4, 3, 2, 1, 0, 2, 1, 0, 1, 0]) + element(slab_use1, [0, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 0, 2, 1, 0, 0, 1, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 0, 0, 5, 4, 3, 2, 1, 0, 2, 1, 0, 1, 0]) + element(slab_use2, [0, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 0, 2, 1, 0, 0, 1, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 0, 0, 5, 4, 3, 2, 1, 0, 2, 1, 0, 1, 0]) + element(slab_use3, [0, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 0, 2, 1, 0, 0, 1, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 0, 0, 5, 4, 3, 2, 1, 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4, 3, 2, 1, 0, 2, 1, 0, 1, 0]) + element(slab_use11, [0, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 0, 2, 1, 0, 0, 1, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 0, 0, 5, 4, 3, 2, 1, 0, 2, 1, 0, 1, 0])

Строки, которые отображают

# Minimize the total loss
ct=(total_loss == sum([mdl.element(slab_use[s], loss) for s in range(MAX_SLABS)]))

mdl.add(ct)
print("ct=",ct)

С уважением

...