CONOPT
Loading...
Searching...
No Matches
qp1.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.NN = 1000
19 self.NQ = self.NN*2 - 1
20 # only the lower triangle of the Q matrix is stored, since it is
21 # symmetric. The diagonal has the value 10. While the first off diagonal
22 # has the value 0.1. Note, the indices for the lower diagonal is (i + 1,
23 # i)
24 self.target = [10]*self.NN
25 self.Qdiag = [1]*self.NN
26 self.Qlowerdiag = [0.1]*(self.NN - 1)
27
28 super().__init__()
29
30 def buildModel(self):
31 """
32 adding the variables and constraints to the model
33 @ingroup PYTHON1THREAD_QP1
34 """
35 # adding the variables to the model
36 for i in range(self.NN):
37 self.addVariable(0.0, pyconopt.CONOPT_INF)
38
39 # adding the constraints to the model
40 # the first constraint is the quadratic objective
41 varidx = list(range(self.NN))
42 zeros = [0]*self.NN
43 ones = [1]*self.NN
44 self.addConstraint(pyconopt.ConstraintType_Free, 0.0, varidx, zeros,
45 ones)
46
47 # the second constraint is the summation constraint: sum(x) = 1
48 self.addConstraint(pyconopt.ConstraintType_Eq, 1.0, varidx, ones,
49 zeros)
50
51 # setting the objective constraint
52 self.setObjectiveElement(pyconopt.ObjectiveElement_Constraint, 0)
53
54 # setting the optimisation direction
55 self.setOptimizationSense(pyconopt.Sense_Minimize)
56
57
58 def evaluateNonlinearTerm(self, x, rowno, ignerr, thread):
59 """
60 @ingroup PYTHON1THREAD_QP1
61 """
62 # only the objective function is nonlinear, so this function will only be
63 # called when rowno == 0
64 g = 0
65 if rowno == 0:
66 g += sum([(x[i] - self.target[i])*q*(x[i] - self.target[i])
67 for i, q in enumerate(self.Qdiag)])
68 g += 2*sum([(x[i + 1] - self.target[i + 1])*q*(x[i] - self.target[i])
69 for i, q in enumerate(self.Qlowerdiag)])
70
71 return g/2
72
73 def evaluateNonlinearJacobian(self, x, rowno, jacnum, ignerr, thread):
74 """
75 @ingroup PYTHON1THREAD_QP1
76 """
77 jac = [0]*self.NN
78 if rowno == 0:
79 for i in range(self.NN):
80 jac[i] += self.Qdiag[i]*(x[i] - self.target[i])
81 if i < self.NN - 1:
82 jac[i + 1] += self.Qlowerdiag[i]*(x[i] - self.target[i])
83 jac[i] += self.Qlowerdiag[i]*(x[i + 1] - self.target[i + 1])
84
85
86 return jac
87
88if __name__ == "__main__":
89 name = os.path.basename(__file__)[:-3]
90
91 conopt = pyconopt.Conopt(name)
92 model = QPModelData()
93 msghdlr = std.TutMessageHandler(name)
94
95 model.buildModel()
96
97 conopt.loadModel(model)
98 conopt.setMessageHandler(msghdlr)
99
100 # getting the license variables
101 license_int_1 = os.environ.get('LICENSE_INT_1', None)
102 license_int_2 = os.environ.get('LICENSE_INT_2', None)
103 license_int_3 = os.environ.get('LICENSE_INT_3', None)
104 license_text = os.environ.get('LICENSE_TEXT', None)
105 if license_int_1 is not None and license_int_2 is not None \
106 and license_int_3 is not None and license_text is not None:
107 conopt.setLicense(int(license_int_1), int(license_int_2),
108 int(license_int_3), license_text)
109
110 coi_error = conopt.solve()
111
112 retcode = std.checkSolve(conopt, 59978.0, coi_error, 0.001)
113
114 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
list target
Definition qp1.py:24
list Qdiag
Definition qp1.py:25
list Qlowerdiag
Definition qp1.py:26
__init__(self)
Definition qp1.py:17
static int checkSolve(String name, int model_status, int solution_status, double objective, double expected_objective, double tol)
Definition std.java:16
evaluateNonlinearJacobian(self, x, rowno, jacnum, ignerr, thread)
callback method for evaluating the jacobian for the nonlinear terms in a given row
Definition qp1.py:73
evaluateNonlinearTerm(self, x, rowno, ignerr, thread)
callback method for evaluating the nonlinear terms in a given row
Definition qp1.py:58
buildModel(self)
adding the variables and constraints to the model
Definition qp1.py:30