CONOPT
Loading...
Searching...
No Matches
square3.py
Go to the documentation of this file.
7
8import sys
9import os
10
11import conopt as co
12
13sys.path.append('../common/')
14import std
15
16
17class SquareModelData(co.ModelData):
18 def __init__(self):
19 super().__init__()
20
21 def buildModel(self):
22 """
23 adding the variables and constraints to the model
24 @ingroup PYTHON1THREAD_SQUARE3
25 """
26 # Model x0 + x1 = 10
27 # x0 - x1 = 0
28 # x0 + x1 <= 9
29 # adding the variables to the model
30 self.x0 = self.addVariable(-co.Conopt.Infinity, co.Conopt.Infinity, 5.0)
31 self.x1 = self.addVariable(-co.Conopt.Infinity, co.Conopt.Infinity, 5.0)
32
33 # adding the constraints to the model
34 self.cons1 = self.addConstraint(
35 co.ConstraintType_Eq, 10.0, [self.x0, self.x1], [1, 1], [0, 0]
36 )
37 self.cons2 = self.addConstraint(
38 co.ConstraintType_Eq, 0.0, [self.x0, self.x1], [0, 0], [1, 1]
39 )
40 self.cons3 = self.addConstraint(
41 co.ConstraintType_LtEq, 9.0, [self.x0, self.x1], [0, 0], [1, 1]
42 )
43
44 def evaluateNonlinearTerm(self, x, rowno, ignerr, thread):
45 """
46 @copydoc conopt.ModelData.evaluateNonlinearTerm
47 @ingroup PYTHON1THREAD_SQUARE3
48 """
49 g = 0
50 if rowno == self.cons1:
51 pass
52
53 elif rowno == self.cons2:
54 g = x[self.x0] - x[self.x1]
55
56 elif rowno == self.cons3:
57 g = x[self.x0] + x[self.x1]
58
59 return g
60
61 def evaluateNonlinearJacobian(self, x, rowno, jacnum, ignerr, thread):
62 """
63 @copydoc conopt.ModelData.evaluateNonlinearJacobian
64 @ingroup PYTHON1THREAD_SQUARE3
65 """
66 jac = []
67 if rowno == self.cons1:
68 pass
69
70 elif rowno == self.cons2:
71 jac.append(1)
72 jac.append(-1)
73
74 elif rowno == self.cons3:
75 jac.append(1)
76 jac.append(1)
77
78 return jac
79
80
81if __name__ == '__main__':
82 name = os.path.basename(__file__)[:-3]
83
84 conopt = co.Conopt(name)
85 model = SquareModelData()
86 msghdlr = std.TutMessageHandler(name)
87
88 model.buildModel()
89
90 conopt.loadModel(model)
91 conopt.setMessageHandler(msghdlr)
92
93 # tell conopt this is a square system
94 conopt.squareModel(1)
95
96 # getting the license variables
97 license_int_1 = os.environ.get('CONOPT_LICENSE_INT_1', None)
98 license_int_2 = os.environ.get('CONOPT_LICENSE_INT_2', None)
99 license_int_3 = os.environ.get('CONOPT_LICENSE_INT_3', None)
100 license_text = os.environ.get('CONOPT_LICENSE_TEXT', None)
101 if (
102 license_int_1 is not None
103 and license_int_2 is not None
104 and license_int_3 is not None
105 and license_text is not None
106 ):
107 conopt.setLicense(
108 int(license_int_1),
109 int(license_int_2),
110 int(license_int_3),
111 license_text,
112 )
113
114 coi_error = conopt.solve()
115
116 if coi_error:
117 std.python_log(conopt, 'Errors encountered during solution')
118
119 elif (
120 (conopt.solutionStatus() != 1)
121 or (conopt.modelStatus() < 4)
122 or (conopt.modelStatus() > 5)
123 ):
124 std.python_log(
125 conopt, 'Solver or Model status not as expected (1,4) or (1,5)'
126 )
127
128 # printing the final status of the optimisation
129 conopt.printStatus()
130
131 std.python_log(conopt, 'Successful Solve')
evaluateNonlinearJacobian(self, x, rowno, jacnum, ignerr, thread)
callback method for evaluating the jacobian for the nonlinear terms in a given row
Definition square3.py:61
buildModel(self)
adding the variables and constraints to the model
Definition square3.py:21
evaluateNonlinearTerm(self, x, rowno, ignerr, thread)
callback method for evaluating the nonlinear terms in a given row
Definition square3.py:44