15 public static void main(String argv[]) {
16 System.loadLibrary(
"conoptjni4");
21 qp4_TutModelData
model =
new qp4_TutModelData();
31 int license_int_1 = Integer.parseInt(System.getenv(
"CONOPT_LICENSE_INT_1"));
32 int license_int_2 = Integer.parseInt(System.getenv(
"CONOPT_LICENSE_INT_2"));
33 int license_int_3 = Integer.parseInt(System.getenv(
"CONOPT_LICENSE_INT_3"));
34 String
license_text = System.getenv(
"CONOPT_LICENSE_TEXT");
38 }
catch (Exception e) {
39 System.out.println(
"Unable to set license: " + e.getMessage());
48 conopt.objectiveValue(), 59978.0, 0.001);
59 private double[] target;
60 private double[] Qdiag;
61 private double[] Qlowerdiag;
63 public qp4_TutModelData() {
69 private void setConstants() {
72 target =
new double[NN];
73 Qdiag =
new double[NN];
74 Qlowerdiag =
new double[NN];
75 for (
int i = 0; i < NN; i++) {
80 for (
int i = 0; i < NN - 1; i++)
90 for (
int i = 0; i < NN; i++)
95 int[] varindex =
new int[NN];
96 int[] zeros =
new int[NN];
97 int[] ones =
new int[NN];
98 for (
int i = 0; i < NN; i++) {
106 Arrays.stream(zeros).asDoubleStream().toArray(), ones);
110 Arrays.stream(ones).asDoubleStream().toArray(), zeros);
122 setLagrangianStructure();
125 private void setLagrangianStructure() {
126 int[] hessianrow =
new int[
NQ];
127 int[] hessiancol =
new int[
NQ];
130 for (
int i = 0; i <
NN - 1; i++) {
132 hessianrow[2*i + 1] = i + 1;
135 hessiancol[2*i + 1] = i;
137 hessianrow[NQ - 1] = NN - 1;
138 hessiancol[NQ - 1] = NN - 1;
151 for (
int i = 0; i < NN; i++)
152 g += (x[i] - target[i])*Qdiag[i]*(x[i] - target[i]);
153 for (
int i = 0; i < NN - 1; i++)
154 g += 2*(x[i + 1] - target[i + 1])*Qlowerdiag[i]*(x[i] - target[i]);
165 assert x.length == jac.length;
168 for (
int i = 0; i < NN; i++)
169 jac[i] += Qdiag[i]*(x[i] - target[i]);
171 for (
int i = 0; i < NN - 1; i++) {
172 jac[i + 1] += Qlowerdiag[i]*(x[i] - target[i]);
173 jac[i] += Qlowerdiag[i]*(x[i + 1] - target[i + 1]);
184 assert x.length == d2g.length;
185 assert dx.length == d2g.length;
187 for (
int i = 0; i < NN; i++)
188 d2g[i] = Qdiag[i]*dx[i];
190 for (
int i = 0; i < NN - 1; i++) {
191 d2g[i + 1] += Qlowerdiag[i]*dx[i];
192 d2g[i] += Qlowerdiag[i]*dx[i + 1];
201 public void evaluateSDLagrangian(
double[] x,
double[] u,
int[] hessianrow,
int[] hessiancol,
double[] hessianval) {
202 assert hessianrow.length == hessianval.length;
203 assert hessiancol.length == hessianval.length;
205 for (
int i = 0; i < NN - 1; i++) {
206 hessianval[2*i] = Qdiag[i]*u[0];
207 hessianval[2*i + 1] = Qlowerdiag[i]*u[0];
209 hessianval[NQ - 1] = Qdiag[NN - 1]*u[0];
static final ConstraintType Eq
static final ConstraintType Free
A class that can be extended to build and solve a model using Conopt.
static final ObjectiveElement Constraint
static final SDEvaluationType Constraint
static final Sense Minimize
static void main(String argv[])
static int checkSolve(String name, int model_status, int solution_status, double objective, double expected_objective, double tol)
addConstraint(self, *args)
Overload 1: adds a constraint to the problem.
setObjectiveElement(self, elem, elemindex)
sets the index for the objective variable or constraint
addVariable(self, *args)
Overload 1: adds a variable to the model.
setOptimizationSense(self, sense)
sets the optimisation direction.
setSDEvaluationType(self, sdevaltype)
informs CONOPT of the method for evaluating the second derivative
setSDLagrangianStructure(self, rownum, colnum)
sets the structure of the second derivatives of the Lagrangian
void evaluateSDLagrangian(double[] x, double[] u, int[] hessianrow, int[] hessiancol, double[] hessianval)
Computes and returns the numerical values of the Lagrangian of the Hessian.
void evaluateNonlinearJacobian(double[] x, double[] jac, int rowno, int[] jacnum, boolean ignerr, int thread)
callback method for evaluating the jacobian for the nonlinear terms in a given row
void evaluateDirectionalSD(double[] x, double[] dx, double[] d2g, int rowno, int[] jacnum, int thread)
computes the directional second derivative for a single constraint
void buildModel()
adds variables and constraints to the model
double evaluateNonlinearTerm(double[] x, int rowno, boolean ignerr, int thread)
callback method for evaluating the nonlinear terms in a given row