CONOPT
Loading...
Searching...
No Matches
largebnd.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_LARGEBND
30 """
31 # adding the variables to the model
32 self.varl = self.addVariable(0.1, 1.0e20, 0.5)
33 self.varinp = self.addVariable(0.1, 1.0e21, 0.5)
34 self.varout = self.addVariable(0.0, 1.0e22)
35 self.varp = self.addVariable(0.0, 1.0e23)
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_LARGEBND
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 @ingroup PYTHON1THREAD_LARGEBND
91 """
92 L = x[self.varl]
93 Inp = x[self.varinp]
94 Out = x[self.varout]
95 P = x[self.varp]
96
97 jac = []
98 if rowno == self.consobj:
99 jac.append(P)
100 jac.append(Out)
101 elif rowno == self.consprod:
102 hold1 = (
103 self.Al * pow(L, (-self.Rho))
104 + self.Ak * pow(self.K, (-self.Rho))
105 + self.Ainp * pow(Inp, (-self.Rho))
106 )
107 hold2 = pow(hold1, (-1.0 / self.Rho))
108 hold3 = hold2 / hold1
109
110 jac.append(hold3 * self.Al * pow(L, (-self.Rho - 1.0)))
111 jac.append(hold3 * self.Ainp * pow(Inp, (-self.Rho - 1.0)))
112
113 return jac
114
115
116if __name__ == '__main__':
117 name = os.path.basename(__file__)[:-3]
118
119 conopt = co.Conopt(name)
120 model = TutModelData()
121 msghdlr = std.TutMessageHandler(name)
122
123 model.buildModel()
124
125 conopt.loadModel(model)
126 conopt.setMessageHandler(msghdlr)
127
128 # getting the license variables
129 license_int_1 = os.environ.get('CONOPT_LICENSE_INT_1', None)
130 license_int_2 = os.environ.get('CONOPT_LICENSE_INT_2', None)
131 license_int_3 = os.environ.get('CONOPT_LICENSE_INT_3', None)
132 license_text = os.environ.get('CONOPT_LICENSE_TEXT', None)
133 if (
134 license_int_1 is not None
135 and license_int_2 is not None
136 and license_int_3 is not None
137 and license_text is not None
138 ):
139 conopt.setLicense(
140 int(license_int_1),
141 int(license_int_2),
142 int(license_int_3),
143 license_text,
144 )
145
146 coi_error = conopt.solve()
147
148 retcode = std.checkSolve(conopt, 0.572943, coi_error)
149
150 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
buildModel(self)
adding the variables and constraints to the model
Definition largebnd.py:26
evaluateNonlinearTerm(self, x, rowno, ignerr, thread)
callback method for evaluating the nonlinear terms in a given row
Definition largebnd.py:62
evaluateNonlinearJacobian(self, x, rowno, jacnum, ignerr, thread)
callback method for evaluating the jacobian for the nonlinear terms in a given row
Definition largebnd.py:87