Here it is; I didn't provide the fuction ir_svn(&pa, &pb, &pc, &pct, &ps, &px) - but the output - below - shows what input to QP was provided... ------------------------------------------------------------------------------------------- // AlgLib.cpp : Defines the entry point for the console application. //
#include "stdafx.h" #include <windows.h> #include "src\optimization.h"
extern int ir_svn(double **pa, double **pb, double **pc, int **pct, double **ps, double **px); extern void TestSolution(double *x, double *pa, double *pb);
using namespace alglib;
int _tmain(int argc, _TCHAR* argv[]) { try { // // This example demonstrates minimization of F(x0,x1) = x0^2 + x1^2 -6*x0 - 4*x1 // subject to linear constraint x0+x1<=2 // // Exact solution is [x0,x1] = [1.5,0.5] // // IMPORTANT: this solver minimizes following function: // f(x) = 0.5*x'*A*x + b'*x. // Note that quadratic term has 0.5 before it. So if you want to minimize // quadratic function, you should rewrite it in such way that quadratic term // is multiplied by 0.5 too. // For example, our function is f(x)=x0^2+x1^2+..., but we rewrite it as // f(x) = 0.5*(2*x0^2+2*x1^2) + .... // and pass diag(2,2) as quadratic term - NOT diag(1,1)! // double *pa, *pb, *pc, *ps, *px; int *pct;
int N = ir_svn(&pa, &pb, &pc, &pct, &ps, &px);
real_2d_array a; a.setcontent(N, N, pa);
real_1d_array b; b.setcontent(N, pb);
real_1d_array s; s.setcontent(N, ps);
real_2d_array c; c.setcontent(N + 1, N + 1, pc);
integer_1d_array ct; ct.setcontent(N + 1, pct);
real_1d_array x; x.setcontent(N, px);
if(N <= 4) { for(int i=0; i<N; i++) { for(int j=0; j<N; j++) { printf("\ta[%u][%u] = %f", i, j, a[i][j]); } printf("\tb[%u] = %f\n", i, b[i]); } for(int i=0; i<N+1; i++) { for(int j=0; j<N+1; j++) { printf("\tc[%u][%u] = %f", i, j, c[i][j]); } printf("\tct[%u] = %u\n", i, ct[i]); } }
minqpstate state; minqpreport rep;
// create solver, set quadratic/linear terms minqpcreate(2, state); minqpsetquadraticterm(state, a); minqpsetlinearterm(state, b); minqpsetlc(state, c, ct);
// Set scale of the parameters. // It is strongly recommended that you set scale of your variables. // Knowing their scales is essential for evaluation of stopping criteria // and for preconditioning of the algorithm steps. // You can find more information on scaling at http://www.alglib.net/optimization/scaling.php // // NOTE: for convex problems you may try using minqpsetscaleautodiag() // which automatically determines variable scales. minqpsetscale(state, s);
// // Solve problem with BLEIC-based QP solver. // // This solver is intended for problems with moderate (up to 50) number // of general linear constraints and unlimited number of box constraints. // // Default stopping criteria are used. //
minqpsetalgobleic(state, 0.0, 0.0, 0.0, 0); //minqpsetalgodenseaul(state, 1.0e-9, 1.0e+4, 5);
minqpoptimize(state); minqpresults(state, x, rep); printf("inneriterationscount %u\n" "ncholesky %u, nmv %u\n" "outeriterationscount %u\n" "terminationtype %d\n" , rep.inneriterationscount, rep.ncholesky, rep.nmv, rep.outeriterationscount, rep.terminationtype); if(rep.terminationtype >= 0 && rep.terminationtype <= 4) { for(int i=0; i<N; i++) printf("x[%u] = %f\n", i, x[i]); }
getchar(); } catch(ap_error) { printf("Exception!\n"); getchar(); } return 0; }
Output: -----------------------------------------------------------------------
a[0][0] = 17.000000 a[0][1] = 19.000000 a[0][2] = -11.000000 a[0][3] = -13.000000 b[0] = -1.000000 a[1][0] = 19.000000 a[1][1] = 22.000000 a[1][2] = -13.000000 a[1][3] = -16.000000 b[1] = -1.000000 a[2][0] = -11.000000 a[2][1] = -13.000000 a[2][2] = 9.000000 a[2][3] = 11.000000 b[2] = -1.000000 a[3][0] = -13.000000 a[3][1] = -16.000000 a[3][2] = 11.000000 a[3][3] = 14.000000 b[3] = -1.000000 c[0][0] = 1.000000 c[0][1] = 0.000000 c[0][2] = 0.000000 c[0][3] = 0.000000 c[0][4] = 0.000000 ct[0] = 1 c[1][0] = 0.000000 c[1][1] = 1.000000 c[1][2] = 0.000000 c[1][3] = 0.000000 c[1][4] = 0.000000 ct[1] = 1 c[2][0] = 0.000000 c[2][1] = 0.000000 c[2][2] = 1.000000 c[2][3] = 0.000000 c[2][4] = 0.000000 ct[2] = 1 c[3][0] = 0.000000 c[3][1] = 0.000000 c[3][2] = 0.000000 c[3][3] = 1.000000 c[3][4] = 0.000000 ct[3] = 1 c[4][0] = 1.000000 c[4][1] = 1.000000 c[4][2] = -1.000000 c[4][3] = -1.000000 c[4][4] = 0.000000 ct[4] = 0 x[0] = 0.750000 x[1] = 0.750000 x[2] = 0.750000 x[3] = 0.750000 inneriterationscount 0 ncholesky 0, nmv 0 outeriterationscount 0 terminationtype -3
|