from docplex.mp.model import Model
from docplex.mp.model_reader import ModelReader

m = Model("has an infinity on rhs")
v1 = m.continuous_var(ub = 10, name = "v1")
v2 = m.continuous_var(ub = 10, name = "v2")
m.add_constraint(3*v1 + 2*v2 <= float("inf"))
m.maximize(5*v1 + 4*v2)
print m.solve()

file1 = m.export_as_lp("has_infiniy.lp")
file2 = m.export_as_sav("has_infinity.sav")

"""
Explanation: two minor model writing/reading bugs in docplex
Two bugs, both minor but worth reporting.
 1. If I build a model with the RHS having float("inf") the .lp that is exported is unreadable. 
 1. When I export a model as a .sav file, it doesn't export into the current directory but uses some funny path instead.
End of explanation
"""


import os
os.path.exists(os.path.join(os.getcwd(), "has_infiniy.lp"))

os.path.exists(os.path.join(os.getcwd(), "has_infinity.sav"))

file2

"""
Explanation: Bug 1 - the export_as_sav doesn't use the current working directory.
End of explanation
"""


mr =ModelReader()
mr.read_model(file1)

"""
Explanation: Bug 2  It can't read the file that was written. The 'inf' is tripping it up.
End of explanation
"""


m2 = mr.read_model(file2)

m2.get_constraint_by_index(0)

"""
Explanation: The save format does work here.
End of explanation
"""


m = Model("no infinity")
v1 = m.continuous_var(ub = 10, name = "v1")
v2 = m.continuous_var(ub = 10, name = "v2")
m.add_constraint(3*v1 + 2*v2 <= 10000)
m.maximize(5*v1 + 4*v2)
print m.solve()

file1 = m.export_as_lp("no_infiniy.lp")

m2 = mr.read_model(file1)

"""
Explanation: Just to make this totally clear, if I remove the infinity, then the .lp file writing/reading works.
End of explanation
"""


