Re: [ojAlgo-user] Problems with the quadratic solver
Mathematics, linear algebra and optimisation
Brought to you by:
apete
From: Anders P. <an...@op...> - 2010-02-21 20:49:39
|
Haven't looked into exactly what your problem is, but: 1) You define the objective function by setting the "contribution weight" on one or more expressions and/or variables. The method getObjectiveExpression() will give you a derived/aggregated expression. You most likely do not need to call this method. If you want to call it, you should do so when the optimization model is complete. 2) Lok in the MarkowitzModel class (even if that's not the model you're after). In that class there is a private method generateOptimisationModel(...). Study that code. /Anders On 21 feb 2010, at 17.16, Joao Magalhaes wrote: > Hello, > > My name is Joao Magalhaes, I am an undegraduate student from Brazil. > I have to solve an optimization problem that has to do with financial stuff. It can be summarized by the constraint matrix below: > > > a b c e1 e2 e3 Mean dm1 dm2 dm3 > eq1 0,5 2 3 -1 > > > > > > = 2 > eq2 1 -0,5 -2 > -1 > > > > > = 3 > eq3 -0,3 3 -0,8 > > -1 > > > > = 0 > eq4 > > 1 1 1 -3 > > > = 0 > eq5 > > 1 > > -1 -1 > > = 0 > eq6 > > > 1 > -1 > -1 > = 0 > eq7 > > > > 1 -1 > > -1 = 0 > eq8 1 1 1 = 1 > > > > > > > > > > > > > > objective function: dm1^2 + dm2^2 + dm3^2 > > > > > > > > > > > > minimize objective function > > > > > > > > > > > > > The goal is to minimize the function dm1^2 + dm2^2 + dm3^2, subject to the constraints in the matrix (each cell is the factor that multiplies the variable, when there is no factor, the multiplier is 0). > I searched for Quadratic Solvers in google, and came across ojAlgo. Based on the javadocs and some source code inspection, I have written the code and the end of this message. However, I came across two problems: > > 1) I cannot set quadratic factors for the objective function. I get a NullPointerException. Inspecting the source code, I concluded that the objective function must be linear, is this correct? If so, it would be also possible to model the problem a little bit differently, by adding a new quadratic constraint and minimizing the new created variable, just as follows: > > > Variance a b c e1 e2 e3 Mean dm1 dm2 dm3 > > eq1 0,5 2 3 -1 = 2 > eq2 1 -0,5 -2 > -1 > > > > > = 3 > eq3 -0,3 3 -0,8 > > -1 > > > > = 0 > eq4 > > > 1 1 1 -3 > > > = 0 > eq5 > > > 1 > > -1 -1 > > = 0 > eq6 > > > > 1 > -1 > -1 > = 0 > eq7 > > > > > 1 -1 > > -1 = 0 > eq8 1 1 1 > > > > > > > = 1 > eq9 -3 ^2 ^2 ^2 = 0 > > > > > > > > > > > > > > > Objective function: variance > > > > > > > > > > > > > minimize variance > > > > > > > > > > > > > > However, I tryied this too, but it did not work. > > 2) In order to keep moving, I used the first model (matrix 1) with 8 equations, but changed the objective function to something linear (just the sum of the dm variables), so that nothing in the problem is quadratic, everything is linear. But again, I get a NullPointerException in the Lagrange Solver: > > Exception in thread "main" java.lang.NullPointerException > at org.ojalgo.matrix.store.MergedRowsStore.<init>(MergedRowsStore.java:46) > at org.ojalgo.optimisation.quadratic.LagrangeSolver.buildIterationSolver(LagrangeSolver.java:56) > at org.ojalgo.optimisation.quadratic.LagrangeSolver.performIteration(LagrangeSolver.java:84) > at org.ojalgo.optimisation.quadratic.QuadraticSolver.iterate(QuadraticSolver.java:303) > at org.ojalgo.optimisation.quadratic.QuadraticSolver.solve(QuadraticSolver.java:275) > at org.ojalgo.optimisation.quadratic.QuadraticSolver.solve(QuadraticSolver.java:256) > at br.com.mindsatwork.ojalgo.test.TestClass.main(TestClass.java:143) > > > My source code is as follow. Can anyone give me some light on what I am doing wrong? Please, apologies if this is a very basic question, but it is the very first time I try ojAlgo. I promise to improve my questions in the future :-) > > Cheers, > Joao Magalhaes > > -- ---------------------- > > import java.math.BigDecimal; > import java.util.HashSet; > import java.util.Set; > > import org.ojalgo.optimisation.Expression; > import org.ojalgo.optimisation.ModelValidationException; > import org.ojalgo.optimisation.OptimisationSolver; > import org.ojalgo.optimisation.Variable; > import org.ojalgo.optimisation.OptimisationSolver.Result; > import org.ojalgo.optimisation.quadratic.QuadraticExpressionsModel; > > > public class Test > { > public static void main( final String[] args ) > throws ModelValidationException > { > final Set<Variable> variables = new HashSet<Variable>(); > final Variable A = new Variable( "A" ); > final Variable B = new Variable( "B" ); > final Variable C = new Variable( "C" ); > final Variable e1 = new Variable( "e1" ); > final Variable e2 = new Variable( "e2" ); > final Variable e3 = new Variable( "e3" ); > final Variable mean = new Variable( "mean" ); > final Variable dm1 = new Variable( "dm1" ); > final Variable dm2 = new Variable( "dm2" ); > final Variable dm3 = new Variable( "dm3" ); > variables.add( A ); > variables.add( B ); > variables.add( C ); > variables.add( e1 ); > variables.add( e2 ); > variables.add( e3 ); > variables.add( mean ); > variables.add( dm1 ); > variables.add( dm2 ); > variables.add( dm3 ); > > final QuadraticExpressionsModel model = new QuadraticExpressionsModel( variables ); > > model.addConstraint( "eq1", new BigDecimal( 0 ), new BigDecimal( 0 ) ); > model.addConstraint( "eq2", new BigDecimal( 0 ), new BigDecimal( 0 ) ); > model.addConstraint( "eq3", new BigDecimal( 0 ), new BigDecimal( 0 ) ); > model.addConstraint( "eq4", new BigDecimal( 0 ), new BigDecimal( 0 ) ); > model.addConstraint( "eq5", new BigDecimal( 0 ), new BigDecimal( 0 ) ); > model.addConstraint( "eq6", new BigDecimal( 0 ), new BigDecimal( 0 ) ); > model.addConstraint( "eq7", new BigDecimal( 0 ), new BigDecimal( 0 ) ); > model.addConstraint( "eq8", new BigDecimal( 0 ), new BigDecimal( 0 ) ); > > model.getExpression( "eq1" ).setLinearFactor( model.indexOfVariable( "A" ), new BigDecimal( 0.5 ) ); > model.getExpression( "eq1" ).setLinearFactor( model.indexOfVariable( "B" ), new BigDecimal( 2 ) ); > model.getExpression( "eq1" ).setLinearFactor( model..indexOfVariable( "C" ), new BigDecimal( 3 ) ); > model.getExpression( "eq1" ).setLinearFactor( model.indexOfVariable( "e1" ), new BigDecimal( -1 ) ); > model.getExpression( "eq1" ).setConstant( new BigDecimal( -2 ) ); > > model.getExpression( "eq2" ).setLinearFactor( model.indexOfVariable( "A" ), new BigDecimal( 1 ) ); > model.getExpression( "eq2" ).setLinearFactor( model..indexOfVariable( "B" ), new BigDecimal( -0.5 ) ); > model.getExpression( "eq2" ).setLinearFactor( model.indexOfVariable( "C" ), new BigDecimal( -2 ) ); > model.getExpression( "eq2" ).setLinearFactor( model..indexOfVariable( "e2" ), new BigDecimal( -1 ) ); > model.getExpression( "eq2" ).setConstant( new BigDecimal( -3 ) ); > > model.getExpression( "eq3" ).setLinearFactor( model.indexOfVariable( "A" ), new BigDecimal( -0.3 ) ); > model.getExpression( "eq3" ).setLinearFactor( model.indexOfVariable( "B" ), new BigDecimal( 3 ) ); > model.getExpression( "eq3" ).setLinearFactor( model..indexOfVariable( "C" ), new BigDecimal( -0.8 ) ); > model.getExpression( "eq3" ).setLinearFactor( model.indexOfVariable( "e3" ), new BigDecimal( -1 ) ); > model.getExpression( "eq3" ).setConstant( new BigDecimal( 0 ) ); > > model.getExpression( "eq4" ).setLinearFactor( model.indexOfVariable( "e1" ), new BigDecimal( 1 ) ); > model.getExpression( "eq4" ).setLinearFactor( model..indexOfVariable( "e2" ), new BigDecimal( 1 ) ); > model.getExpression( "eq4" ).setLinearFactor( model.indexOfVariable( "e3" ), new BigDecimal( 1 ) ); > model.getExpression( "eq4" ).setLinearFactor( model..indexOfVariable( "mean" ), new BigDecimal( -3 ) ); > model.getExpression( "eq4" ).setConstant( new BigDecimal( 0 ) ); > > model.getExpression( "eq5" ).setLinearFactor( model.indexOfVariable( "e1" ), new BigDecimal( 1 ) ); > model.getExpression( "eq5" ).setLinearFactor( model.indexOfVariable( "mean" ), new BigDecimal( -1 ) ); > model.getExpression( "eq5" ).setLinearFactor( model.indexOfVariable( "dm1" ), new BigDecimal( -1 ) ); > model.getExpression( "eq5" ).setConstant( new BigDecimal( 0 ) ); > > model.getExpression( "eq6" ).setLinearFactor( model.indexOfVariable( "e2" ), new BigDecimal( 1 ) ); > model.getExpression( "eq6" ).setLinearFactor( model.indexOfVariable( "mean" ), new BigDecimal( -1 ) ); > model.getExpression( "eq6" ).setLinearFactor( model.indexOfVariable( "dm2" ), new BigDecimal( -1 ) ); > model.getExpression( "eq6" ).setConstant( new BigDecimal( 0 ) ); > > model.getExpression( "eq7" ).setLinearFactor( model.indexOfVariable( "e3" ), new BigDecimal( 1 ) ); > model.getExpression( "eq7" ).setLinearFactor( model.indexOfVariable( "mean" ), new BigDecimal( -1 ) ); > model.getExpression( "eq7" ).setLinearFactor( model.indexOfVariable( "dm3" ), new BigDecimal( -1 ) ); > model.getExpression( "eq7" ).setConstant( new BigDecimal( 0 ) ); > > model.getExpression( "eq8" ).setLinearFactor( model.indexOfVariable( "A" ), new BigDecimal( 1 ) ); > model.getExpression( "eq8" ).setLinearFactor( model.indexOfVariable( "B" ), new BigDecimal( 1 ) ); > model.getExpression( "eq8" ).setLinearFactor( model.indexOfVariable( "C" ), new BigDecimal( 1 ) ); > model.getExpression( "eq8" ).setConstant( new BigDecimal( -1 ) ); > > final Expression obj = model.getObjectiveExpression(); > > obj.setLinearFactor( model.indexOfVariable( "dm1" ), new BigDecimal( 1 ) ); > obj.setLinearFactor( model.indexOfVariable( "dm2" ), new BigDecimal( 1 ) ); > obj.setLinearFactor( model.indexOfVariable( "dm3" ), new BigDecimal( 1 ) ); > > // obj.setQuadraticFactor( model.indexOfVariable( "dm1" ), model.indexOfVariable( "dm1" ), > // new BigDecimal( 1 ) ); > // obj..setQuadraticFactor( model.indexOfVariable( "dm2" ), model.indexOfVariable( "dm2" ), > // new BigDecimal( 1 ) ); > // obj.setQuadraticFactor( model.indexOfVariable( "dm3" ), model.indexOfVariable( "dm3" ), > // new BigDecimal( 1 ) ); > > model.setMaximisation( false ); > model.setMinimisation( true ); > > final OptimisationSolver solver = model.getDefaultSolver(); > > final Result res = solver.solve(); > > System.out.println( res.getState().toString() ); > System.out.println( res.toString() ); > } > > } > > > > ------------------------------------------------------------------------------ > Download Intel® Parallel Studio Eval > Try the new software tools for yourself. Speed compiling, find bugs > proactively, and fine-tune applications for parallel performance. > See why Intel Parallel Studio got high marks during beta. > http://p.sf.net/sfu/intel-sw-dev_______________________________________________ > ojAlgo-user mailing list > ojA...@li... > https://lists.sourceforge.net/lists/listinfo/ojalgo-user |