View Javadoc
1   /*
2    * Licensed to the Hipparchus project under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The Hipparchus project licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.hipparchus.optim.nonlinear.vector.constrained;
18  
19  import java.util.ArrayList;
20  import java.util.Collections;
21  
22  import org.hipparchus.linear.Array2DRowRealMatrix;
23  import org.hipparchus.linear.ArrayRealVector;
24  import org.hipparchus.linear.EigenDecompositionSymmetric;
25  import org.hipparchus.linear.MatrixUtils;
26  import org.hipparchus.linear.RealMatrix;
27  import org.hipparchus.linear.RealVector;
28  import org.hipparchus.optim.nonlinear.scalar.ObjectiveFunction;
29  import org.hipparchus.util.FastMath;
30  
31  /**
32   * Sequential Quadratic Programming Optimizer.
33   * <p>
34   * min f(x)
35   * <p>
36   * q(x)=b1
37   * <p>
38   * h(x)&gt;=b2
39   * <p>
40   * Algorithm based on paper:"On the convergence of a sequential quadratic
41   * programming method(Klaus Shittkowki,January 1982)"
42   * @since 3.1
43   * @deprecated as of 4.1, replaced by {@link SQPOptimizerS2}; will be removed in 5.0.
44   * Note that {@link SQPOptimizerS2} is not a drop-in replacement: its iteration limit is
45   * {@link SQPOption#setMaxIteration(int)}, it uses finite-difference gradients unless
46   * {@link SQPOption#setGradientMode(GradientMode)} is set to {@link GradientMode#EXTERNAL},
47   * and its convergence test differs.
48   */
49  @Deprecated
50  public class SQPOptimizerS extends AbstractSQPOptimizer {
51  
52      /** Forgetting factor. */
53      private static final int FORGETTING_FACTOR = 10;
54  
55      /** Jacobian constraint. */
56      private RealMatrix constraintJacob;
57  
58      /** Value of the equality constraint. */
59      private RealVector equalityEval;
60  
61      /** Value of the inequality constraint. */
62      private RealVector inequalityEval;
63  
64      /** Gradient of the objective function. */
65      private RealVector functionGradient;
66  
67      /** Hessian of the objective function. */
68      private RealMatrix hessian;
69  
70      /** Simple constructor.
71       */
72      public SQPOptimizerS() {
73          // nothing to do
74      }
75  
76      /** {@inheritDoc} */
77      @Override
78      public LagrangeSolution doOptimize() {
79          int me = 0;
80          int mi = 0;
81  
82          //EQUALITY CONSTRAINT
83          if (this.getEqConstraint() != null) {
84              me = getEqConstraint().dimY();
85          }
86          //INEQUALITY CONSTRAINT
87          if (this.getIqConstraint() != null) {
88              mi = getIqConstraint().dimY();
89          }
90  
91          double alpha;
92          double rho = 100.0;
93          int failedSearch = 0;
94          RealVector x;
95          if (this.getStartPoint() != null) {
96              x = new ArrayRealVector(this.getStartPoint());
97          } else {
98              x = new ArrayRealVector(this.getObj().dim());
99          }
100 
101         RealVector y = new ArrayRealVector(me + mi, 0.0);
102         RealVector r = new ArrayRealVector(me + mi, 1.0);
103         ArrayList<Double> oldPenalty = new ArrayList<>();
104         //INITIAL VALUES
105         double functionEval = this.getObj().value(x);
106         functionGradient = this.getObj().gradient(x);
107         double maxGrad = functionGradient.getLInfNorm();
108 
109 
110         if (this.getEqConstraint() != null) {
111 
112             equalityEval = this.getEqConstraint().value(x);
113         }
114         if (this.getIqConstraint() != null) {
115 
116             inequalityEval = this.getIqConstraint().value(x);
117         }
118         constraintJacob = computeJacobianConstraint(x);
119 
120         if (this.getEqConstraint() != null) {
121             maxGrad = FastMath.max(maxGrad, equalityEval.getLInfNorm());
122         }
123         if (this.getIqConstraint() != null) {
124             maxGrad = FastMath.max(maxGrad, inequalityEval.getLInfNorm());
125         }
126 
127         if (!getSettings().useFunHessian()) {
128             hessian = MatrixUtils.createRealIdentityMatrix(x.getDimension());
129         } else {
130             hessian = this.getObj().hessian(x);
131         }
132 
133         for (int i = 0; i < this.getMaxIterations(); i++) {
134             iterations.increment();
135 
136             alpha = 1.0;
137             LagrangeSolution sol1 = null;
138 
139             int qpLoop = 0;
140             double sigma = maxGrad;
141             double currentPenaltyGrad;
142 
143             //LOOP TO FIND SOLUTION WITH SIGMA<SIGMA THRESHOLD
144             while (sigma > getSettings().getSigmaMax() && qpLoop < getSettings().getQpMaxLoop()) {
145                 sol1 = solveQP(x, y, rho);
146                 sigma = sol1.getValue();
147 
148                 if (sigma > getSettings().getSigmaMax()) {
149                     rho = getSettings().getRhoCons() * rho;
150                     qpLoop += 1;
151 
152                 }
153 
154             }
155             //IF SIGMA>SIGMA THRESHOLD ASSIGN DIRECTION FROM PENALTY GRADIENT
156             final RealVector dx;
157             final RealVector dy;
158             if (qpLoop == getSettings().getQpMaxLoop()) {
159 
160                 dx = (MatrixUtils.inverse(hessian).operate(penaltyFunctionGradX(functionGradient, x, y, r))).mapMultiply(-1.0);
161                 dy = y.subtract(penaltyFunctionGradY(x, y, r));
162                 sigma = 0;
163 
164             } else {
165                 dx = sol1.getX();
166                 sigma = sol1.getValue();
167                 if (sigma < 0) {
168                     sigma = 0;
169                 }
170                 dy = sol1.getLambda();
171                 r = updateRj(hessian, y, dx, dy, r, sigma);
172             }
173 
174             currentPenaltyGrad = penaltyFunctionGrad(functionGradient, dx, dy, x, y, r);
175             int search = 0;
176 
177             rho = updateRho(dx, dy, hessian, constraintJacob, sigma);
178 
179             double currentPenalty = penaltyFunction(functionEval, 0, x, y, dx, dy.subtract(y), r);
180 
181             double alphaF = this.getObj().value(x.add(dx.mapMultiply(alpha)));
182             double alfaPenalty = penaltyFunction(alphaF, alpha, x, y, dx, dy.subtract(y), r);
183 
184 
185             //LINE SEARCH
186 
187             while ((alfaPenalty - currentPenalty) >= getSettings().getMu() * alpha * currentPenaltyGrad &&
188                     search < getSettings().getMaxLineSearchIteration()) {
189                 double alfaStar = -0.5 * alpha * alpha * currentPenaltyGrad / (-alpha * currentPenaltyGrad + alfaPenalty - currentPenalty);
190 
191 
192                 alpha = FastMath.max(getSettings().getB() * alpha, FastMath.min(1.0, alfaStar));
193                 alphaF = this.getObj().value(x.add(dx.mapMultiply(alpha)));
194                 alfaPenalty = penaltyFunction(alphaF, alpha, x, y, dx, dy.subtract(y), r);
195                 search = search + 1;
196 
197             }
198 
199 
200 
201             if (getSettings().getConvCriteria() == 0) {
202                 if (dx.mapMultiply(alpha).dotProduct(hessian.operate(dx.mapMultiply(alpha))) < getSettings().getEps() * getSettings().getEps()) {
203 //                    x = x.add(dx.mapMultiply(alfa));
204 //                    y = y.add((dy.subtract(y)).mapMultiply(alfa));
205                     break;
206                 }
207             } else {
208                 if (alpha * dx.getNorm() < getSettings().getEps() * (1 + x.getNorm())) {
209 //                    x = x.add(dx.mapMultiply(alfa));
210 //                    y = y.add((dy.subtract(y)).mapMultiply(alfa));
211                     break;
212                 }
213 
214             }
215 
216             if (search == getSettings().getMaxLineSearchIteration()) {
217                 failedSearch += 1;
218             }
219 
220             boolean notMonotone = false;
221             if (search == getSettings().getMaxLineSearchIteration()) {
222 
223                 alpha = 1.0;
224                 search = 0;
225                 Double max = Collections.max(oldPenalty);
226                 if (oldPenalty.size() == 1) {
227                     max = max * 1.3;
228                 }
229                 while ((alfaPenalty - max) >= getSettings().getMu() * alpha * currentPenaltyGrad &&
230                        search < getSettings().getMaxLineSearchIteration()) {
231 
232                     double alfaStar = -0.5 * alpha * alpha * currentPenaltyGrad / (-alpha * currentPenaltyGrad + alfaPenalty - currentPenalty);
233 
234 
235                     alpha = FastMath.max(getSettings().getB() * alpha, FastMath.min(1.0, alfaStar));
236                     // alfa = FastMath.min(1.0, FastMath.max(this.b * alfa, alfaStar));
237                     // alfa = FastMath.max(this.b * alfa, alfaStar);
238                     alphaF = this.getObj().value(x.add(dx.mapMultiply(alpha)));
239                     alfaPenalty = penaltyFunction(alphaF, alpha, x, y, dx, dy.subtract(y), r);
240                     search = search + 1;
241 
242                 }
243                 notMonotone = true;
244                 // if (search < maxLineSearchIteration) failedSearch = 0;
245             }
246 
247             //UPDATE ALL FUNCTION
248             RealVector oldGradient = functionGradient;
249             RealMatrix oldJacob = constraintJacob;
250             // RealVector old1 = penaltyFunctionGradX(oldGradient,x, y.add((dy.subtract(y)).mapMultiply(alfa)),r);
251             RealVector old1 = lagrangianGradX(oldGradient, oldJacob, x, y.add((dy.subtract(y)).mapMultiply(alpha)));
252             functionEval = alphaF;
253             functionGradient = this.getObj().gradient(x.add(dx.mapMultiply(alpha)));
254             if (this.getEqConstraint() != null) {
255 
256                 equalityEval = this.getEqConstraint().value(x.add(dx.mapMultiply(alpha)));
257             }
258             if (this.getIqConstraint() != null) {
259 
260                 inequalityEval = this.getIqConstraint().value(x.add(dx.mapMultiply(alpha)));
261             }
262 
263             constraintJacob = computeJacobianConstraint(x.add(dx.mapMultiply(alpha)));
264             // RealVector new1 = penaltyFunctionGradX(functionGradient,x.add(dx.mapMultiply(alfa)), y.add((dy.subtract(y)).mapMultiply(alfa)),r);
265             RealVector new1 = lagrangianGradX(functionGradient, constraintJacob, x.add(dx.mapMultiply(alpha)), y.add((dy.subtract(y)).mapMultiply(alpha)));
266 
267             if (failedSearch == 2) {
268                 hessian = MatrixUtils.createRealIdentityMatrix(x.getDimension());
269                 failedSearch = 0;
270             }
271 
272             if (!notMonotone) {
273 //                if (iterations.getCount()==1)
274 //                {
275 //                    RealVector yfirst = new1.subtract(old1);
276 //                    RealVector sfirst = dx.mapMultiply(alfa);
277 //                    double scaleFactor = yfirst.dotProduct(sfirst)/yfirst.dotProduct(yfirst);
278 //                    hessian = hessian.scalarMultiply(scaleFactor);
279 //                }
280                 hessian = BFGSFormula(hessian, dx, alpha, new1, old1);
281             }
282 
283             //STORE PENALTY IN QUEQUE TO PERFORM NOT MONOTONE LINE SEARCH IF NECESSARY
284             // oldPenalty.add(alfaPenalty);
285             oldPenalty.add(currentPenalty);
286             if (oldPenalty.size() > FORGETTING_FACTOR) {
287                 oldPenalty.remove(0);
288             }
289 
290             x = x.add(dx.mapMultiply(alpha));
291             y = y.add((dy.subtract(y)).mapMultiply(alpha));
292         }
293 
294         return new LagrangeSolution(x, y, functionEval);
295     }
296 
297     private double penaltyFunction(double currentF, double alfa, RealVector x, RealVector y, RealVector dx, RealVector uv, RealVector r) {
298         // the set of constraints is the same as the previous one but they must be evaluated with the increment
299 
300         int me = 0;
301         int mi;
302         double partial = currentF;
303         RealVector yalfa = y.add(uv.mapMultiply(alfa));
304         if (getEqConstraint() != null) {
305             me = getEqConstraint().dimY();
306             RealVector re = r.getSubVector(0, me);
307             RealVector ye = yalfa.getSubVector(0, me);
308             RealVector g = this.getEqConstraint().value(x.add(dx.mapMultiply(alfa))).subtract(getEqConstraint().getLowerBound());
309             RealVector g2 = g.ebeMultiply(g);
310             partial -= ye.dotProduct(g) - 0.5 * re.dotProduct(g2);
311         }
312 
313         if (getIqConstraint() != null) {
314             mi = getIqConstraint().dimY();
315             RealVector ri = r.getSubVector(me, mi);
316             RealVector yi = yalfa.getSubVector(me, mi);
317             RealVector yk = y.getSubVector(me, mi);
318             RealVector gk = this.inequalityEval.subtract(getIqConstraint().getLowerBound());
319             RealVector g = this.getIqConstraint().value(x.add(dx.mapMultiply(alfa))).subtract(getIqConstraint().getLowerBound());
320             RealVector mask = new ArrayRealVector(g.getDimension(), 1.0);
321 
322             for (int i = 0; i < gk.getDimension(); i++) {
323 
324                 if (gk.getEntry(i) > (yk.getEntry(i) / ri.getEntry(i))) {
325 
326                     mask.setEntry(i, 0.0);
327 
328                     partial -= 0.5 * yi.getEntry(i) * yi.getEntry(i) / ri.getEntry(i);
329                 }
330 
331             }
332 
333             RealVector g2 = g.ebeMultiply(g.ebeMultiply(mask));
334             partial -= yi.dotProduct(g.ebeMultiply(mask)) - 0.5 * ri.dotProduct(g2);
335 
336         }
337 
338         return partial;
339     }
340 
341     private double penaltyFunctionGrad(RealVector currentGrad, RealVector dx, RealVector dy, RealVector x, RealVector y, RealVector r) {
342 
343         int me = 0;
344         int mi;
345         double partial = currentGrad.dotProduct(dx);
346         if (getEqConstraint() != null) {
347             me = getEqConstraint().dimY();
348             RealVector re = r.getSubVector(0, me);
349             RealVector ye = y.getSubVector(0, me);
350             RealVector dye = dy.getSubVector(0, me);
351             RealVector ge = this.getEqConstraint().value(x).subtract(getEqConstraint().getLowerBound());
352             RealMatrix jacob = this.getEqConstraint().jacobian(x);
353             RealVector firstTerm = jacob.transpose().operate(ye);
354             RealVector secondTerm = jacob.transpose().operate(ge.ebeMultiply(re));
355 //partial -= firstTerm.dotProduct(dx) - secondTerm.dotProduct(dx) + g.dotProduct(dye);
356             partial += -firstTerm.dotProduct(dx) + secondTerm.dotProduct(dx) - ge.dotProduct(dye.subtract(ye));
357         }
358 
359         if (getIqConstraint() != null) {
360             mi = getIqConstraint().dimY();
361 
362             RealVector ri = r.getSubVector(me, mi);
363             RealVector dyi = dy.getSubVector(me, mi);
364             RealVector yi = y.getSubVector(me, mi);
365 
366             RealVector gi = this.getIqConstraint().value(x).subtract(getIqConstraint().getLowerBound());
367             RealMatrix jacob = this.getIqConstraint().jacobian(x);
368 
369             RealVector mask = new ArrayRealVector(mi, 1.0);
370             RealVector viri = new ArrayRealVector(mi, 0.0);
371 
372             for (int i = 0; i < gi.getDimension(); i++) {
373 
374                 if (gi.getEntry(i) > yi.getEntry(i) / ri.getEntry(i)) {
375                     mask.setEntry(i, 0.0);
376 
377                 } else {
378                     viri.setEntry(i, yi.getEntry(i) / ri.getEntry(i));
379                 }
380 
381             }
382             RealVector firstTerm = jacob.transpose().operate(yi.ebeMultiply(mask));
383             RealVector secondTerm = jacob.transpose().operate(gi.ebeMultiply(ri.ebeMultiply(mask)));
384 
385             partial -= firstTerm.dotProduct(dx) - secondTerm.dotProduct(dx) + (gi.ebeMultiply(mask)).dotProduct(dyi.subtract(yi)) + viri.dotProduct(dyi.subtract(yi));
386             // partial -= firstTerm.dotProduct(dx) - secondTerm.dotProduct(dx) + g.dotProduct(dyi.ebeMultiply(mask));
387         }
388 
389         return partial;
390     }
391 
392     private RealVector penaltyFunctionGradX(RealVector currentGrad, RealVector x, RealVector y, RealVector r) {
393 
394         int me = 0;
395         int mi;
396         RealVector partial = currentGrad.copy();
397         if (getEqConstraint() != null) {
398             me = getEqConstraint().dimY();
399             RealVector re = r.getSubVector(0, me);
400             RealVector ye = y.getSubVector(0, me);
401 
402             RealVector ge = this.getEqConstraint().value(x).subtract(getEqConstraint().getLowerBound());
403             RealMatrix jacob = this.getEqConstraint().jacobian(x);
404 
405             RealVector firstTerm = jacob.transpose().operate(ye);
406             RealVector secondTerm = jacob.transpose().operate(ge.ebeMultiply(re));
407 
408             partial = partial.subtract(firstTerm.subtract(secondTerm));
409         }
410 
411         if (getIqConstraint() != null) {
412             mi = getIqConstraint().dimY();
413 
414             RealVector ri = r.getSubVector(me, mi);
415 
416             RealVector yi = y.getSubVector(me, mi);
417             RealVector gi = this.getIqConstraint().value(x).subtract(getIqConstraint().getLowerBound());
418             RealMatrix jacob = this.getIqConstraint().jacobian(x);
419 
420             RealVector mask = new ArrayRealVector(mi, 1.0);
421 
422             for (int i = 0; i < gi.getDimension(); i++) {
423 
424                 if (gi.getEntry(i) > yi.getEntry(i) / ri.getEntry(i)) {
425                     mask.setEntry(i, 0.0);
426 
427                 }
428 
429             }
430             RealVector firstTerm = jacob.transpose().operate(yi.ebeMultiply(mask));
431             RealVector secondTerm = jacob.transpose().operate(gi.ebeMultiply(ri.ebeMultiply(mask)));
432             partial = partial.subtract(firstTerm.subtract(secondTerm));
433         }
434 
435         return partial;
436     }
437 
438     private RealVector penaltyFunctionGradY(RealVector x, RealVector y, RealVector r) {
439 
440         int me = 0;
441         int mi;
442         RealVector partial = new ArrayRealVector(y.getDimension());
443         if (getEqConstraint() != null) {
444             me = getEqConstraint().dimY();
445             RealVector g = this.getEqConstraint().value(x).subtract(getEqConstraint().getLowerBound());
446             partial.setSubVector(0, g.mapMultiply(-1.0));
447         }
448 
449         if (getIqConstraint() != null) {
450             mi = getIqConstraint().dimY();
451 
452             RealVector ri = r.getSubVector(me, mi);
453 
454             RealVector yi = y.getSubVector(me, mi);
455             RealVector gi = this.getIqConstraint().value(x).subtract(getIqConstraint().getLowerBound());
456 
457             RealVector mask = new ArrayRealVector(mi, 1.0);
458 
459             RealVector viri = new ArrayRealVector(mi, 0.0);
460 
461             for (int i = 0; i < gi.getDimension(); i++) {
462 
463                 if (gi.getEntry(i) > yi.getEntry(i) / ri.getEntry(i)) {
464                     mask.setEntry(i, 0.0);
465 
466                 } else {
467                     viri.setEntry(i, yi.getEntry(i) / ri.getEntry(i));
468                 }
469 
470             }
471 
472             partial.setSubVector(me, gi.ebeMultiply(mask).add(viri).mapMultiply(-1.0));
473         }
474 
475         return partial;
476     }
477 
478     private RealVector updateRj(RealMatrix H, RealVector y, RealVector dx, RealVector dy, RealVector r, double additional) { //r = updateRj(currentH,dx,y,u,r,sigm);
479         //CALCULATE SIGMA VECTOR THAT DEPEND FROM ITERATION
480         RealVector sigma = new ArrayRealVector(r.getDimension());
481         for (int i = 0; i < sigma.getDimension(); i++) {
482             final double appoggio = iterations.getCount() / FastMath.sqrt(r.getEntry(i));
483             sigma.setEntry(i, FastMath.min(1.0, appoggio));
484         }
485 
486         int me = 0;
487         int mi = 0;
488         if (getEqConstraint() != null) {
489             me = getEqConstraint().dimY();
490         }
491         if (getIqConstraint() != null) {
492             mi = getIqConstraint().dimY();
493         }
494 
495         RealVector sigmar = sigma.ebeMultiply(r);
496         //(u-v)^2 or (ru-v)
497         RealVector numerator = ((dy.subtract(y)).ebeMultiply(dy.subtract(y))).mapMultiply(2.0 * (mi + me));
498 
499         double denominator = dx.dotProduct(H.operate(dx)) * (1.0 - additional);
500         RealVector r1 = r.copy();
501         if (getEqConstraint() != null) {
502             for (int i = 0; i < me; i++) {
503                 r1.setEntry(i, FastMath.max(sigmar.getEntry(i), numerator.getEntry(i) / denominator));
504             }
505         }
506         if (getIqConstraint() != null) {
507             for (int i = 0; i < mi; i++) {
508                 r1.setEntry(me + i, FastMath.max(sigmar.getEntry(me + i), numerator.getEntry(me + i) / denominator));
509             }
510         }
511 
512 
513         return r1;
514     }
515 
516     private double updateRho(RealVector dx, RealVector dy, RealMatrix H, RealMatrix jacobianG, double additionalVariable) {
517 
518         double num = 10.0 * FastMath.pow(dx.dotProduct(jacobianG.transpose().operate(dy)), 2);
519         double den = (1.0 - additionalVariable) * (1.0 - additionalVariable) * dx.dotProduct(H.operate(dx));
520         //double den = (1.0 - additionalVariable) * dx.dotProduct(H.operate(dx));
521 
522         return FastMath.max(10.0, num / den);
523     }
524 
525     private LagrangeSolution solveQP(RealVector x, RealVector y, double rho) {
526 
527         RealMatrix H = hessian;
528         RealVector g = functionGradient;
529 
530         int me = 0;
531         int mi = 0;
532         int add = 0;
533         boolean violated = false;
534         if (getEqConstraint() != null) {
535             me = getEqConstraint().dimY();
536         }
537         if (getIqConstraint() != null) {
538 
539             mi = getIqConstraint().dimY();
540             violated = inequalityEval.subtract(getIqConstraint().getLowerBound()).getMinValue() <= getSettings().getEps() ||
541                        y.getMaxValue() >= 0;
542 
543         }
544         // violated = true;
545         if (me > 0 || violated) {
546             add = 1;
547 
548         }
549 
550         RealMatrix H1 = new Array2DRowRealMatrix(H.getRowDimension() + add, H.getRowDimension() + add);
551         H1.setSubMatrix(H.getData(), 0, 0);
552         if (add == 1) {
553             H1.setEntry(H.getRowDimension(), H.getRowDimension(), rho);
554         }
555 
556         RealVector g1 = new ArrayRealVector(g.getDimension() + add);
557         g1.setSubVector(0, g);
558 
559         LinearEqualityConstraint eqc = null;
560         RealVector conditioneq;
561         if (getEqConstraint() != null) {
562             RealMatrix eqJacob = constraintJacob.getSubMatrix(0, me - 1, 0, x.getDimension() - 1);
563 
564             RealMatrix Ae = new Array2DRowRealMatrix(me, x.getDimension() + add);
565             RealVector be = new ArrayRealVector(me);
566             Ae.setSubMatrix(eqJacob.getData(), 0, 0);
567             conditioneq = this.equalityEval.subtract(getEqConstraint().getLowerBound());
568             Ae.setColumnVector(x.getDimension(), conditioneq.mapMultiply(-1.0));
569 
570             be.setSubVector(0, getEqConstraint().getLowerBound().subtract(this.equalityEval));
571             eqc = new LinearEqualityConstraint(Ae, be);
572 
573         }
574         LinearInequalityConstraint iqc = null;
575 
576         if (getIqConstraint() != null) {
577 //
578             RealMatrix iqJacob = constraintJacob.getSubMatrix(me, me + mi - 1, 0, x.getDimension() - 1);
579 
580             RealMatrix Ai = new Array2DRowRealMatrix(mi, x.getDimension() + add);
581             RealVector bi = new ArrayRealVector(mi);
582             Ai.setSubMatrix(iqJacob.getData(), 0, 0);
583 
584             RealVector conditioniq = this.inequalityEval.subtract(getIqConstraint().getLowerBound());
585 
586             if (add == 1) {
587 
588                 for (int i = 0; i < conditioniq.getDimension(); i++) {
589 
590                     if (!(conditioniq.getEntry(i) <= getSettings().getEps() || y.getEntry(me + i) > 0)) {
591                         conditioniq.setEntry(i, 0);
592                     }
593                 }
594 
595                 Ai.setColumnVector(x.getDimension(), conditioniq.mapMultiply(-1.0));
596 
597             }
598             bi.setSubVector(0, getIqConstraint().getLowerBound().subtract(this.inequalityEval));
599             iqc = new LinearInequalityConstraint(Ai, bi);
600 
601         }
602         LinearBoundedConstraint bc = null;
603         if (add == 1) {
604 
605             RealMatrix sigmaA = new Array2DRowRealMatrix(1, x.getDimension() + 1);
606             sigmaA.setEntry(0, x.getDimension(), 1.0);
607             ArrayRealVector lb = new ArrayRealVector(1, 0.0);
608             ArrayRealVector ub = new ArrayRealVector(1, 1.0);
609 
610             bc = new LinearBoundedConstraint(sigmaA, lb, ub);
611 
612         }
613 
614         QuadraticFunction q = new QuadraticFunction(H1, g1, 0);
615 
616         ADMMQPOptimizer solver = new ADMMQPOptimizer();
617         LagrangeSolution sol = solver.optimize(new ObjectiveFunction(q), iqc, eqc, bc);
618 
619         final double sigma;
620         if (add == 1) {
621             sigma = sol.getX().getEntry(x.getDimension());
622         } else {
623             sigma = 0;
624         }
625 
626         return new LagrangeSolution(sol.getX().getSubVector(0, x.getDimension()),
627                                     sol.getLambda().getSubVector(0, me + mi),
628                                     sigma);
629 
630     }
631 
632     private RealMatrix computeJacobianConstraint(RealVector x) {
633         int me = 0;
634         int mi = 0;
635         RealMatrix je = null;
636         RealMatrix ji = null;
637         if (this.getEqConstraint() != null) {
638             me = this.getEqConstraint().dimY();
639             je = this.getEqConstraint().jacobian(x);
640         }
641 
642         if (this.getIqConstraint() != null) {
643             mi = this.getIqConstraint().dimY();
644             ji = this.getIqConstraint().jacobian(x);
645         }
646 
647         RealMatrix jacobian = new Array2DRowRealMatrix(me + mi, x.getDimension());
648         if (me > 0) {
649             jacobian.setSubMatrix(je.getData(), 0, 0);
650         }
651         if (mi > 0) {
652             jacobian.setSubMatrix(ji.getData(), me, 0);
653         }
654 
655         return jacobian;
656     }
657     /*
658      *DAMPED BFGS FORMULA
659      */
660 
661     private RealMatrix BFGSFormula(RealMatrix oldH, RealVector dx, double alfa, RealVector newGrad, RealVector oldGrad) {
662 
663         RealMatrix oldH1 = oldH.copy();
664         RealVector y1 = newGrad.subtract(oldGrad);
665         RealVector s = dx.mapMultiply(alfa);
666         double theta = 1.0;
667         if (s.dotProduct(y1) < 0.2 * s.dotProduct(oldH.operate(s))) {
668             theta = 0.8 * s.dotProduct(oldH.operate(s)) / (s.dotProduct(oldH.operate(s)) - s.dotProduct(y1));
669         }
670 
671         RealVector y = y1.mapMultiply(theta).add((oldH.operate(s)).mapMultiply(1.0 - theta));
672 
673         RealMatrix firstTerm = y.outerProduct(y).scalarMultiply(1.0 / s.dotProduct(y));
674         RealMatrix secondTerm = oldH1.multiply(s.outerProduct(s)).multiply(oldH);
675         double thirtTerm = s.dotProduct(oldH1.operate(s));
676         RealMatrix Hnew = oldH1.add(firstTerm).subtract(secondTerm.scalarMultiply(1.0 / thirtTerm));
677         //RESET HESSIAN IF NOT POSITIVE DEFINITE
678         EigenDecompositionSymmetric dsX = new EigenDecompositionSymmetric(Hnew);
679         double min = new ArrayRealVector(dsX.getEigenvalues()).getMinValue();
680         if (min < 0) {
681             Hnew = MatrixUtils.createRealIdentityMatrix(oldH.getRowDimension());
682         }
683         return Hnew;
684 
685     }
686 
687 }