Re: [ojAlgo-user] QuadraticSolver issue
Mathematics, linear algebra and optimisation
Brought to you by:
apete
From: Simon G. <si...@ya...> - 2011-06-14 16:02:09
|
package temporaryapp; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author simon */ import ms2Utils.FileUtils; import org.ojalgo.optimisation.quadratic.*; import org.ojalgo.matrix.BasicMatrix; import org.ojalgo.optimisation.OptimisationSolver.Result; import org.ojalgo.optimisation.*; import org.ojalgo.matrix.store.*; public class TempClass { private double var, ret, fxd_ret; private double[] C; private double[][] Q; private String[] var_names; public static void TempClass() { } public static void main(String[] args) { double[][] cov_mat = strng2Dbl(FileUtils.fileToArray("/home/simon/Documents/Frontera Eficiente de Markowitz/COVAR_MATRIX.txt", "\t")); double[] r = strng2Dbl(FileUtils.fileToList("/home/simon/Documents/Frontera Eficiente de Markowitz/RETURN.txt")); String[] vars = FileUtils.fileToList("/home/simon/Documents/Frontera Eficiente de Markowitz/VARS.txt"); TempClass FE = new TempClass(); FE.setQ(cov_mat); FE.setC(r); FE.setVar_names(vars); // FE.minimizeVarMinusReturn(); FE.minimizeVarGivenReturn(0.3); } private void minimizeVarMinusReturn() { /* CREACION DE VARIABLES COMPATIBLES CON LA LIBRERIA OjAlgo */ double[][] c_temp = new double[Q[0].length][1]; for (int i = 0; i < c_temp.length; i++) { c_temp[i][0] = C[i]; } double[] ae = new double[Q.length]; double[][] ai = new double[Q[0].length][Q.length]; double[][] be = new double[1][1]; be[0][0] = 1; double[][] bi = new double[Q[0].length][1]; for (int i = 0; i < Q.length; i++) { ae[i] = 1; bi[i][0] = 1; } for (int i = 0; i < ai.length; i++) { for (int j = 0; j < ai[0].length; j++) { if (i == j) { ai[i][j] = 1; } else { ai[i][j] = 0; } } } final PhysicalStore<Double> Q_aux = PrimitiveDenseStore.FACTORY.copy(Q); final PhysicalStore<Double> C_aux = PrimitiveDenseStore.FACTORY.copy(c_temp); final PhysicalStore<Double> aux_AE = PrimitiveDenseStore.FACTORY.makeColumn(ae); final PhysicalStore<Double> aux_BE = PrimitiveDenseStore.FACTORY.copy(be); final PhysicalStore<Double> aux_AI = PrimitiveDenseStore.FACTORY.copy(ai); final PhysicalStore<Double> aux_BI = PrimitiveDenseStore.FACTORY.copy(bi); MatrixStore Q_ms = Q_aux.copy(); MatrixStore C_ms = C_aux.copy(); MatrixStore AE_ms = aux_AE.copy().transpose(); MatrixStore BE_ms = aux_BE.copy(); MatrixStore AI_ms = aux_AI.copy().transpose(); MatrixStore BI_ms = aux_BI.copy(); Variable[] VARIABLE = new Variable[var_names.length]; for (int i = 0; i < var_names.length; i++) { VARIABLE[i] = new Variable(var_names[i]); System.out.println(VARIABLE[i].toString()); } /* FIN CREACION DE VARIABLES COMPATIBLES CON LA LIBRERIA OjAlgo */ /* INICIALIZACION DE OBJETO QUADRATIC SOLVER */ QuadraticExpressionsModel qem = new QuadraticExpressionsModel(VARIABLE); QuadraticSolver.Builder qdrtc_slvr_bldr = new QuadraticSolver.Builder(qem).objective(Q_ms, C_ms).equalities(AE_ms, BE_ms).inequalities(AI_ms, BI_ms); QuadraticSolver qdrtc_slvr = qdrtc_slvr_bldr.build(); /* FIN INICIALIZACION DE OBJETO QUADRATIC SOLVER */ /* RESULTADOS */ Result rslt = qdrtc_slvr.solve(); BasicMatrix rslt_mtrx = rslt.getSolution(); PhysicalStore<Double> result = rslt_mtrx.toPrimitiveStore(); var = result.transpose().multiplyRight(Q_ms).multiplyRight(result).get(0, 0); ret = C_aux.transpose().multiplyRight(result).get(0, 0); System.out.println("\nRESULT"); for (int i = 0; i < rslt_mtrx.getRowDim(); i++) { for (int j = 0; j < rslt_mtrx.getColDim(); j++) { System.out.println(rslt_mtrx.doubleValue(i, j) + " "); } System.out.println(); } System.out.println("\nVARIANCE " + var); System.out.println("\nRETORNO " + ret); /* FIN RESULTADOS */ } private void minimizeVarGivenReturn(double given_return) { setFxd_ret(given_return); /* CREACION DE VARIABLES COMPATIBLES CON LA LIBRERIA OjAlgo * Aqui haremos que los retornos entren en las restricciones. Esto significa * que en vez de introducir los retornos en la matriz C, esta sera zeros * y los retornos entraran en AE. De esta manera se obtienen retornos * iguales a fxd_ret. */ double[][] c_temp = new double[Q[0].length][1]; for (int i = 0; i < c_temp.length; i++) { c_temp[i][0] = 0; } double[][] ae = new double[2][Q.length]; double[][] ai = new double[Q[0].length][Q.length]; double[][] be = new double[2][1]; be[0][0] = 1; be[1][0] = fxd_ret; double[][] bi = new double[Q[0].length][1]; for (int i = 0; i < Q.length; i++) { ae[0][i] = 1; ae[1][i] = C[i]; bi[i][0] = 1; } for (int i = 0; i < ai.length; i++) { for (int j = 0; j < ai[0].length; j++) { if (i == j) { ai[i][j] = 1; } else { ai[i][j] = 0; } } } final PhysicalStore<Double> Q_aux = PrimitiveDenseStore.FACTORY.copy(Q); final PhysicalStore<Double> C_aux = PrimitiveDenseStore.FACTORY.copy(c_temp); final PhysicalStore<Double> aux_AE = PrimitiveDenseStore.FACTORY.copy(ae); final PhysicalStore<Double> aux_BE = PrimitiveDenseStore.FACTORY.copy(be); final PhysicalStore<Double> aux_AI = PrimitiveDenseStore.FACTORY.copy(ai); final PhysicalStore<Double> aux_BI = PrimitiveDenseStore.FACTORY.copy(bi); MatrixStore Q_ms = Q_aux.copy(); MatrixStore C_ms = C_aux.copy(); MatrixStore AE_ms = aux_AE.copy(); MatrixStore BE_ms = aux_BE.copy(); MatrixStore AI_ms = aux_AI.copy().transpose(); MatrixStore BI_ms = aux_BI.copy(); Variable[] VARIABLE = new Variable[var_names.length]; for (int i = 0; i < var_names.length; i++) { VARIABLE[i] = new Variable(var_names[i]); System.out.println(VARIABLE[i].toString()); } /* FIN CREACION DE VARIABLES COMPATIBLES CON LA LIBRERIA OjAlgo */ /* INICIALIZACION DE OBJETO QUADRATIC SOLVER */ QuadraticExpressionsModel qem = new QuadraticExpressionsModel(VARIABLE); QuadraticSolver.Builder qdrtc_slvr_bldr = new QuadraticSolver.Builder(qem).objective(Q_ms, C_ms).equalities(AE_ms, BE_ms).inequalities(AI_ms, BI_ms); QuadraticSolver qdrtc_slvr = qdrtc_slvr_bldr.build(); /* FIN INICIALIZACION DE OBJETO QUADRATIC SOLVER */ /* RESULTADOS */ Result rslt = qdrtc_slvr.solve(); BasicMatrix rslt_mtrx = rslt.getSolution(); PhysicalStore<Double> result = rslt_mtrx.toPrimitiveStore(); var = result.transpose().multiplyRight(Q_ms).multiplyRight(result).get(0, 0); System.out.println("\nRESULT"); for (int i = 0; i < rslt_mtrx.getRowDim(); i++) { for (int j = 0; j < rslt_mtrx.getColDim(); j++) { System.out.println(rslt_mtrx.doubleValue(i, j) + " "); } System.out.println(); } System.out.println("\nVARIANCE " + var); /* FIN RESULTADOS */ } private static double[][] strng2Dbl(String[][] strng_mat) { double[][] mat = new double[strng_mat.length][strng_mat[0].length]; for (int i = 0; i < strng_mat.length; i++) { for (int j = 0; j < strng_mat[0].length; j++) { mat[i][j] = Double.parseDouble(strng_mat[i][j]); } } return mat; } private static double[] strng2Dbl(String[] strng_vec) { double[] vec = new double[strng_vec.length]; for (int i = 0; i < strng_vec.length; i++) { vec[i] = Double.parseDouble(strng_vec[i]); } return vec; } /** * @param R the R to set */ public void setC(double[] C) { this.C = C; } /** * @param W the W to set */ public void setQ(double[][] Q) { this.Q = Q; } /** * @return the var */ public double getVar() { return var; } /** * @return the ret */ public double getRet() { return ret; } /** * @param var_names the var_names to set */ public void setVar_names(String[] var_names) { this.var_names = var_names; } /** * @param fxd_ret the fxd_ret to set */ public void setFxd_ret(double fxd_ret) { this.fxd_ret = fxd_ret; } } |