'Python Cplex Ceil() Function
Hello, I am studying a published paper. One of the constraints from this paper(which can be shown in the link) requires the ceil() function. We can use the ceil() function at the OPL but I couldn't find the Python equality.
Notice: I tried the math.ceil() and cp.moduler ceil(), but I did not get any solution.
Edit: I still haven't found its Python equivalent, but there is an alternative way:
slack_var = model.continuous_var(name="slack")
ceiled_var = model.integer_var(name='ceiled')
Notice: The slack variable has to be less than "1". ( 0 <= slack < 1)
ceiled_var - slack_var = "your calculations"
Solution 1:[1]
For ceil, you can use both math or numpy, however, numpy can be overkill.
import math
ceiled_variable = math.ceil(your_variable)
or
import numpy as np
ceiled_variable = np.ceil(your_variable)
Solution 2:[2]
ceil is not a linear function so you should linearize. See example here.
from docplex.mp.model import Model
mdl = Model(name='ceil')
r=range(0,4)
x=[1.5,4.0,2.0001,5.9999]
y = mdl.integer_var_list(4,0,1000,name="y")
f = mdl.continuous_var_list(4,0,0.9999999,name="f")
for i in r:
mdl.add(y[i]==x[i]+f[i])
mdl.solve()
for i in r:
print(x[i]," ==> ",y[i].solution_value)
"""
which gives
1.5 ==> 2.0
4.0 ==> 4.0
2.0001 ==> 3.0
5.9999 ==> 6.0
"""
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | herdogan |
Solution 2 | Alex Fleischer |