pythoncplexdocplex

Can someone fix my python code & docplex?


I have to do a python code for a project but when i run it, i have a problem with it. I try to fix it but it's always the same problem. It said the same code error...

this is my code

from docplex.mp.model import Model
m=Model(name="bedford")
X1=m.continuous_var(name='Ashley')
X2=m.continuous_var(name='Bedford')
X3=m.continuous_var(name='Cosnol')
X4=m.continuous_var(name='Dunby')
X5=m.continuous_var(name='Earlam')
X6=m.continuous_var(name='Florence')
X7=m.continuous_var(name='Gaston')
X8=m.continuous_var(name='Hopt')
#average volatility
avgvol=19
#volatility constraint
Volatility=m.add_constraint((15-avgvol)*X1+ 
                            (16-avgvol)*X2+ 
                            (18-avgvol)*X3+ 
                            (20-avgvol)*X4+ 
                            (21-avgvol)*X5+ 
                            (22-avgvol)*X6+ 
                            (23-avgvol)*X7+ 
                            (24-avgvol)*X8 
                            >=0,"volatility_constraint"
                           )
#Capacity constraint
Supply=m.add_constraint(X1+X2+X3+X4+X5+X6+X7+X8==1225,"Supply")
#Rail, Truck & Union constraints
Truck=m.add_constraint(X2+X4+X5+X6==720,"Truck")
Rail=m.add_constraint(X1+X3+X7+X8==650,"Rail")
Union=m.add_constraint(X1+X2-X3+X4-X5+X6-X7-X8>=0,"Union")
#Constraints
Ashley=m.add_constraint(X1<= 300,"Ashley")
Bedford=m.add_constraint(X2<= 600,"Bedford")
Cosnol=m.add_constraint(X3<= 500,"Cosnol")
Dunby=m.add_constraint(X4<= 655,"Dunby")
Earlam=m.add_constraint(X5<= 575,"Earlam")
Florence=m.add_constraint(X6<= 680,"Florence")
Gaston=m.add_constraint(X7<= 450,"Gaston")
Hopt=m.add_constraint(X8<=490,"Hopt")
m.minimize(X1*49.5+X2*50+X3*61+X4*63.5+X5*66.5+X6*71+X7*72.5+X8*80)
m.print_information()
s=m.solve()
m.print_solution()

and the error code: I tried to understand but at the final it's always the same problem, I need a help of someone

DOcplexException                          Traceback (most recent call last)
Input In [6], in <cell line: 49>()
     47 m.print_information()
     48 s=m.solve()
---> 49 m.print_solution()
File ~/opt/anaconda3/lib/python3.9/site-packages/docplex/mp/model.py:6081, in         Model.print_solution(self, print_zeros, solution_header_fmt, var_value_fmt, **kwargs)
   6063 def print_solution(self, print_zeros=False,
   6064                    solution_header_fmt=None,
   6065                    var_value_fmt=None,
   6066                    **kwargs):
   6067     """  Prints the values of the model variables after a solve.
   6068 
   6069     Only valid after a successful solve. If the model has not been solved successfully, an
   (...)
   6079         :func:`docplex.mp.solution.SolveSolution.display`
   6080     """
-> 6081     self._check_has_solution()
   6082     if var_value_fmt is None:
   6083         if self._has_username_with_spaces():
File ~/opt/anaconda3/lib/python3.9/site-packages/docplex/mp/model.py:5189, in     Model._check_has_solution(self)
   5187     self.fatal("Model<{0}> has not been solved yet", self.name)
   5188 else:
-> 5189     self.fatal("Model<{0}> did not solve successfully", self.name)
File ~/opt/anaconda3/lib/python3.9/site-packages/docplex/mp/model.py:1080, in Model.fatal(self, msg, *args)
   1079 def fatal(self, msg, *args):
-> 1080     self._error_handler.fatal(msg, args)
File ~/opt/anaconda3/lib/python3.9/site-packages/docplex/mp/error_handler.py:210, in AbstractErrorHandler.fatal(self, msg, args)
    208 resolved_message = resolve_pattern(msg, args)
    209 docplex_error_stop_here()
--> 210 raise DOcplexException(resolved_message)
DOcplexException: Model<bedford> did not solve successfully

Solution

  • if you ask for the log in

    m.solve(log_output=True)
    

    then you will get

    Row 'Supply' is dependent and infeasible.
    

    And then if you remove

    Supply=m.add_constraint(X1+X2+X3+X4+X5+X6+X7+X8==1225,"Supply")
    

    you will get a solution

    objective: 82353.000
      Ashley=200.000
      Bedford=488.000
      Earlam=232.000
      Gaston=450.000
    

    If you sant to see conflicts like in https://github.com/AlexFleischerParis/zoodocplex/blob/master/zoorelaxationandconflict.py

    from docplex.mp.model import Model
    from docplex.mp.conflict_refiner import ConflictRefiner
    
    m=Model(name="bedford")
    X1=m.continuous_var(name='Ashley')
    X2=m.continuous_var(name='Bedford')
    X3=m.continuous_var(name='Cosnol')
    X4=m.continuous_var(name='Dunby')
    X5=m.continuous_var(name='Earlam')
    X6=m.continuous_var(name='Florence')
    X7=m.continuous_var(name='Gaston')
    X8=m.continuous_var(name='Hopt')
    #average volatility
    avgvol=19
    #volatility constraint
    Volatility=m.add_constraint((15-avgvol)*X1+ 
                                (16-avgvol)*X2+ 
                                (18-avgvol)*X3+ 
                                (20-avgvol)*X4+ 
                                (21-avgvol)*X5+ 
                                (22-avgvol)*X6+ 
                                (23-avgvol)*X7+ 
                                (24-avgvol)*X8 
                                >=0,"volatility_constraint"
                               )
    #Capacity constraint
    Supply=m.add_constraint(X1+X2+X3+X4+X5+X6+X7+X8==1225,"Supply")
    #Rail, Truck & Union constraints
    Truck=m.add_constraint(X2+X4+X5+X6==720,"Truck")
    Rail=m.add_constraint(X1+X3+X7+X8==650,"Rail")
    Union=m.add_constraint(X1+X2-X3+X4-X5+X6-X7-X8>=0,"Union")
    #Constraints
    Ashley=m.add_constraint(X1<= 300,"Ashley")
    Bedford=m.add_constraint(X2<= 600,"Bedford")
    Cosnol=m.add_constraint(X3<= 500,"Cosnol")
    Dunby=m.add_constraint(X4<= 655,"Dunby")
    Earlam=m.add_constraint(X5<= 575,"Earlam")
    Florence=m.add_constraint(X6<= 680,"Florence")
    Gaston=m.add_constraint(X7<= 450,"Gaston")
    Hopt=m.add_constraint(X8<=490,"Hopt")
    m.minimize(X1*49.5+X2*50+X3*61+X4*63.5+X5*66.5+X6*71+X7*72.5+X8*80)
    m.print_information()
    #m.solve(log_output=True)
    
    cr=ConflictRefiner()
    conflicts=cr.refine_conflict(m)
    conflicts.display()
    m.print_solution()
    

    and you will get

    conflict(s): 3
      - status: Member, LinearConstraint: Supply: Ashley + Bedford + Cosnol + Dunby + Earlam + Flo.. == 1225
      - status: Member, LinearConstraint: Truck: Bedford + Dunby + Earlam + Florence == 720
      - status: Member, LinearConstraint: Rail: Ashley + Cosnol + Gaston + Hopt == 650