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