CONOPT
Loading...
Searching...
No Matches
tutorialk.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 super().__init__()
24
25 def buildModel(self):
26 """
27 adding the variables and constraints to the model
28 @ingroup PYTHON1THREAD_TUTORIALk
29 """
30 # adding the variables to the model
31 self.varl = self.addVariable(0.1, co.Conopt.Infinity, 0.5)
32 self.vark = self.addVariable(4.0, 4.0, 4.0)
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.vark, self.varinp, self.varout],
49 [0, 0, 0, -1],
50 [1, 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_TUTORIALk
66 """
67 L = x[self.varl]
68 K = x[self.vark]
69 Inp = x[self.varinp]
70 Out = x[self.varout]
71 P = x[self.varp]
72
73 g = 0
74 if rowno == self.consobj:
75 g = P * Out
76 elif rowno == self.consprod:
77 hold1 = (
78 self.Al * pow(L, (-self.Rho))
79 + self.Ak * pow(K, (-self.Rho))
80 + self.Ainp * pow(Inp, (-self.Rho))
81 )
82 hold2 = pow(hold1, (-1.0 / self.Rho))
83
84 g = hold2
85
86 return g
87
88 def evaluateNonlinearJacobian(self, x, rowno, jacnum, ignerr, thread):
89 """
90 @copydoc conopt.ModelData.evaluateNonlinearJacobian
91
92 NOTE: The jacobian is returned as a list of length jacnum. In this
93 example, the returned list is constructed using `append`. It is also
94 possible to initially create a list of length jacnum containing only 0s,
95 then update the values by the variable indices.
96
97 @ingroup PYTHON1THREAD_TUTORIALk
98 """
99 L = x[self.varl]
100 K = x[self.vark]
101 Inp = x[self.varinp]
102 Out = x[self.varout]
103 P = x[self.varp]
104
105 jac = []
106 if rowno == self.consobj:
107 jac.append(P)
108 jac.append(Out)
109 elif rowno == self.consprod:
110 hold1 = (
111 self.Al * pow(L, (-self.Rho))
112 + self.Ak * pow(K, (-self.Rho))
113 + self.Ainp * pow(Inp, (-self.Rho))
114 )
115 hold2 = pow(hold1, (-1.0 / self.Rho))
116 hold3 = hold2 / hold1
117
118 jac.append(hold3 * self.Al * pow(L, (-self.Rho - 1.0)))
119 jac.append(hold3 * self.Ak * pow(K, (-self.Rho - 1.0)))
120 jac.append(hold3 * self.Ainp * pow(Inp, (-self.Rho - 1.0)))
121
122 return jac
123
124
125if __name__ == '__main__':
126 name = os.path.basename(__file__)[:-3]
127
128 conopt = co.Conopt(name)
129 model = TutModelData()
130 msghdlr = std.TutMessageHandler(name)
131
132 model.buildModel()
133
134 conopt.loadModel(model)
135 conopt.setMessageHandler(msghdlr)
136
137 # getting the license variables
138 license_int_1 = os.environ.get('CONOPT_LICENSE_INT_1', None)
139 license_int_2 = os.environ.get('CONOPT_LICENSE_INT_2', None)
140 license_int_3 = os.environ.get('CONOPT_LICENSE_INT_3', None)
141 license_text = os.environ.get('CONOPT_LICENSE_TEXT', None)
142 if (
143 license_int_1 is not None
144 and license_int_2 is not None
145 and license_int_3 is not None
146 and license_text is not None
147 ):
148 conopt.setLicense(
149 int(license_int_1),
150 int(license_int_2),
151 int(license_int_3),
152 license_text,
153 )
154
155 coi_error = conopt.solve()
156
157 retcode = std.checkSolve(conopt, 0.572943, coi_error)
158
159 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
evaluateNonlinearJacobian(self, x, rowno, jacnum, ignerr, thread)
callback method for evaluating the jacobian for the nonlinear terms in a given row
Definition tutorialk.py:88
evaluateNonlinearTerm(self, x, rowno, ignerr, thread)
callback method for evaluating the nonlinear terms in a given row
Definition tutorialk.py:62
buildModel(self)
adding the variables and constraints to the model
Definition tutorialk.py:25