gpt4 book ai didi

java - Jython与ortool库

转载 作者:行者123 更新时间:2023-12-01 16:33:47 24 4
gpt4 key购买 nike

我尝试从我的Java程序(使用Jython)运行此python脚本:

from __future__ import print_function
from ortools.sat.python import cp_model
def main():
# This program tries to find an optimal assignment of nurses to shifts
# (3 shifts per day, for 7 days), subject to some constraints (see below).
# Each nurse can request to be assigned to specific shifts.
# The optimal assignment maximizes the number of fulfilled shift requests.
num_nurses = 5
num_shifts = 3
num_days = 7
all_nurses = range(num_nurses)
all_shifts = range(num_shifts)
all_days = range(num_days)
shift_requests = [[[0, 0, 1], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 1],
[0, 1, 0], [0, 0, 1]],
[[0, 0, 0], [0, 0, 0], [0, 1, 0], [0, 1, 0], [1, 0, 0],
[0, 0, 0], [0, 0, 1]],
[[0, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 0], [0, 0, 0],
[0, 1, 0], [0, 0, 0]],
[[0, 0, 1], [0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 0],
[1, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 0, 0], [1, 0, 0],
[0, 1, 0], [0, 0, 0]]]
# Creates the model.
model = cp_model.CpModel()

# Creates shift variables.
# shifts[(n, d, s)]: nurse 'n' works shift 's' on day 'd'.
shifts = {}
for n in all_nurses:
for d in all_days:
for s in all_shifts:
shifts[(n, d,
s)] = model.NewBoolVar('shift_n%id%is%i' % (n, d, s))

# Each shift is assigned to exactly one nurse in .
for d in all_days:
for s in all_shifts:
model.Add(sum(shifts[(n, d, s)] for n in all_nurses) == 1)

# Each nurse works at most one shift per day.
for n in all_nurses:
for d in all_days:
model.Add(sum(shifts[(n, d, s)] for s in all_shifts) <= 1)

# min_shifts_assigned is the largest integer such that every nurse can be
# assigned at least that number of shifts.
min_shifts_per_nurse = (num_shifts * num_days) // num_nurses
max_shifts_per_nurse = min_shifts_per_nurse + 1
for n in all_nurses:
num_shifts_worked = sum(
shifts[(n, d, s)] for d in all_days for s in all_shifts)
model.Add(min_shifts_per_nurse <= num_shifts_worked)
model.Add(num_shifts_worked <= max_shifts_per_nurse)

model.Maximize(
sum(shift_requests[n][d][s] * shifts[(n, d, s)] for n in all_nurses
for d in all_days for s in all_shifts))
# Creates the solver and solve.
solver = cp_model.CpSolver()
solver.Solve(model)
for d in all_days:
print('Day', d)
for n in all_nurses:
for s in all_shifts:
if solver.Value(shifts[(n, d, s)]) == 1:
if shift_requests[n][d][s] == 1:
print('Nurse', n, 'works shift', s, '(requested).')
else:
print('Nurse', n, 'works shift', s, '(not requested).')
print()

# Statistics.
print()
print('Statistics')
print(' - Number of shift requests met = %i' % solver.ObjectiveValue(),
'(out of', num_nurses * min_shifts_per_nurse, ')')
print(' - wall time : %f s' % solver.WallTime())


if __name__ == '__main__':
main()


问题是,它无法识别ortool库。当我通过python IDE运行此脚本时,它的工作正常(我在python中安装了ortool库)。但是当我通过Jython运行它时,由于它无法识别ortool库,因此无法正常工作。

有人知道如何解决?

谢谢。

最佳答案

使用Jython,您不能使用许多第三方Python库,例如NumPy,SciPy,OR-Tools或任何使用C扩展的库。
抱歉。

关于java - Jython与ortool库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62002809/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com