CONOPT
Loading...
Searching...
No Matches
tutoriali.py
Go to the documentation of this file.
7
8import os
9import sys
10
11import pyconopt
12
13sys.path.append('../common/')
14import std
15
17 def __init__(self):
18 self.L = 0
19 self.Inp = 0
20 self.Out = 0
21 self.P = 0
22 self.Al = 0.16
23 self.Ak = 2.0
24 self.Ainp = 0.16
25 self.Rho = 1.0
26 self.K = 4.0
27
28 # declaring place holder variables used for the evaluation of the first
29 # derivative
30 self.hold = [0, 0, 0]
31
32 super().__init__()
33
34 def buildModel(self):
35 """
36 adding the variables and constraints to the model
37 @ingroup PYTHON1THREAD_TUTORIALI
38 """
39 # adding the variables to the model
40 self.addVariable(0.1, pyconopt.CONOPT_INF, 0.5)
41 self.addVariable(0.1, pyconopt.CONOPT_INF, 0.5)
42 self.addVariable(0.0, pyconopt.CONOPT_INF)
43 self.addVariable(0.0, pyconopt.CONOPT_INF)
44
45 # adding the constraints to the model
46 self.addConstraint(pyconopt.ConstraintType_Free, -0.1, [0, 1, 2, 3],
47 [-1, -1, 0, 0], [0, 0, 1, 1])
48 self.addConstraint(pyconopt.ConstraintType_Eq, 0.0, [0, 1, 2], [0, 0, -1],
49 [1, 1, 0])
50 self.addConstraint(pyconopt.ConstraintType_Eq, 4.0, [2, 3], [1, 2],
51 [0, 0])
52
53 # setting the objective constraint
54 self.setObjectiveElement(pyconopt.ObjectiveElement_Constraint, 0)
55
56 # setting the optimisation direction
57 self.setOptimizationSense(pyconopt.Sense_Maximize)
58
59
60 def initFDEvaluation(self, x, rowlist, mode, numthread, ignerr):
61 """
62 @ingroup PYTHON1THREAD_TUTORIALI
63 """
64 self.L = x[0]
65 self.Inp = x[1]
66 self.Out = x[2]
67 self.P = x[3]
68
69 self.hold1 = (self.Al*pow(self.L, (-self.Rho)) + \
70 self.Ak*pow(self.K, (-self.Rho)) + \
71 self.Ainp*pow(self.Inp, (-self.Rho)))
72 self.hold2 = pow(self.hold1,( -1./self.Rho ))
73 self.hold3 = self.hold2 / self.hold1
74
75
76 def evaluateNonlinearTerm(self, x, rowno, ignerr, thread):
77 """
78 @ingroup PYTHON1THREAD_TUTORIALI
79 """
80 g = 0
81 if rowno == 0:
82 g = self.P * self.Out
83 elif rowno == 1:
84 g = self.hold2
85
86 return g
87
88 def evaluateNonlinearJacobian(self, x, rowno, jacnum, ignerr, thread):
89 """
90 @ingroup PYTHON1THREAD_TUTORIALI
91 """
92 jac = []
93 if rowno == 0:
94 jac.append(self.P)
95 jac.append(self.Out)
96 elif rowno == 1:
97 jac.append(self.hold3 * self.Al * pow(self.L ,(-self.Rho-1.)))
98 jac.append(self.hold3 * self.Ainp * pow(self.Inp,(-self.Rho-1.)))
99
100 return jac
101
102if __name__ == "__main__":
103 name = os.path.basename(__file__)[:-3]
104
105 conopt = pyconopt.Conopt(name)
106 model = TutModelData()
107 msghdlr = std.TutMessageHandler(name)
108
109 model.buildModel()
110
111 conopt.loadModel(model)
112 conopt.setMessageHandler(msghdlr)
113
114 # getting the license variables
115 license_int_1 = os.environ.get('LICENSE_INT_1', None)
116 license_int_2 = os.environ.get('LICENSE_INT_2', None)
117 license_int_3 = os.environ.get('LICENSE_INT_3', None)
118 license_text = os.environ.get('LICENSE_TEXT', None)
119 if license_int_1 is not None and license_int_2 is not None \
120 and license_int_3 is not None and license_text is not None:
121 conopt.setLicense(int(license_int_1), int(license_int_2),
122 int(license_int_3), license_text)
123
124 coi_error = conopt.solve()
125
126 retcode = std.checkSolve(conopt, 0.572943, coi_error)
127
128 sys.exit(retcode)
setOptimizationSense(self, sense)
sets the optimisation direction.
Definition pyconopt.py:2119
setObjectiveElement(self, elem, elemindex)
sets the index for the objective variable or constraint
Definition pyconopt.py:2111
addVariable(self, *args)
Overload 1: adds a variable to the model.
Definition pyconopt.py:2052
addConstraint(self, *args)
Overload 1: adds a constraint to the problem.
Definition pyconopt.py:2011
The Conopt class.
Definition pyconopt.py:1416
A class that can be extended to build and solve a model using Conopt.
Definition pyconopt.py:2391
static int checkSolve(String name, int model_status, int solution_status, double objective, double expected_objective, double tol)
Definition std.java:16
initFDEvaluation(self, x, rowlist, mode, numthread, ignerr)
callback method for initialising the first derivative evaluation.
Definition tutoriali.py:60
evaluateNonlinearTerm(self, x, rowno, ignerr, thread)
callback method for evaluating the nonlinear terms in a given row
Definition tutoriali.py:76
buildModel(self)
adding the variables and constraints to the model
Definition tutoriali.py:34
evaluateNonlinearJacobian(self, x, rowno, jacnum, ignerr, thread)
callback method for evaluating the jacobian for the nonlinear terms in a given row
Definition tutoriali.py:88