def test_cov7(self): # Testing error checking with return value M = self._setup() def f(model): return () try: M.cc = Complementarity(rule=f) self.fail("Expected ValueError") except ValueError: pass def f(model): return try: M.cc = Complementarity(rule=f) self.fail("Expected ValueError") except ValueError: pass def f(model): return {} try: M.cc = Complementarity(rule=f) self.fail("Expected ValueError") except ValueError: pass
def test_t1a(self): # y + x1 >= 0 _|_ x1 + 2*x2 + 3*x3 >= 1 M = self._setup() M.c = Constraint(expr=M.y + M.x3 >= M.x2) M.cc = Complementarity( expr=complements(M.y + M.x1 >= 0, M.x1 + 2 * M.x2 + 3 * M.x3 >= 1)) self._test("t1a", M)
def test_t1b(self): # Reversing the expressions in test t1a: # x1 + 2*x2 + 3*x3 >= 1 _|_ y + x1 >= 0 M = self._setup() M.cc = Complementarity( expr=complements(M.x1 + 2 * M.x2 + 3 * M.x3 >= 1, M.y + M.x1 >= 0)) self._test("t1b", M)
def test_t2b(self): # Reversing the expressions in test t2a: # x2 - x3 <= -1 _|_ y + x2 >= 0 M = self._setup() M.cc = Complementarity( expr=complements(M.x2 - M.x3 <= -1, M.y + M.x2 >= 0)) self._test("t2b", M)
def test_t4b(self): # Reversing the expressions in test t7b: # y + x3 _|_ x1 + 2*x2 + 3*x3 = 1 M = self._setup() M.cc = Complementarity( expr=complements(M.y + M.x3, M.x1 + 2 * M.x2 + 3 * M.x3 == 1)) self._test("t4b", M)
def test_t3b(self): # Reversing the expressions in test t3a: # x1 + x2 >= -1 _|_ y + x3 >= 0 M = self._setup() M.cc = Complementarity( expr=complements(M.x1 + M.x2 >= -1, M.y + M.x3 >= 0)) self._test("t3b", M)
def test_cov9(self): # Testing construction with a tuple M = self._setup() def f(model): return (M.y + M.x3, M.x1 + 2*M.x2 == 1) M.cc = Complementarity(rule=f) self._test("cov8", M)
def test_cov4(self): # Testing construction with no indexing and a rule M = self._setup() def f(model): return complements(M.y + M.x3, M.x1 + 2*M.x2 == 1) M.cc = Complementarity(rule=f) self._test("cov4", M)
def test_t9(self): # Testing that we can skip deactivated complementarity conditions M = self._setup() M.cc = Complementarity(expr=complements(M.y + M.x3, M.x1 + 2 * M.x2 == 1)) M.cc.deactivate() self._test("t9", M)
def test_t10(self): # Testing that we can skip an array of deactivated complementarity conditions M = self._setup() def f(model, i): return complements(M.y + M.x3, M.x1 + 2*M.x2 == i) M.cc = Complementarity([0,1,2], rule=f) M.cc[1].deactivate() self._test("t10", M)
def test_cov2(self): # Testing warning for no rule""" M = self._setup() M.cc = Complementarity([0, 1, 2]) # AMPL needs at least one variable in the problem therefore # we need to have a constraint that keeps them around M.keep_var_con = Constraint(expr=M.x1 == 0.5) self._test("cov2", M)
def test_cov6(self): # Testing construction with indexing and an expression M = self._setup() try: M.cc = Complementarity([0, 1], expr=()) self.fail("Expected an IndexError") except IndexError: pass
def test_cov5(self): # Testing construction with rules that generate an exception M = self._setup() def f(model): raise IOError("cov5 error") try: M.cc1 = Complementarity(rule=f) self.fail("Expected an IOError") except IOError: pass def f(model, i): raise IOError("cov5 error") try: M.cc2 = Complementarity([0,1], rule=f) self.fail("Expected an IOError") except IOError: pass
def test_cov11(self): # Testing construction with a badly formed expression M = self._setup() M.cc = Complementarity(expr=complements(1 <= M.x1 <= M.y, M.x2)) try: M.cc.to_standard_form() self.fail("Expected a RuntimeError") except RuntimeError: pass
def test_t9(self): # Testing that we can skip deactivated complementarity conditions M = self._setup() M.cc = Complementarity(expr=complements(M.y + M.x3, M.x1 + 2 * M.x2 == 1)) M.cc.deactivate() # AMPL needs at least one variable in the problem therefore # we need to have a constraint that keeps them around M.keep_var_con = Constraint(expr=M.x1 == 0.5) self._test("t9", M)
def test_t13(self): # Testing that we can skip an array of deactivated complementarity conditions M = self._setup() def f(model, i): if i == 0: return complements(M.y + M.x3, M.x1 + 2*M.x2 == 0) if i == 1: return Complementarity.Skip if i == 2: return complements(M.y + M.x3, M.x1 + 2*M.x2 == 2) M.cc = Complementarity([0,1,2], rule=f) self._test("t13", M)
def get_model(self): m = ConcreteModel() m.x = Var(bounds=(-100, 100)) m.obj = Objective(expr=m.x) m.disjunct1 = Disjunct() m.disjunct1.comp = Complementarity(expr=complements(m.x >= 0, 4*m.x - 3 >= 0)) m.disjunct2 = Disjunct() m.disjunct2.cons = Constraint(expr=m.x >= 2) m.disjunction = Disjunction(expr=[m.disjunct1, m.disjunct2]) return m
m.x3 = Var(within=Reals, bounds=(0, None), initialize=2.59064374743843) m.x4 = Var(within=Reals, bounds=(0, None), initialize=0.935385768072125) m.x5 = Var(within=Reals, bounds=(0, None), initialize=17.9489523419981) m.x6 = Var(within=Reals, bounds=(0, None), initialize=4.09781044734645) m.x7 = Var(within=Reals, bounds=(0, None), initialize=1.30472575767983) m.x8 = Var(within=Reals, bounds=(0, None), initialize=5.59008254355618) m.x9 = Var(within=Reals, bounds=(0, None), initialize=3.22217945382394) m.x10 = Var(within=Reals, bounds=(0, None), initialize=1.67709431683909) m.x11 = Var(within=Reals, bounds=(None, None), initialize=47.2793507462771) m.obj = Objective(expr=m.x10 * m.x11 - 3 * m.x10 - 9.23329152870807 * m.x10**2.33333333333333, sense=maximize) m.c1 = Complementarity(expr=( (10 * m.x1)**0.833333333333333 + 0.833333333333333 * m.x1 * m.x11 / (m.x1 + m.x2 + m.x3 + m.x4 + m.x5 + m.x6 + m.x7 + m.x8 + m.x9 + m.x10) - m.x11 >= -5, m.x1 >= 0)) m.c2 = Complementarity(expr=( (10 * m.x2)**1 + 0.833333333333333 * m.x2 * m.x11 / (m.x1 + m.x2 + m.x3 + m.x4 + m.x5 + m.x6 + m.x7 + m.x8 + m.x9 + m.x10) - m.x11 >= -3, m.x2 >= 0)) m.c3 = Complementarity(expr=( (10 * m.x3)**1.11111111111111 + 0.833333333333333 * m.x3 * m.x11 / (m.x1 + m.x2 + m.x3 + m.x4 + m.x5 + m.x6 + m.x7 + m.x8 + m.x9 + m.x10) - m.x11 >= -8, m.x3 >= 0)) m.c4 = Complementarity(expr=( (10 * m.x4)**1.66666666666667 + 0.833333333333333 * m.x4 * m.x11 / (m.x1 + m.x2 + m.x3 + m.x4 + m.x5 + m.x6 + m.x7 + m.x8 + m.x9 + m.x10) -
# ex1a.py import pyomo.environ as pyo from pyomo.mpec import Complementarity, complements n = 5 model = pyo.ConcreteModel() model.x = pyo.Var( range(1,n+1) ) model.f = pyo.Objective(expr=sum(i*(model.x[i]-1)**2 for i in range(1,n+1)) ) def compl_(model, i): return complements(model.x[i] >= 0, model.x[i+1] >= 0) model.compl = Complementarity( range(1,n), rule=compl_ )
def test_t4c(self): # 1 = x1 + 2*x2 + 3*x3 _|_ y + x3 M = self._setup() M.cc = Complementarity( expr=complements(1 == M.x1 + 2 * M.x2 + 3 * M.x3, M.y + M.x3)) self._test("t4c", M)
def test_t3a(self): # y + x3 >= 0 _|_ x1 + x2 >= -1 M = self._setup() M.cc = Complementarity( expr=complements(M.y + M.x3 >= 0, M.x1 + M.x2 >= -1)) self._test("t3a", M)
def test_t1c(self): # y >= - x1 _|_ x1 + 2*x2 >= 1 - 3*x3 M = self._setup() M.cc = Complementarity( expr=complements(M.y >= -M.x1, M.x1 + 2 * M.x2 >= 1 - 3 * M.x3)) self._test("t1c", M)
def test_t2a(self): # y + x2 >= 0 _|_ x2 - x3 <= -1 M = self._setup() M.cc = Complementarity( expr=complements(M.y + M.x2 >= 0, M.x2 - M.x3 <= -1)) self._test("t2a", M)
def test_t4d(self): # x1 + 2*x2 == 1 - 3*x3 _|_ y + x3 M = self._setup() M.cc = Complementarity( expr=complements(M.x1 + 2 * M.x2 == 1 - 3 * M.x3, M.y + M.x3)) self._test("t4d", M)
# # Nonzero counts # Total const NL DLL # 20 10 10 0 from pyomo.environ import * from pyomo.mpec import Complementarity model = m = ConcreteModel() m.x1 = Var(within=Reals,bounds=(None,None),initialize=1) m.x2 = Var(within=Reals,bounds=(None,None),initialize=1) m.x3 = Var(within=Reals,bounds=(None,None),initialize=1) m.x4 = Var(within=Reals,bounds=(None,None),initialize=1) m.x5 = Var(within=Reals,bounds=(None,None),initialize=1) m.x6 = Var(within=Reals,bounds=(None,None),initialize=1) m.c1 = Complementarity(expr=(m.x1*m.x2 == 1) m.c2 = Complementarity(expr=(m.x3/m.x1/m.x4 == 4.8) m.c3 = Complementarity(expr=(m.x5/m.x2/m.x6 == 0.98) m.c4 = Complementarity(expr=(m.x6*m.x4 == 1) m.c5 = Complementarity(expr=( m.x1 - m.x2 + 1E-7*m.x3 - 1E-5*m.x5 == 0) m.c6 = Complementarity(expr=( 2*m.x1 - 2*m.x2 + 1E-7*m.x3 - 0.01*m.x4 - 1E-5*m.x5 + 0.01*m.x6 == 0)
def test_t12(self): # x1 _|_ 2 <= y + x1 <= 3""" M = self._setup() M.cc = Complementarity( expr=complements(M.x1, inequality(2, M.y + M.x1, 3))) self._test("t12", M)
def test_cov6(self): # Testing construction with indexing and an expression M = self._setup() with self.assertRaisesRegex(ValueError, "Invalid tuple for Complementarity"): M.cc = Complementarity([0, 1], expr=())
def test_cov2(self): # Testing warning for no rule""" M = self._setup() M.cc = Complementarity([0, 1, 2]) self._test("cov2", M)
m.x83 = Var(within=Reals,bounds=(None,None),initialize=159.1419) m.x84 = Var(within=Reals,bounds=(0.06,0.06),initialize=0.06) m.x85 = Var(within=Reals,bounds=(0.06,0.06),initialize=0.06) m.x86 = Var(within=Reals,bounds=(39.1744,39.1744),initialize=39.1744) m.x87 = Var(within=Reals,bounds=(None,None),initialize=20.6884) m.x88 = Var(within=Reals,bounds=(None,None),initialize=46.1511) m.x89 = Var(within=Reals,bounds=(None,None),initialize=92.3023) m.x90 = Var(within=Reals,bounds=(None,None),initialize=0) m.x91 = Var(within=Reals,bounds=(0,0),initialize=0) m.x92 = Var(within=Reals,bounds=(58.759,58.759),initialize=58.759) m.x93 = Var(within=Reals,bounds=(None,None),initialize=548.7478) m.x94 = Var(within=Reals,bounds=(None,None),initialize=574.8463) m.x95 = Var(within=Reals,bounds=(None,None),initialize=100.1122) m.x96 = Var(within=Reals,bounds=(None,None),initialize=0) m.c1 = Complementarity(expr=(-0.90909*m.x1*(1.1 + m.x23) + m.x5 == 0) m.c2 = Complementarity(expr=(-0.81466*m.x1*(1.22751 + m.x23) + m.x6 == 0) m.c3 = Complementarity(expr=(-0.92521*m.x1*(1.08084 + m.x23) + m.x7 == 0) m.c4 = Complementarity(expr=( - m.x1 + m.x8 == 0) m.c5 = Complementarity(expr=( - m.x1 + m.x9 == 0) m.c6 = Complementarity(expr=( - m.x1 + m.x10 == 0) m.c7 = Complementarity(expr=(m.x17*m.x25 - (m.x2*m.x31 + m.x5*m.x37) == 0) m.c8 = Complementarity(expr=(m.x18*m.x26 - (m.x3*m.x32 + m.x6*m.x38) == 0)
def test_t11(self): # 2 <= y + x1 <= 3 _|_ x1 M = self._setup() M.cc = Complementarity(expr=complements(inequality(2, M.y + M.x1, 3), M.x1)) self._test("t11", M)