Python 我的CP-SAT程序(谷歌或工具)不';t在on_solution_回调函数中执行命令
你能帮帮我吗?我不熟悉python、Google或工具,不知道如何调试它。似乎在Python 我的CP-SAT程序(谷歌或工具)不';t在on_solution_回调函数中执行命令,python,or-tools,Python,Or Tools,你能帮帮我吗?我不熟悉python、Google或工具,不知道如何调试它。似乎在def on\u solution\u回调(self):行之前,代码就开始工作了。没有错误,但也没有生成解决方案。非常感谢。下面是我的全部代码 from __future__ import absolute_import from __future__ import division from __future__ import print_function from ortools.sat.python impo
def on\u solution\u回调(self):
行之前,代码就开始工作了。没有错误,但也没有生成解决方案。非常感谢。下面是我的全部代码
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from ortools.sat.python import cp_model
class EmployeesPartialSolutionPrinter(cp_model.CpSolverSolutionCallback):
"""Print intermediate solutions."""
# Here, we retrieve parameter values and assign to respective variables
def __init__(self, shifts_cashier, shifts_genad, num_cashiers, num_genads, num_days, num_shifts, sols):
cp_model.CpSolverSolutionCallback.__init__(self)
self._shifts_cashier = shifts_cashier
self._shifts_genad = shifts_genad
self._num_cashiers = num_cashiers
self._num_genads = num_genads
self._num_days = num_days
self._num_shifts = num_shifts
self._solutions = set(sols)
self._solution_count = 0
# Prints solutions in a format
def on_solution_callback(self):
print("printttt")
if self._solution_count in self._solutions:
print('Solution %i' % self._solution_count)
for d in range(self._num_days):
print('Day %i' % d)
for n in range(self._num_cashiers):
is_working = False
for s in range(self._num_shifts):
if self.Value(self._shifts_cashier[(n, d, s)]):
is_working = True
print(' Cashier %i works shift %i' % (n, s))
if not is_working:
print(' Cashier {} does not work'.format(n))
for g in range(self._num_genads):
is_working = False
for s in range(self._num_shifts):
if self.Value(self._shifts_genad[(g, d, s)]):
is_working = True
print(' GenAd %i works shift %i' % (g, s))
if not is_working:
print(' GenAd {} does not work'.format(g))
print()
self._solution_count += 1
def solution_count(self):
return self._solution_count
def main():
# Here, we initialize our data variables
num_cashiers = 4
num_genads = 4
num_shifts = 3
num_days = 3
all_cashiers = range(num_cashiers)
all_genads = range(num_genads)
all_shifts = range(num_shifts)
all_days = range(num_days)
# Creates the model
model = cp_model.CpModel()
# Creates shift variables for cashiers
# shifts[(n, d, s)]: cashier 'n' works shift 's' on day 'd'
shifts_cashier = {}
for n in all_cashiers:
for d in all_days:
for s in all_shifts:
shifts_cashier[(n, d, s)] = model.NewBoolVar('shift_n%id%is%i' % (n, d, s))
# Creates shift variables for general admins
# shifts[(g, d, s)]: general admin 'g' works shift 's' on day 'd'
shifts_genad = {}
for g in all_genads:
for d in all_days:
for s in all_shifts:
shifts_genad[(g, d, s)] = model.NewBoolVar('shift_g%id%is%i' % (g, d, s))
# Each shift is assigned to exactly one cashier and one admin in the schedule period
for d in all_days:
for s in all_shifts:
model.Add(sum(shifts_cashier[(n, d, s)] for n in all_cashiers) == 1)
model.Add(sum(shifts_genad[(g, d, s)] for n in all_genads) == 1)
# Each cashier works at most one shift per day
for n in all_cashiers:
for d in all_days:
model.Add(sum(shifts_cashier[(n, d, s)] for s in all_shifts) <= 1)
# Each general admin works at most one shift per day
for g in all_genads:
for d in all_days:
model.Add(sum(shifts_genad[(g, d, s)] for s in all_shifts) <= 1)
# min_shifts_per_cashier is the largest integer such that every cashier
# can be assigned at least that many shifts. If the number of cashiers doesn't
# divide the total number of shifts over the schedule period,
# some cashiers have to work one more shift, for a total of
# min_shifts_per_cashier + 1.
min_shifts_per_cashier = (num_shifts * num_days) // num_cashiers
max_shifts_per_cashier = min_shifts_per_cashier + 1
for n in all_cashiers:
num_shifts_worked_c = sum(shifts_cashier[(n, d, s)] for d in all_days for s in all_shifts)
model.Add(min_shifts_per_cashier <= num_shifts_worked_c)
model.Add(num_shifts_worked_c <= max_shifts_per_cashier)
# min_shifts_per_genad is the largest integer such that every genad
# can be assigned at least that many shifts. If the number of genads doesn't
# divide the total number of shifts over the schedule period,
# some genads have to work one more shift, for a total of
# min_shifts_per_genad + 1.
min_shifts_per_genad = (num_shifts * num_days) // num_genads
max_shifts_per_genad = min_shifts_per_genad + 1
for n in all_genads:
num_shifts_worked_g = sum(shifts_genad[(g, d, s)] for d in all_days for s in all_shifts)
model.Add(min_shifts_per_genad <= num_shifts_worked_g)
model.Add(num_shifts_worked_g <= max_shifts_per_genad)
# Creates the solver and solve
solver = cp_model.CpSolver()
solver.parameters.linearization_level = 0
# Display the first five solutions.
a_few_solutions = range(2)
solution_printer = EmployeesPartialSolutionPrinter(shifts_cashier, shifts_genad, num_cashiers, num_genads, num_days, num_shifts, a_few_solutions)
solver.SearchForAllSolutions(model, solution_printer)
# Statistics.
print()
print('Statistics')
print(' - conflicts : %i' % solver.NumConflicts())
print(' - branches : %i' % solver.NumBranches())
print(' - wall time : %f s' % solver.WallTime())
print(' - solutions found : %i' % solution_printer.solution_count())
if __name__ == '__main__':
main()
来自未来导入绝对导入
来自未来进口部
来自未来导入打印功能
从ortools.sat.python导入cp_模型
类EmployeesPartialSolutionPrinter(cp_model.CpSolverSolutionCallback):
“”“打印中间解决方案。”“”
#这里,我们检索参数值并分配给相应的变量
定义初始日期(自我、班次、出纳、班次、出纳、天数、天数、班次、解决方案):
cp_model.CpSolverSolutionCallback._init__(self)
自助。\轮班\出纳=轮班\出纳
self.\u shifts\u genad=shifts\u genad
self.\u num\u出纳=num\u出纳
self.\u num\u genads=num\u genads
self.\u num\u days=num\u days
self.\u num\u移位=num\u移位
自身解决方案=集合(sols)
自身解决方案计数=0
#以某种格式打印解决方案
def on_solution_回调(自):
打印(“printtt”)
如果self.\u解决方案计入self.\u解决方案:
打印(“解决方案%i”%self.\u解决方案\u计数)
对于范围内的d(自我数天):
打印('天%i'%d)
对于范围内的n(自助出纳人数):
_正在工作=错误
对于范围内的s(自身数量移位):
如果self.Value(self.\u移位\u出纳[(n,d,s)]):
工作正常吗
打印('出纳%i工班%i'(n,s))
如果不起作用:
打印('出纳{}不工作'。格式(n))
对于范围内的g(自数值):
_正在工作=错误
对于范围内的s(自身数量移位):
如果self.Value(self.\u移位\u genad[(g,d,s)]):
工作正常吗
打印('GenAd%i工作班次%i'(g,s))
如果不起作用:
打印('GenAd{}不起作用。格式(g))
打印()
自身解决方案计数+=1
def溶液_计数(自身):
返回自我。\u解决方案\u计数
def main():
#这里,我们初始化数据变量
出纳人数=4
num_genads=4
移位数=3
天数=3
所有出纳=范围(出纳数)
all_genads=范围(num_genads)
所有移位=范围(num移位)
所有天数=范围(天数)
#创建模型
model=cp_model.CpModel()
#为收银员创建移位变量
#班次[(n,d,s)]:出纳“n”在“d”天轮班工作
班次\u出纳={}
对于所有出纳中的n:
对于所有天的d:
对于所有轮班的s:
班次\u出纳[(n,d,s)]=model.NewBoolVar('shift\u n%id%是%i'(n,d,s))
#为一般管理员创建移位变量
#班次[(g、d、s)]:总经理“g”在“d”天轮班工作
移位_genad={}
对于g,在所有情况下:
对于所有天的d:
对于所有轮班的s:
shift_genad[(g,d,s)]=model.NewBoolVar('shift_g%id%是%i'(g,d,s))
#在计划期内,每个班次只分配给一名出纳和一名管理员
对于所有天的d:
对于所有轮班的s:
模型.添加(所有出纳中n的总和(移位出纳[(n,d,s)])=1)
模型.Add(所有n个n个n的总和(移位量[(g,d,s)])==1)
#每个出纳每天最多工作一班
对于所有出纳中的n:
对于所有天的d:
模型.为所有班次中的s添加(班次和出纳[(n,d,s)])您的问题没有解决方案。因此解决方案回调没有打印任何内容是很正常的。嗨,Laurent。我知道您来自Google。您知道我如何处理这个问题并获得预期的输出吗?谢谢您检查您的模型。它是错的。@mrd\U ds在PyCharm中快速检查表明此行是错误的model.Add(sum)(shifts\u genad[(g,d,s)]对于n在所有年份中)==1)
(注意g和n)Stradivari,非常感谢。这是一个很大的帮助!你能告诉我你是如何快速检查它的吗?非常感谢!