#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "optimization.h"
#include <fstream>

using namespace alglib;
using namespace std;

int main(int argc, char **argv)
{

	unsigned int dim = 120;
	unsigned int noc = 117;

	real_2d_array a;
	double *p_a = new double[dim * dim];
	for (unsigned int i = 0; i < dim; i++){
		for (unsigned int j = 0; j < dim; j++){
			p_a[i*dim + j] = 0.0F;
		}
	}
	double var;
	ifstream fileName_1("Matrix_A.txt");
	for (unsigned int i = 0; i < dim; i++){
		for (unsigned int j = 0; j < dim; j++){
			fileName_1 >> var;
			p_a[i*dim + j] = var;
		}
	}
	fileName_1.close();
	a.setcontent(dim, dim, p_a);

	real_1d_array b;
	double p_b[120] = { 0 };
	b.setcontent(dim, p_b);

	real_1d_array x0;
	double p_x0[120] = { 0.3 };
	x0.setcontent(dim, p_x0);

	real_1d_array s = "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,100,100,100]";
	double var_1;
	real_2d_array c;
	double *p_c = new double[(noc + 1)*(dim + 1)];
	ifstream fileName_2("Matrix_Constrints.txt");
	for (unsigned int i = 0; i < noc+1; i++){
		for (unsigned int j = 0; j < dim+1; j++){
			fileName_2 >> var_1;
			p_c[i*(dim+1) + j] = var_1;
		}
	}

	fileName_2.close();
	c.setcontent(noc + 1, dim + 1, p_c);

	integer_1d_array ct = "[-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0]";

	real_1d_array x;
	minqpstate state;
	minqpreport rep;


	minqpcreate(120, state);
	minqpsetquadraticterm(state, a);
	minqpsetlinearterm(state, b);
	minqpsetlc(state, c, ct);
	minqpsetscale(state, s);
	minqpsetstartingpoint(state,x0);

	// Solve problem with DENSE-AUL solver.
	//
	// This solver is optimized for problems with up to several thousands of
	// variables and large amount of general linear constraints. Problems with
	// less than 50 general linear constraints can be efficiently solved with
	// BLEIC, problems with box-only constraints can be solved with QuickQP.
	// However, DENSE-AUL will work in any (including unconstrained) case..0
	//
	// Default stopping criteria are used.
	//
	minqpsetalgodenseaul(state, 0, 1.0e+4, 5);
	minqpoptimize(state);
	minqpresults(state, x, rep);
	printf("%s\n", x.tostring(6).c_str()); // EXPECTED: [1.500,0.500]

	return 0;
}