CONOPT
Loading...
Searching...
No Matches
tutorial.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 TutModelData(co.ModelData):
18 def __init__(self):
19 self.Al = 0.16
20 self.Ak = 2.0
21 self.Ainp = 0.16
22 self.Rho = 1.0
23 self.K = 4.0
24 super().__init__()
25
26 def buildModel(self):
27 """
28 adding the variables and constraints to the model
29 @ingroup PYTHON1THREAD_TUTORIAL
30 """
31 # adding the variables to the model
32 self.varl = self.addVariable(0.1, co.Conopt.Infinity, 0.5)
33 self.varinp = self.addVariable(0.1, co.Conopt.Infinity, 0.5)
34 self.varout = self.addVariable(0.0, co.Conopt.Infinity)
35 self.varp = self.addVariable(0.0, co.Conopt.Infinity)
36
37 # adding the constraints to the model
38 self.consobj = self.addConstraint(
39 co.ConstraintType_Free,
40 -0.1,
41 [self.varl, self.varinp, self.varout, self.varp],
42 [-1, -1, 0, 0],
43 [0, 0, 1, 1],
44 )
45 self.consprod = self.addConstraint(
46 co.ConstraintType_Eq,
47 0.0,
48 [self.varl, self.varinp, self.varout],
49 [0, 0, -1],
50 [1, 1, 0],
51 )
52 self.addConstraint(
53 co.ConstraintType_Eq, 4.0, [self.varout, self.varp], [1, 2], [0, 0]
54 )
55
56 # setting the objective constraint
57 self.setObjectiveElement(co.ObjectiveElement_Constraint, 0)
58
59 # setting the optimisation direction
60 self.setOptimizationSense(co.Sense_Maximize)
61
62 def evaluateNonlinearTerm(self, x, rowno, ignerr, thread):
63 """
64 @copydoc conopt.ModelData.evaluateNonlinearTerm
65 @ingroup PYTHON1THREAD_TUTORIAL
66 """
67 L = x[self.varl]
68 Inp = x[self.varinp]
69 Out = x[self.varout]
70 P = x[self.varp]
71
72 g = 0
73 if rowno == self.consobj:
74 g = P * Out
75 elif rowno == self.consprod:
76 hold1 = (
77 self.Al * pow(L, (-self.Rho))
78 + self.Ak * pow(self.K, (-self.Rho))
79 + self.Ainp * pow(Inp, (-self.Rho))
80 )
81 hold2 = pow(hold1, (-1.0 / self.Rho))
82
83 g = hold2
84
85 return g
86
87 def evaluateNonlinearJacobian(self, x, rowno, jacnum, ignerr, thread):
88 """
89 @copydoc conopt.ModelData.evaluateNonlinearJacobian
90
91 NOTE: The jacobian is returned as a list of length jacnum. In this
92 example, the returned list is constructed using `append`. It is also
93 possible to initially create a list of length jacnum containing only 0s,
94 then update the values by the variable indices.
95
96 @ingroup PYTHON1THREAD_TUTORIAL
97 """
98 L = x[self.varl]
99 Inp = x[self.varinp]
100 Out = x[self.varout]
101 P = x[self.varp]
102
103 jac = []
104 if rowno == self.consobj:
105 jac.append(P)
106 jac.append(Out)
107 elif rowno == self.consprod:
108 hold1 = (
109 self.Al * pow(L, (-self.Rho))
110 + self.Ak * pow(self.K, (-self.Rho))
111 + self.Ainp * pow(Inp, (-self.Rho))
112 )
113 hold2 = pow(hold1, (-1.0 / self.Rho))
114 hold3 = hold2 / hold1
115
116 jac.append(hold3 * self.Al * pow(L, (-self.Rho - 1.0)))
117 jac.append(hold3 * self.Ainp * pow(Inp, (-self.Rho - 1.0)))
118
119 return jac
120
121
122if __name__ == '__main__':
123 name = os.path.basename(__file__)[:-3]
124
125 conopt = co.Conopt(name)
126 model = TutModelData()
127 msghdlr = std.TutMessageHandler(name)
128
129 model.buildModel()
130
131 conopt.loadModel(model)
132 conopt.setMessageHandler(msghdlr)
133
134 # getting the license variables
135 license_int_1 = os.environ.get('CONOPT_LICENSE_INT_1', None)
136 license_int_2 = os.environ.get('CONOPT_LICENSE_INT_2', None)
137 license_int_3 = os.environ.get('CONOPT_LICENSE_INT_3', None)
138 license_text = os.environ.get('CONOPT_LICENSE_TEXT', None)
139 if (
140 license_int_1 is not None
141 and license_int_2 is not None
142 and license_int_3 is not None
143 and license_text is not None
144 ):
145 conopt.setLicense(
146 int(license_int_1),
147 int(license_int_2),
148 int(license_int_3),
149 license_text,
150 )
151
152 coi_error = conopt.solve()
153
154 retcode = std.checkSolve(conopt, 0.572943, coi_error)
155
156 sys.exit(retcode)
static int checkSolve(String name, int model_status, int solution_status, double objective, double expected_objective, double tol)
Definition std.java:20
evaluateNonlinearTerm(self, x, rowno, ignerr, thread)
callback method for evaluating the nonlinear terms in a given row
Definition tutorial.py:62
buildModel(self)
adding the variables and constraints to the model
Definition tutorial.py:26
evaluateNonlinearJacobian(self, x, rowno, jacnum, ignerr, thread)
callback method for evaluating the jacobian for the nonlinear terms in a given row
Definition tutorial.py:87