ojalgo-user Mailing List for oj! Algorithms (Page 20)
Mathematics, linear algebra and optimisation
Brought to you by:
apete
You can subscribe to this list here.
2003 |
Jan
|
Feb
|
Mar
|
Apr
|
May
|
Jun
(1) |
Jul
(1) |
Aug
|
Sep
|
Oct
|
Nov
|
Dec
|
---|---|---|---|---|---|---|---|---|---|---|---|---|
2004 |
Jan
|
Feb
(1) |
Mar
|
Apr
(14) |
May
(1) |
Jun
(1) |
Jul
|
Aug
|
Sep
|
Oct
(1) |
Nov
|
Dec
(1) |
2005 |
Jan
(3) |
Feb
|
Mar
|
Apr
|
May
|
Jun
(1) |
Jul
|
Aug
(1) |
Sep
(3) |
Oct
|
Nov
|
Dec
(1) |
2006 |
Jan
(1) |
Feb
(1) |
Mar
(1) |
Apr
|
May
(1) |
Jun
(2) |
Jul
|
Aug
(5) |
Sep
|
Oct
(3) |
Nov
(4) |
Dec
(2) |
2007 |
Jan
(8) |
Feb
(1) |
Mar
(2) |
Apr
(11) |
May
(6) |
Jun
|
Jul
(10) |
Aug
(2) |
Sep
|
Oct
(4) |
Nov
|
Dec
(11) |
2008 |
Jan
(22) |
Feb
(4) |
Mar
(2) |
Apr
(4) |
May
(2) |
Jun
|
Jul
|
Aug
(1) |
Sep
|
Oct
(6) |
Nov
(17) |
Dec
(5) |
2009 |
Jan
(15) |
Feb
(2) |
Mar
(2) |
Apr
(3) |
May
(4) |
Jun
(5) |
Jul
(9) |
Aug
(2) |
Sep
|
Oct
(5) |
Nov
(14) |
Dec
|
2010 |
Jan
(3) |
Feb
(5) |
Mar
(2) |
Apr
(19) |
May
|
Jun
|
Jul
|
Aug
(2) |
Sep
|
Oct
(5) |
Nov
(3) |
Dec
|
2011 |
Jan
|
Feb
(1) |
Mar
(7) |
Apr
(10) |
May
(1) |
Jun
(15) |
Jul
(1) |
Aug
|
Sep
|
Oct
(13) |
Nov
(9) |
Dec
(11) |
2012 |
Jan
|
Feb
(1) |
Mar
(9) |
Apr
(23) |
May
(16) |
Jun
(9) |
Jul
(13) |
Aug
|
Sep
(4) |
Oct
(1) |
Nov
(1) |
Dec
|
2013 |
Jan
|
Feb
(12) |
Mar
(7) |
Apr
(24) |
May
|
Jun
(7) |
Jul
(4) |
Aug
|
Sep
(3) |
Oct
(16) |
Nov
(7) |
Dec
(3) |
2014 |
Jan
(22) |
Feb
(1) |
Mar
(1) |
Apr
|
May
(2) |
Jun
(3) |
Jul
(4) |
Aug
(4) |
Sep
(1) |
Oct
(2) |
Nov
(6) |
Dec
(2) |
2015 |
Jan
(11) |
Feb
(10) |
Mar
(1) |
Apr
(9) |
May
|
Jun
|
Jul
(1) |
Aug
(4) |
Sep
(6) |
Oct
(3) |
Nov
(15) |
Dec
(4) |
2016 |
Jan
(6) |
Feb
|
Mar
(18) |
Apr
(5) |
May
(9) |
Jun
(3) |
Jul
(5) |
Aug
(2) |
Sep
(5) |
Oct
(2) |
Nov
|
Dec
(5) |
2017 |
Jan
(4) |
Feb
(3) |
Mar
|
Apr
|
May
(12) |
Jun
(1) |
Jul
(4) |
Aug
|
Sep
(3) |
Oct
(1) |
Nov
|
Dec
(1) |
2018 |
Jan
(5) |
Feb
|
Mar
(1) |
Apr
(1) |
May
(1) |
Jun
|
Jul
(4) |
Aug
(1) |
Sep
(8) |
Oct
|
Nov
|
Dec
(1) |
2019 |
Jan
|
Feb
(2) |
Mar
(1) |
Apr
|
May
|
Jun
|
Jul
|
Aug
|
Sep
|
Oct
|
Nov
|
Dec
|
2022 |
Jan
|
Feb
|
Mar
|
Apr
|
May
(1) |
Jun
|
Jul
|
Aug
|
Sep
|
Oct
|
Nov
|
Dec
|
From: Anders P. <an...@op...> - 2010-04-05 11:23:30
|
The class to start with is LinearExpressionsModel. http://ojalgo.org/generated/org/ojalgo/optimisation/linear/LinearExpressionsModel.html Also look in the mailing list archive to see if someone asked similar questions before. /Anders On 5 apr 2010, at 00.58, Leigh Anderson wrote: > Hello, > > I am looking for a solver that will solve a mixed integer programming problem. I am not a computer programmer and was wondering if someone had some example java code I could look at. Ojalgo seems like it will be very useful but I do not know how to start writing correct code. Hopefully someone can give me an example to work off of. > > Cheers, > > Leigh > > ----------------------------------------------- > Leigh Anderson > M.Sc Candidate Agricultural Economics > University of Saskatchewan > Office: (306) 966-4043 > Mobile (306) 241-9250 > lei...@us... > > ------------------------------------------------------------------------------ > 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 |
From: Leigh A. <lei...@us...> - 2010-04-04 22:58:32
|
Hello, I am looking for a solver that will solve a mixed integer programming problem. I am not a computer programmer and was wondering if someone had some example java code I could look at. Ojalgo seems like it will be very useful but I do not know how to start writing correct code. Hopefully someone can give me an example to work off of. Cheers, Leigh ----------------------------------------------- Leigh Anderson M.Sc Candidate Agricultural Economics University of Saskatchewan Office: (306) 966-4043 Mobile (306) 241-9250 lei...@us... |
From: Anders P. <an...@op...> - 2010-03-03 21:57:02
|
Try using http://ojalgo.org/generated/org/ojalgo/optimisation/linear/LinearExpressionsModel.html The MIP solver is experimental, but there has been success reports. /Anders On 3 mar 2010, at 17.19, Luke Lindsay wrote: > I am looking to solve the following type of problem.. > > There is a seller who has different quantities of number of types of > good A, B,C,D... A number of buyers make the seller offers, e.g "$190 > for 2 units of good A and 1 unit of good B" and the seller has to > decide which offers to accept in order to maximize revenue. > > Is ojalgo suited to solving this type of problem and if so can you > give me any pointers on how to get started or point me towards > anything I should read. > > Any help greatly appreciated, > > Luke > > ------------------------------------------------------------------------------ > 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 > > |
From: Luke L. <luk...@gm...> - 2010-03-03 16:19:09
|
I am looking to solve the following type of problem.. There is a seller who has different quantities of number of types of good A, B,C,D... A number of buyers make the seller offers, e.g "$190 for 2 units of good A and 1 unit of good B" and the seller has to decide which offers to accept in order to maximize revenue. Is ojalgo suited to solving this type of problem and if so can you give me any pointers on how to get started or point me towards anything I should read. Any help greatly appreciated, Luke |
From: Anders P. <an...@op...> - 2010-02-22 19:57:30
|
Use one of these methods: addEmptyLinearExpression(String) // Linear only addEmptyQuadraticExpression(String) // Quadratic only addGeneralExpression(String) // Linear and Quadratic Don't call getExpression(?) all the time - keep the reference instead. Turn those expressions into constraints and/or objectives by setting the lower/upper limits or the contribution weight. /Anders On 22 feb 2010, at 01.44, Joao Magalhaes wrote: > Anders, > > Thank you for your help. I removed the code about the objective > function, and after studying MarkowitzModel.generateOptimisationModel > method, I concluded I should set lower limits for variables that have > lower limits. It also looked like the method addSimpleWeightExpression > is used to define the objective function, however, when I tried this, > I got a NullPointerException because my objective function has > quadratic terms. So, I used the method addEmptyQuadraticExpression, > and defined a big "contribution weight" (BigMath.VERY_POSITIVE) by > calling method setContributionWeight. I think that, by doing this, I > defined this quadratic equation as my objective function, is this > correct? > The objective function is dm1^2 + dm2^2 + dm3^2, so, my final code > for the expression looks like this: > > model.addEmptyQuadraticExpression( "eq9" ); > model.getExpression( "eq9" ).setQuadraticFactor( > model.indexOfVariable( "dm1" ), > model.indexOfVariable( "dm1" ), > BigMath.ONE ); > model.getExpression( "eq9" ).setQuadraticFactor( > model.indexOfVariable( "dm2" ), > model.indexOfVariable( "dm2" ), > BigMath.ONE ); > model.getExpression( "eq9" ).setQuadraticFactor( > model.indexOfVariable( "dm3" ), > model.indexOfVariable( "dm3" ), > BigMath.ONE ); > model.getExpression( "eq9" ).setContributionWeight( BigMath.VERY_POSITIVE ); > > The model I created has all these variables (the complete code is > at the end of this message) > > However, I am not quite sure about how to set the constraints of my > problem. They are the following: > > eq1: 0.5*a + 2*b + 3*c - e1 = 2 > eq2: a - 0.5*b - 2*c - e2 = 3 > eq3: -0.3*a + 3*c - e3 = 0 > eq4: e1 + e2 + e3 - 3*mean = 0 > eq5: e1 - mean - dm1 = 0 > eq6: e2 - mean - dm2 = 0 > eq7: e3 - mean - dm3 = 0 > eq8: a + b + c = 1 > > By reading the javadocs, I understood that the method I should use > is addConstraint, which receives the lower and the upper limits. So, > in order to get an equality, I just have to make upperLimit = > lowerLimit. So, my code looked like this (notice that I used > upperLimit = lowerLimit = 0, and setConstant to define the equality): > > model.addConstraint( "eq1", BigMath.ZERO, BigMath.ZERO ); > model.addConstraint( "eq2", BigMath.ZERO, BigMath.ZERO ); > model.addConstraint( "eq3", BigMath.ZERO, BigMath.ZERO ); > model.addConstraint( "eq4", BigMath.ZERO, BigMath.ZERO ); > model.addConstraint( "eq5", BigMath.ZERO, BigMath.ZERO ); > model.addConstraint( "eq6", BigMath.ZERO, BigMath.ZERO ); > model.addConstraint( "eq7", BigMath.ZERO, BigMath.ZERO ); > model.addConstraint( "eq8", BigMath.ZERO, BigMath.ZERO ); > > model.getExpression( "eq1" ).setLinearFactor( model.indexOfVariable( > "A" ), BigMath.HALF ); > 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" ), BigMath.ONE ); > 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( BigMath.ZERO ); > > model.getExpression( "eq4" ).setLinearFactor( model.indexOfVariable( > "e1" ), BigMath.ONE ); > model.getExpression( "eq4" ).setLinearFactor( model.indexOfVariable( > "e2" ), BigMath.ONE ); > model.getExpression( "eq4" ).setLinearFactor( model.indexOfVariable( > "e3" ), BigMath.ONE ); > model.getExpression( "eq4" ).setLinearFactor( model.indexOfVariable( > "mean" ), new BigDecimal( -3 ) ); > model.getExpression( "eq4" ).setConstant( BigMath.ZERO ); > > model.getExpression( "eq5" ).setLinearFactor( model.indexOfVariable( > "e1" ), BigMath.ONE ); > model.getExpression( "eq5" ).setLinearFactor( model.indexOfVariable( > "mean" ), new BigDecimal( -1 ) ); > model.getExpression( "eq5" ).setLinearFactor( model.indexOfVariable( > "dm1" ), new BigDecimal( -1 ) ); > model.getExpression( "eq5" ).setConstant( BigMath.ZERO ); > > model.getExpression( "eq6" ).setLinearFactor( model.indexOfVariable( > "e2" ), BigMath.ONE ); > model.getExpression( "eq6" ).setLinearFactor( model.indexOfVariable( > "mean" ), new BigDecimal( -1 ) ); > model.getExpression( "eq6" ).setLinearFactor( model.indexOfVariable( > "dm2" ), new BigDecimal( -1 ) ); > model.getExpression( "eq6" ).setConstant( BigMath.ZERO ); > > model.getExpression( "eq7" ).setLinearFactor( model.indexOfVariable( > "e3" ), BigMath.ONE ); > model.getExpression( "eq7" ).setLinearFactor( model.indexOfVariable( > "mean" ), new BigDecimal( -1 ) ); > model.getExpression( "eq7" ).setLinearFactor( model.indexOfVariable( > "dm3" ), new BigDecimal( -1 ) ); > model.getExpression( "eq7" ).setConstant( BigMath.ZERO ); > > model.getExpression( "eq8" ).setLinearFactor( model.indexOfVariable( > "A" ), BigMath.ONE ); > model.getExpression( "eq8" ).setLinearFactor( model.indexOfVariable( > "B" ), BigMath.ONE ); > model.getExpression( "eq8" ).setLinearFactor( model.indexOfVariable( > "C" ), BigMath.ONE ); > model.getExpression( "eq8" ).setConstant( new BigDecimal( -1 ) ); > > So, I defined minimisation (by calling setMinimisation), generated > a solver (by calling getDefaultSolver), and invoked the method solve, > as in the following code: > > model.setMaximisation( false ); > model.setMinimisation( true ); > > final OptimisationSolver solver = model.getDefaultSolver(); > > final Result res = solver.solve(); > > This time, I did not get an exception, but the following result: > > OPTIMAL (1) { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 } > > But this is not what I expected. > Please, can you tell me what I am doing wrong? > > Cheers > Joao Magalhaes > > --- -------- Code: > > import java.math.BigDecimal; > import java.util.HashSet; > import java.util.Set; > > import org.ojalgo.constant.BigMath; > 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 ); > > A.setLowerLimit( BigMath.ZERO ); > B.setLowerLimit( BigMath.ZERO ); > C.setLowerLimit( BigMath.ZERO ); > e1.setLowerLimit( BigMath.ZERO ); > e2.setLowerLimit( BigMath.ZERO ); > e3.setLowerLimit( BigMath.ZERO ); > mean.setLowerLimit( BigMath.ZERO ); > dm1.setLowerLimit( BigMath.VERY_NEGATIVE ); > dm2.setLowerLimit( BigMath.VERY_NEGATIVE ); > dm3.setLowerLimit( BigMath.VERY_NEGATIVE ); > > A.setUpperLimit( BigMath.VERY_POSITIVE ); > B.setUpperLimit( BigMath.VERY_POSITIVE ); > C.setUpperLimit( BigMath.VERY_POSITIVE ); > e1.setUpperLimit( BigMath.VERY_POSITIVE ); > e2.setUpperLimit( BigMath.VERY_POSITIVE ); > e3.setUpperLimit( BigMath.VERY_POSITIVE ); > mean.setUpperLimit( BigMath.VERY_POSITIVE ); > dm1.setUpperLimit( BigMath.VERY_POSITIVE ); > dm2.setUpperLimit( BigMath.VERY_POSITIVE ); > dm3.setUpperLimit( BigMath.VERY_POSITIVE ); > > final QuadraticExpressionsModel model = new > QuadraticExpressionsModel( variables ); > > model.addConstraint( "eq1", BigMath.ZERO, BigMath.ZERO ); > model.addConstraint( "eq2", BigMath.ZERO, BigMath.ZERO ); > model.addConstraint( "eq3", BigMath.ZERO, BigMath.ZERO ); > model.addConstraint( "eq4", BigMath.ZERO, BigMath.ZERO ); > model.addConstraint( "eq5", BigMath.ZERO, BigMath.ZERO ); > model.addConstraint( "eq6", BigMath.ZERO, BigMath.ZERO ); > model.addConstraint( "eq7", BigMath.ZERO, BigMath.ZERO ); > model.addConstraint( "eq8", BigMath.ZERO, BigMath.ZERO ); > model.addEmptyQuadraticExpression( "eq9" ); > > model.getExpression( "eq1" ).setLinearFactor( model.indexOfVariable( > "A" ), BigMath.HALF ); > 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" ), BigMath.ONE ); > 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( BigMath.ZERO ); > > model.getExpression( "eq4" ).setLinearFactor( model.indexOfVariable( > "e1" ), BigMath.ONE ); > model.getExpression( "eq4" ).setLinearFactor( model.indexOfVariable( > "e2" ), BigMath.ONE ); > model.getExpression( "eq4" ).setLinearFactor( model.indexOfVariable( > "e3" ), BigMath.ONE ); > model.getExpression( "eq4" ).setLinearFactor( model.indexOfVariable( > "mean" ), new BigDecimal( -3 ) ); > model.getExpression( "eq4" ).setConstant( BigMath.ZERO ); > > model.getExpression( "eq5" ).setLinearFactor( model.indexOfVariable( > "e1" ), BigMath.ONE ); > model.getExpression( "eq5" ).setLinearFactor( model.indexOfVariable( > "mean" ), new BigDecimal( -1 ) ); > model.getExpression( "eq5" ).setLinearFactor( model.indexOfVariable( > "dm1" ), new BigDecimal( -1 ) ); > model.getExpression( "eq5" ).setConstant( BigMath.ZERO ); > > model.getExpression( "eq6" ).setLinearFactor( model.indexOfVariable( > "e2" ), BigMath.ONE ); > model.getExpression( "eq6" ).setLinearFactor( model.indexOfVariable( > "mean" ), new BigDecimal( -1 ) ); > model.getExpression( "eq6" ).setLinearFactor( model.indexOfVariable( > "dm2" ), new BigDecimal( -1 ) ); > model.getExpression( "eq6" ).setConstant( BigMath.ZERO ); > > model.getExpression( "eq7" ).setLinearFactor( model.indexOfVariable( > "e3" ), BigMath.ONE ); > model.getExpression( "eq7" ).setLinearFactor( model.indexOfVariable( > "mean" ), new BigDecimal( -1 ) ); > model.getExpression( "eq7" ).setLinearFactor( model.indexOfVariable( > "dm3" ), new BigDecimal( -1 ) ); > model.getExpression( "eq7" ).setConstant( BigMath.ZERO ); > > model.getExpression( "eq8" ).setLinearFactor( model.indexOfVariable( > "A" ), BigMath.ONE ); > model.getExpression( "eq8" ).setLinearFactor( model.indexOfVariable( > "B" ), BigMath.ONE ); > model.getExpression( "eq8" ).setLinearFactor( model.indexOfVariable( > "C" ), BigMath.ONE ); > model.getExpression( "eq8" ).setConstant( new BigDecimal( -1 ) ); > > model.getExpression( "eq9" ).setQuadraticFactor( > model.indexOfVariable( "dm1" ), > model.indexOfVariable( "dm1" ), > BigMath.ONE ); > model.getExpression( "eq9" ).setQuadraticFactor( > model.indexOfVariable( "dm2" ), > model.indexOfVariable( "dm2" ), > BigMath.ONE ); > model.getExpression( "eq9" ).setQuadraticFactor( > model.indexOfVariable( "dm3" ), > model.indexOfVariable( "dm3" ), > BigMath.ONE ); > > model.getExpression( "eq9" ).setContributionWeight( BigMath.VERY_POSITIVE ); > > 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() ); > } > } > > >> 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 exssions and/or variables. The method getObjectiveExssion() will give >> you a derived/aggregated exssion. 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 Sun, Feb 21, 2010 at 6:18 PM, Joao Magalhaes <ma...@gm...> wrote: >> Hello again, >> >> Looks like the matrix got messed up in my previous post. So, I am >> sending the message again, but with the equations instead of the >> matrix. >> Apologies for this. >> 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 equations below: >> >> Constraints: >> >> eq1: 0.5*a + 2*b + 3*c - e1 = 2 >> eq2: a - 0.5*b - 2*c - e2 = 3 >> eq3: -0.3*a + 3*c - e3 = 0 >> eq4: e1 + e2 + e3 - 3*mean = 0 >> eq5: e1 - mean - dm1 = 0 >> eq6: e2 - mean - dm2 = 0 >> eq7: e3 - mean - dm3 = 0 >> eq8: a + b + c = 1 >> >> Objective: minimize dm1^2 + dm2^2 + dm3^2 >> >> The goal is to minimize the function dm1^2 + dm2^2 + dm3^2, subject to >> the constraints. >> 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: >> >> eq1: 0.5*a + 2*b + 3*c - e1 = 2 >> eq2: a - 0.5*b - 2*c - e2 = 3 >> eq3: -0.3*a + 3*c - e3 = 0 >> eq4: e1 + e2 + e3 - 3*mean = 0 >> eq5: e1 - mean - dm1 = 0 >> eq6: e2 - mean - dm2 = 0 >> eq7: e3 - mean - dm3 = 0 >> eq8: a + b + c = 1 >> eq9: 3* var - dm1^2 - dm2^2 - dm3^2 = 0 >> >> Objective: minimize var >> >> However, I tried this too, but it did not work. >> >> 2) In order to keep moving on, I used the first model 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 follows. 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 > > ------------------------------------------------------------------------------ > 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 > > |
From: Joao M. <ma...@gm...> - 2010-02-22 00:50:18
|
Anders, Thank you for your help. I removed the code about the objective function, and after studying MarkowitzModel.generateOptimisationModel method, I concluded I should set lower limits for variables that have lower limits. It also looked like the method addSimpleWeightExpression is used to define the objective function, however, when I tried this, I got a NullPointerException because my objective function has quadratic terms. So, I used the method addEmptyQuadraticExpression, and defined a big "contribution weight" (BigMath.VERY_POSITIVE) by calling method setContributionWeight. I think that, by doing this, I defined this quadratic equation as my objective function, is this correct? The objective function is dm1^2 + dm2^2 + dm3^2, so, my final code for the expression looks like this: model.addEmptyQuadraticExpression( "eq9" ); model.getExpression( "eq9" ).setQuadraticFactor( model.indexOfVariable( "dm1" ), model.indexOfVariable( "dm1" ), BigMath.ONE ); model.getExpression( "eq9" ).setQuadraticFactor( model.indexOfVariable( "dm2" ), model.indexOfVariable( "dm2" ), BigMath.ONE ); model.getExpression( "eq9" ).setQuadraticFactor( model.indexOfVariable( "dm3" ), model.indexOfVariable( "dm3" ), BigMath.ONE ); model.getExpression( "eq9" ).setContributionWeight( BigMath.VERY_POSITIVE ); The model I created has all these variables (the complete code is at the end of this message) However, I am not quite sure about how to set the constraints of my problem. They are the following: eq1: 0.5*a + 2*b + 3*c - e1 = 2 eq2: a - 0.5*b - 2*c - e2 = 3 eq3: -0.3*a + 3*c - e3 = 0 eq4: e1 + e2 + e3 - 3*mean = 0 eq5: e1 - mean - dm1 = 0 eq6: e2 - mean - dm2 = 0 eq7: e3 - mean - dm3 = 0 eq8: a + b + c = 1 By reading the javadocs, I understood that the method I should use is addConstraint, which receives the lower and the upper limits. So, in order to get an equality, I just have to make upperLimit = lowerLimit. So, my code looked like this (notice that I used upperLimit = lowerLimit = 0, and setConstant to define the equality): model.addConstraint( "eq1", BigMath.ZERO, BigMath.ZERO ); model.addConstraint( "eq2", BigMath.ZERO, BigMath.ZERO ); model.addConstraint( "eq3", BigMath.ZERO, BigMath.ZERO ); model.addConstraint( "eq4", BigMath.ZERO, BigMath.ZERO ); model.addConstraint( "eq5", BigMath.ZERO, BigMath.ZERO ); model.addConstraint( "eq6", BigMath.ZERO, BigMath.ZERO ); model.addConstraint( "eq7", BigMath.ZERO, BigMath.ZERO ); model.addConstraint( "eq8", BigMath.ZERO, BigMath.ZERO ); model.getExpression( "eq1" ).setLinearFactor( model.indexOfVariable( "A" ), BigMath.HALF ); 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" ), BigMath.ONE ); 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( BigMath.ZERO ); model.getExpression( "eq4" ).setLinearFactor( model.indexOfVariable( "e1" ), BigMath.ONE ); model.getExpression( "eq4" ).setLinearFactor( model.indexOfVariable( "e2" ), BigMath.ONE ); model.getExpression( "eq4" ).setLinearFactor( model.indexOfVariable( "e3" ), BigMath.ONE ); model.getExpression( "eq4" ).setLinearFactor( model.indexOfVariable( "mean" ), new BigDecimal( -3 ) ); model.getExpression( "eq4" ).setConstant( BigMath.ZERO ); model.getExpression( "eq5" ).setLinearFactor( model.indexOfVariable( "e1" ), BigMath.ONE ); model.getExpression( "eq5" ).setLinearFactor( model.indexOfVariable( "mean" ), new BigDecimal( -1 ) ); model.getExpression( "eq5" ).setLinearFactor( model.indexOfVariable( "dm1" ), new BigDecimal( -1 ) ); model.getExpression( "eq5" ).setConstant( BigMath.ZERO ); model.getExpression( "eq6" ).setLinearFactor( model.indexOfVariable( "e2" ), BigMath.ONE ); model.getExpression( "eq6" ).setLinearFactor( model.indexOfVariable( "mean" ), new BigDecimal( -1 ) ); model.getExpression( "eq6" ).setLinearFactor( model.indexOfVariable( "dm2" ), new BigDecimal( -1 ) ); model.getExpression( "eq6" ).setConstant( BigMath.ZERO ); model.getExpression( "eq7" ).setLinearFactor( model.indexOfVariable( "e3" ), BigMath.ONE ); model.getExpression( "eq7" ).setLinearFactor( model.indexOfVariable( "mean" ), new BigDecimal( -1 ) ); model.getExpression( "eq7" ).setLinearFactor( model.indexOfVariable( "dm3" ), new BigDecimal( -1 ) ); model.getExpression( "eq7" ).setConstant( BigMath.ZERO ); model.getExpression( "eq8" ).setLinearFactor( model.indexOfVariable( "A" ), BigMath.ONE ); model.getExpression( "eq8" ).setLinearFactor( model.indexOfVariable( "B" ), BigMath.ONE ); model.getExpression( "eq8" ).setLinearFactor( model.indexOfVariable( "C" ), BigMath.ONE ); model.getExpression( "eq8" ).setConstant( new BigDecimal( -1 ) ); So, I defined minimisation (by calling setMinimisation), generated a solver (by calling getDefaultSolver), and invoked the method solve, as in the following code: model.setMaximisation( false ); model.setMinimisation( true ); final OptimisationSolver solver = model.getDefaultSolver(); final Result res = solver.solve(); This time, I did not get an exception, but the following result: OPTIMAL (1) { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 } But this is not what I expected. Please, can you tell me what I am doing wrong? Cheers Joao Magalhaes --- -------- Code: import java.math.BigDecimal; import java.util.HashSet; import java.util.Set; import org.ojalgo.constant.BigMath; 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 ); A.setLowerLimit( BigMath.ZERO ); B.setLowerLimit( BigMath.ZERO ); C.setLowerLimit( BigMath.ZERO ); e1.setLowerLimit( BigMath.ZERO ); e2.setLowerLimit( BigMath.ZERO ); e3.setLowerLimit( BigMath.ZERO ); mean.setLowerLimit( BigMath.ZERO ); dm1.setLowerLimit( BigMath.VERY_NEGATIVE ); dm2.setLowerLimit( BigMath.VERY_NEGATIVE ); dm3.setLowerLimit( BigMath.VERY_NEGATIVE ); A.setUpperLimit( BigMath.VERY_POSITIVE ); B.setUpperLimit( BigMath.VERY_POSITIVE ); C.setUpperLimit( BigMath.VERY_POSITIVE ); e1.setUpperLimit( BigMath.VERY_POSITIVE ); e2.setUpperLimit( BigMath.VERY_POSITIVE ); e3.setUpperLimit( BigMath.VERY_POSITIVE ); mean.setUpperLimit( BigMath.VERY_POSITIVE ); dm1.setUpperLimit( BigMath.VERY_POSITIVE ); dm2.setUpperLimit( BigMath.VERY_POSITIVE ); dm3.setUpperLimit( BigMath.VERY_POSITIVE ); final QuadraticExpressionsModel model = new QuadraticExpressionsModel( variables ); model.addConstraint( "eq1", BigMath.ZERO, BigMath.ZERO ); model.addConstraint( "eq2", BigMath.ZERO, BigMath.ZERO ); model.addConstraint( "eq3", BigMath.ZERO, BigMath.ZERO ); model.addConstraint( "eq4", BigMath.ZERO, BigMath.ZERO ); model.addConstraint( "eq5", BigMath.ZERO, BigMath.ZERO ); model.addConstraint( "eq6", BigMath.ZERO, BigMath.ZERO ); model.addConstraint( "eq7", BigMath.ZERO, BigMath.ZERO ); model.addConstraint( "eq8", BigMath.ZERO, BigMath.ZERO ); model.addEmptyQuadraticExpression( "eq9" ); model.getExpression( "eq1" ).setLinearFactor( model.indexOfVariable( "A" ), BigMath.HALF ); 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" ), BigMath.ONE ); 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( BigMath.ZERO ); model.getExpression( "eq4" ).setLinearFactor( model.indexOfVariable( "e1" ), BigMath.ONE ); model.getExpression( "eq4" ).setLinearFactor( model.indexOfVariable( "e2" ), BigMath.ONE ); model.getExpression( "eq4" ).setLinearFactor( model.indexOfVariable( "e3" ), BigMath.ONE ); model.getExpression( "eq4" ).setLinearFactor( model.indexOfVariable( "mean" ), new BigDecimal( -3 ) ); model.getExpression( "eq4" ).setConstant( BigMath.ZERO ); model.getExpression( "eq5" ).setLinearFactor( model.indexOfVariable( "e1" ), BigMath.ONE ); model.getExpression( "eq5" ).setLinearFactor( model.indexOfVariable( "mean" ), new BigDecimal( -1 ) ); model.getExpression( "eq5" ).setLinearFactor( model.indexOfVariable( "dm1" ), new BigDecimal( -1 ) ); model.getExpression( "eq5" ).setConstant( BigMath.ZERO ); model.getExpression( "eq6" ).setLinearFactor( model.indexOfVariable( "e2" ), BigMath.ONE ); model.getExpression( "eq6" ).setLinearFactor( model.indexOfVariable( "mean" ), new BigDecimal( -1 ) ); model.getExpression( "eq6" ).setLinearFactor( model.indexOfVariable( "dm2" ), new BigDecimal( -1 ) ); model.getExpression( "eq6" ).setConstant( BigMath.ZERO ); model.getExpression( "eq7" ).setLinearFactor( model.indexOfVariable( "e3" ), BigMath.ONE ); model.getExpression( "eq7" ).setLinearFactor( model.indexOfVariable( "mean" ), new BigDecimal( -1 ) ); model.getExpression( "eq7" ).setLinearFactor( model.indexOfVariable( "dm3" ), new BigDecimal( -1 ) ); model.getExpression( "eq7" ).setConstant( BigMath.ZERO ); model.getExpression( "eq8" ).setLinearFactor( model.indexOfVariable( "A" ), BigMath.ONE ); model.getExpression( "eq8" ).setLinearFactor( model.indexOfVariable( "B" ), BigMath.ONE ); model.getExpression( "eq8" ).setLinearFactor( model.indexOfVariable( "C" ), BigMath.ONE ); model.getExpression( "eq8" ).setConstant( new BigDecimal( -1 ) ); model.getExpression( "eq9" ).setQuadraticFactor( model.indexOfVariable( "dm1" ), model.indexOfVariable( "dm1" ), BigMath.ONE ); model.getExpression( "eq9" ).setQuadraticFactor( model.indexOfVariable( "dm2" ), model.indexOfVariable( "dm2" ), BigMath.ONE ); model.getExpression( "eq9" ).setQuadraticFactor( model.indexOfVariable( "dm3" ), model.indexOfVariable( "dm3" ), BigMath.ONE ); model.getExpression( "eq9" ).setContributionWeight( BigMath.VERY_POSITIVE ); 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() ); } } > 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 exssions and/or variables. The method getObjectiveExssion() will give > you a derived/aggregated exssion. 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 Sun, Feb 21, 2010 at 6:18 PM, Joao Magalhaes <ma...@gm...> wrote: > Hello again, > > Looks like the matrix got messed up in my previous post. So, I am > sending the message again, but with the equations instead of the > matrix. > Apologies for this. > 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 equations below: > > Constraints: > > eq1: 0.5*a + 2*b + 3*c - e1 = 2 > eq2: a - 0.5*b - 2*c - e2 = 3 > eq3: -0.3*a + 3*c - e3 = 0 > eq4: e1 + e2 + e3 - 3*mean = 0 > eq5: e1 - mean - dm1 = 0 > eq6: e2 - mean - dm2 = 0 > eq7: e3 - mean - dm3 = 0 > eq8: a + b + c = 1 > > Objective: minimize dm1^2 + dm2^2 + dm3^2 > > The goal is to minimize the function dm1^2 + dm2^2 + dm3^2, subject to > the constraints. > 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: > > eq1: 0.5*a + 2*b + 3*c - e1 = 2 > eq2: a - 0.5*b - 2*c - e2 = 3 > eq3: -0.3*a + 3*c - e3 = 0 > eq4: e1 + e2 + e3 - 3*mean = 0 > eq5: e1 - mean - dm1 = 0 > eq6: e2 - mean - dm2 = 0 > eq7: e3 - mean - dm3 = 0 > eq8: a + b + c = 1 > eq9: 3* var - dm1^2 - dm2^2 - dm3^2 = 0 > > Objective: minimize var > > However, I tried this too, but it did not work. > > 2) In order to keep moving on, I used the first model 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 follows. 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 |
From: Joao M. <ma...@gm...> - 2010-02-21 21:18:31
|
Hello again, Looks like the matrix got messed up in my previous post. So, I am sending the message again, but with the equations instead of the matrix. Apologies for this. 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 equations below: Constraints: eq1: 0.5*a + 2*b + 3*c - e1 = 2 eq2: a - 0.5*b - 2*c - e2 = 3 eq3: -0.3*a + 3*c - e3 = 0 eq4: e1 + e2 + e3 - 3*mean = 0 eq5: e1 - mean - dm1 = 0 eq6: e2 - mean - dm2 = 0 eq7: e3 - mean - dm3 = 0 eq8: a + b + c = 1 Objective: minimize dm1^2 + dm2^2 + dm3^2 The goal is to minimize the function dm1^2 + dm2^2 + dm3^2, subject to the constraints. 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: eq1: 0.5*a + 2*b + 3*c - e1 = 2 eq2: a - 0.5*b - 2*c - e2 = 3 eq3: -0.3*a + 3*c - e3 = 0 eq4: e1 + e2 + e3 - 3*mean = 0 eq5: e1 - mean - dm1 = 0 eq6: e2 - mean - dm2 = 0 eq7: e3 - mean - dm3 = 0 eq8: a + b + c = 1 eq9: 3* var - dm1^2 - dm2^2 - dm3^2 = 0 Objective: minimize var However, I tried this too, but it did not work. 2) In order to keep moving on, I used the first model 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 follows. 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() ); } } |
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 |
From: Joao M. <ma...@gm...> - 2010-02-21 16:17:14
|
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() ); } } |
From: Anders P. <an...@op...> - 2010-01-26 15:44:22
|
New version of the tests: http://ojalgo.org/generated/ejml_sailfish_20100126.zip Currently running the UJML tests. /Anders On 13 jan 2010, at 15.51, Anders Peterson wrote: > http://ojalgo.org/performance_ujmp.html > > On 13 jan 2010, at 14.50, Anders Peterson wrote: > >> http://ojalgo.org/performance_ejml.html >> >> ------------------------------------------------------------------------------ >> This SF.Net email is sponsored by the Verizon Developer Community >> Take advantage of Verizon's best-in-class app development support >> A streamlined, 14 day to market process makes app distribution fast and easy >> Join now and get one step closer to millions of Verizon customers >> http://p.sf.net/sfu/verizon-dev2dev >> _______________________________________________ >> ojAlgo-user mailing list >> ojA...@li... >> https://lists.sourceforge.net/lists/listinfo/ojalgo-user |
From: Anders P. <an...@op...> - 2010-01-13 14:51:14
|
http://ojalgo.org/performance_ujmp.html On 13 jan 2010, at 14.50, Anders Peterson wrote: > http://ojalgo.org/performance_ejml.html > > ------------------------------------------------------------------------------ > This SF.Net email is sponsored by the Verizon Developer Community > Take advantage of Verizon's best-in-class app development support > A streamlined, 14 day to market process makes app distribution fast and easy > Join now and get one step closer to millions of Verizon customers > http://p.sf.net/sfu/verizon-dev2dev > _______________________________________________ > ojAlgo-user mailing list > ojA...@li... > https://lists.sourceforge.net/lists/listinfo/ojalgo-user |
From: Anders P. <an...@op...> - 2010-01-13 14:06:20
|
http://ojalgo.org/performance_ejml.html |
From: Anders P. <an...@op...> - 2009-11-17 21:41:34
|
Your test cases for both the LinearSolver and QuadraticSolver touch upon "known problems" with the respective solvers. I will solve these problems, but it wont happen today or tomorrow. Using a combination of Apache and ojAlgo is a solution as good as any. /Anders On 17 nov 2009, at 17.58, Pierre AUSLAENDER wrote: > > Hello, > > Further to the e-mail below, here is the same test case, but using the SimplexSolver from apache.commons.math. The test case converts the ojAlgo linear model to the required apache.commons.math constraints. > > > > When I now use this Apache solver to find initial solutions (or detect infeasibility) for the QuadraticSolver, everything seems OK. > > So what it seems to me is that the LinearSolver has maybe a bug in some circumstances; and that QuadraticSolver really loves starting from an interior point, and then seems to be just fine. > > Thanks, > Pierre > > > ----- Forwarded by Pierre AUSLAENDER/GVAExternal/PCO on 17.11.2009 17:50 ----- > Pierre AUSLAENDER/GVAExternal/PCO > tel. 2074 > 17.11.2009 16:33 > > To > oja...@li... > cc > Subject > LinearSolver problem > > > > > Hi Anders, > > I have a very simple linear test with only 2 variables and 1 constraint that fails. Here is the test case. > > We have variables x0 and x1, with values between 0 and 100, and we only require x0 = 2*x1. > > > > "test1" sets x0 = 10 and returns INFEASIBLE > "test2" sets x1 = 5 and returns OPTIMAL > > Both tests have strictly one and the same solution though. > > Did I do something wrong? > > I have a second question: ExpressionsBasedModel.validateSolution(...) does not check the variable boundaries. They only check the expressions added via "add...Expression(...)". Is this on purpose? > > Thanks, > Pierre > > ________________________________________________________________ > > PAM S.A Geneva Tel. +41 (0)58 323 3333 > 60, route des Acacias Fax +41 (0)58 323 2324 > CH-1211 GENEVE 73 http://www.pictet.com/ > ________________________________________________________________ > > This document should only be read by those persons to whom it is > addressed and is not intended to be relied upon by any person > without subsequent written confirmation of its contents. If you > have received this e-mail message in error, please destroy it > and delete it from your computer. > Any form of reproduction, dissemination, copying, disclosure, > modification, distribution and/or publication of this E-mail > message is strictly prohibited. > ________________________________________________________________ > > Pictet reserves the right to monitor and record business and > personal communications. By responding to an email or call from > a Pictet employee you are taken to have consented to such > monitoring and recording. > ________________________________________________________________ > > > <LinearSolverTest.java><SimplexSolverTest.java>------------------------------------------------------------------------------ > Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day > trial. Simplify your report design, integration and deployment - and focus on > what you do best, core application coding. Discover what's new with > Crystal Reports now. http://p.sf.net/sfu/bobj-july_______________________________________________ > ojAlgo-user mailing list > ojA...@li... > https://lists.sourceforge.net/lists/listinfo/ojalgo-user |
From: Anders P. <an...@op...> - 2009-11-17 21:31:55
|
No, you don't do anything wrong. The simplex solver has problems with some (redundant) problem formulations. I haven't figured out how to solve it yet. In this case you don't really have an optimisation problem. (You don't even give values for "c".) All you need to do is solve Ax= B where A= 1 -2 0 0 1 1 1 0 0 and B= 0 100 10 The validateSolution() method should of course also check the variable boundaries. This will be fixed soon. /Anders On 17 nov 2009, at 16.33, Pierre AUSLAENDER wrote: > > Hi Anders, > > I have a very simple linear test with only 2 variables and 1 constraint that fails. Here is the test case. > > We have variables x0 and x1, with values between 0 and 100, and we only require x0 = 2*x1. > > > > "test1" sets x0 = 10 and returns INFEASIBLE > "test2" sets x1 = 5 and returns OPTIMAL > > Both tests have strictly one and the same solution though. > > Did I do something wrong? > > I have a second question: ExpressionsBasedModel.validateSolution(...) does not check the variable boundaries. They only check the expressions added via "add...Expression(...)". Is this on purpose? > > Thanks, > Pierre > > ________________________________________________________________ > > PAM S.A Geneva Tel. +41 (0)58 323 3333 > 60, route des Acacias Fax +41 (0)58 323 2324 > CH-1211 GENEVE 73 http://www.pictet.com/ > ________________________________________________________________ > > This document should only be read by those persons to whom it is > addressed and is not intended to be relied upon by any person > without subsequent written confirmation of its contents. If you > have received this e-mail message in error, please destroy it > and delete it from your computer. > Any form of reproduction, dissemination, copying, disclosure, > modification, distribution and/or publication of this E-mail > message is strictly prohibited. > ________________________________________________________________ > > Pictet reserves the right to monitor and record business and > personal communications. By responding to an email or call from > a Pictet employee you are taken to have consented to such > monitoring and recording. > ________________________________________________________________ > > > <LinearSolverTest.java>------------------------------------------------------------------------------ > Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day > trial. Simplify your report design, integration and deployment - and focus on > what you do best, core application coding. Discover what's new with > Crystal Reports now. http://p.sf.net/sfu/bobj-july_______________________________________________ > ojAlgo-user mailing list > ojA...@li... > https://lists.sourceforge.net/lists/listinfo/ojalgo-user |
From: Anders P. <an...@op...> - 2009-11-17 09:33:02
|
On 17 nov 2009, at 09.47, Pierre AUSLAENDER wrote: > Yes, that seems perfectly correct. Sorry I didn't notice the null > solution. > > However I still have two failures, tests 2 and 5. We agree that 2 and 5 are "problematic"... > In the following code, I use the LinearSolver to get an > initialIterationPoint for the QuandraticSolver, or to detect that > the system is not feasible. The LinearSolver seems to behave very > well. It seems to work and I get rid of the two remaining failures. > > Do you think this is generally safe and not too time consuming? > Would you suggest I can use this in my code to get initial iteration > points and also to check if the system is feasible? That's probably a good idea. If you can make things easier for the ActiveSetSolver you have gained something. I'll probably include this as an built-in option for future versions of the ActiveSetSolver. The weak spot of the QuadraticSolver is to determine if the problem is infeasible or not (in combination with finding the correct combination of active inequality constraints). The LinearSolver has problems with redundant model elements. More advanced linear solvers have presolvers that eliminate redundant variables and constraints. ojAlgo does not yet have this. /Anders > /* > * (c) Copyright Pictet & Cie 2009 > */ > package com.pictet.pam.bam.test; > > import java.math.BigDecimal; > import java.util.Arrays; > > import org.junit.Assert; > import org.junit.Test; > import org.ojalgo.constant.BigMath; > import org.ojalgo.matrix.store.MatrixStore; > import org.ojalgo.optimisation.Expression; > import org.ojalgo.optimisation.State; > import org.ojalgo.optimisation.Variable; > import org.ojalgo.optimisation.OptimisationSolver.Result; > import org.ojalgo.optimisation.linear.LinearExpressionsModel; > import org.ojalgo.optimisation.linear.LinearSolver; > import org.ojalgo.optimisation.quadratic.QuadraticExpressionsModel; > import org.ojalgo.optimisation.quadratic.QuadraticSolver; > > /** > * @author pauslaender > * > */ > public class OptimisationSolverTest { > > Variable[] vars; > BigDecimal[] point; > QuadraticExpressionsModel quadraticModel; > LinearExpressionsModel linearModel; > > /** > * > * @param numberOfVars > * greater than 6 > */ > void setupModels(int numberOfVars) { > if (numberOfVars < 6) { > throw new > IllegalArgumentException("numberOfVars must be >= 6 !!!"); > } > // > // variables > // > vars = new Variable[numberOfVars]; > vars[0] = new Variable("x0").lower(new > BigDecimal(00.0)).upper(new BigDecimal(15.0)); > vars[1] = new Variable("x1").lower(new > BigDecimal(17.0)).upper(new BigDecimal(27.0)); > vars[2] = new Variable("x2").lower(new > BigDecimal(19.0)).upper(new BigDecimal(34.0)); > vars[3] = new Variable("x3").lower(new > BigDecimal(25.0)).upper(new BigDecimal(48.0)); > vars[4] = new Variable("x4").lower(new > BigDecimal(05.0)).upper(new BigDecimal(18.0)); > vars[5] = new Variable("x5").lower(new > BigDecimal(02.0)).upper(new BigDecimal(09.0)); > for (int i = 6; i < numberOfVars; ++i) { > vars[i] = new Variable("x" + > i).level(BigMath.ZERO); > } > // > // minimize distance to this point > // > point = new BigDecimal[numberOfVars]; > point[0] = new BigDecimal(1.0); > point[1] = new BigDecimal(25.0); > point[2] = new BigDecimal(33.0); > point[3] = new BigDecimal(29.0); > point[4] = new BigDecimal(9.0); > point[5] = new BigDecimal(2.0); > for (int i = 6; i < numberOfVars; ++i) { > point[i] = new BigDecimal(0.0); > } > // > // models > // > linearModel = new LinearExpressionsModel(vars); > quadraticModel = new QuadraticExpressionsModel(vars); > // > // objective function - only assigned to the > quadratic model > // > { > Expression e = > quadraticModel.addOffsetExpression("objective", Arrays.asList(point)); > e.setContributionWeight(BigMath.HALF); > } > // > // sum(xi) = 100.0 > // > { > Expression e = > quadraticModel.addSimpleWeightExpression("sum(xi) = 100.0"); > e.level(BigMath.HUNDRED); > e = > linearModel.addSimpleWeightExpression("sum(xi) = 100.0"); > e.level(BigMath.HUNDRED); > } > // > // x1 + x2 <= 45 > // > { > Expression e = > quadraticModel.addEmptyLinearExpression("x1 + x2 <= 45.0"); > e.setLinearFactor(1, BigMath.ONE); > e.setLinearFactor(2, BigMath.ONE); > e.lower(BigMath.ZERO).upper(new > BigDecimal(45.0)); > e = linearModel.addEmptyLinearExpression("x1 > + x2 <= 45.0"); > e.setLinearFactor(1, BigMath.ONE); > e.setLinearFactor(2, BigMath.ONE); > e.lower(BigMath.ZERO).upper(new > BigDecimal(45.0)); > } > // > // x4 - 2*x5 = 0 > // > { > Expression e = > quadraticModel.addEmptyLinearExpression("x4 - 2*x5 = 0"); > e.setLinearFactor(4, BigMath.ONE); > e.setLinearFactor(5, BigMath.TWO.negate()); > e.level(BigMath.ZERO); > e = linearModel.addEmptyLinearExpression("x4 > - 2*x5 = 0"); > e.setLinearFactor(4, BigMath.ONE); > e.setLinearFactor(5, BigMath.TWO.negate()); > e.level(BigMath.ZERO); > } > } > > @Test > public void test1() { > setupModels(6); > MatrixStore<Double> initialIterationPoint = > solveLinear(); > Assert.assertTrue(initialIterationPoint != null); > MatrixStore<Double> solution = > solveQuadratic(initialIterationPoint); > Assert.assertTrue(solution != null); > } > > @Test > public void test2() { > setupModels(6); > vars[3].level(new BigDecimal(40.0)); > vars[4].level(new BigDecimal(18.0)); > MatrixStore<Double> initialIterationPoint = > solveLinear(); > Assert.assertFalse(initialIterationPoint != null); > MatrixStore<Double> solution = > solveQuadratic(initialIterationPoint); > Assert.assertFalse(solution != null); > } > > @Test > public void test3() { > setupModels(6); > vars[3].level(new BigDecimal(48.0)); > vars[4].level(new BigDecimal(18.0)); > vars[5].level(new BigDecimal(5.0)); > MatrixStore<Double> initialIterationPoint = > solveLinear(); > Assert.assertFalse(initialIterationPoint != null); > MatrixStore<Double> solution = > solveQuadratic(initialIterationPoint); > Assert.assertFalse(solution != null); > } > > @Test > public void test4() { > setupModels(42); > MatrixStore<Double> initialIterationPoint = > solveLinear(); > Assert.assertTrue(initialIterationPoint != null); > MatrixStore<Double> solution = > solveQuadratic(initialIterationPoint); > Assert.assertTrue(solution != null); > } > > @Test > public void test5() { > setupModels(42); > vars[3].level(new BigDecimal(40.0)); > vars[4].level(new BigDecimal(18.0)); > MatrixStore<Double> initialIterationPoint = > solveLinear(); > Assert.assertFalse(initialIterationPoint != null); > MatrixStore<Double> solution = > solveQuadratic(initialIterationPoint); > Assert.assertFalse(solution != null); > } > > @Test > public void test6() { > setupModels(42); > vars[3].level(new BigDecimal(40.0)); > vars[4].level(new BigDecimal(18.0)); > vars[5].level(new BigDecimal(5.0)); > MatrixStore<Double> initialIterationPoint = > solveLinear(); > Assert.assertFalse(initialIterationPoint != null); > MatrixStore<Double> solution = > solveQuadratic(initialIterationPoint); > Assert.assertFalse(solution != null); > } > > MatrixStore<Double> solveLinear() { > final LinearSolver solver = new > LinearSolver.Builder(linearModel).build(); > final Result result = solver.solve(); > > if (result.getSolution() != null) { > if > (result.getState().isNotLessThan(State.FEASIBLE)) { > if > (linearModel.validateSolution(result.getSolution(), > solver.options.solutionContext)) { > return > result.getSolution().toPrimitiveStore(); > } > } > } > > return null; > } > > MatrixStore<Double> solveQuadratic(final MatrixStore<Double> > initialIterationPoint) { > if (initialIterationPoint == null) { > return null; > } > final QuadraticSolver solver = new > QuadraticSolver.Builder(quadraticModel).build(); > final Result result = solver.solve(); > > if (result.getSolution() != null) { > final boolean validated = > quadraticModel.validateSolution(result.getSolution(), > solver.options.solutionContext); > if > (result.getState().isNotLessThan(State.FEASIBLE)) { > final String message = "State: " + > result.getState() + ", validated: " + validated; > Assert.assertTrue(message, validated); > if (validated) { > return > result.getSolution().toPrimitiveStore(); > } > } else { > final String message = "State: " + > result.getState() + ", validated: " + validated; > Assert.assertFalse(message, > validated); > } > } else { > Assert.assertFalse("No solution but state > FEASIBLE", result.getState().isNotLessThan(State.FEASIBLE)); > } > > return null; > } > > } > > Thanks, > Pierre > > > > "Anders Peterson" <an...@op...> > 16.11.2009 22:10 > Please respond to > oja...@li... > > > To > oja...@li... > cc > Subject > Re: [ojAlgo-user] QuadraticSolver with unsatisfiable constraints > > > > Warning: this message has been delivered from outside the Pictet > Group. If the content is related to banking matters, please apply > the same procedure as for an incoming Fax or letter. > Attention: Ce message provient de l'extérieur du Groupe Pictet. S'il > possède un caractère bancaire, veuillez le traiter de la même > manière qu'un Fax ou une lettre entrante. > > > > > > I modified the solve-method to be > > void solve() { > > final QuadraticSolver solver = new > QuadraticSolver.Builder(model).build(); > final Result result = solver.solve(); > > if (result.getSolution() != null) { > final boolean validated = > model.validateSolution(result.getSolution(), > solver.options.solutionContext); > if (result.getState().isNotLessThan(State.FEASIBLE)) { > final String message = "State: " + result.getState() > + ", validated: " + validated; > Assert.assertTrue(message, validated); > } else { > final String message = "State: " + result.getState() > + ", validated: " + validated; > Assert.assertFalse(message, validated); > } > } else { > Assert.assertFalse("No solution but state FEASIBLE", > result.getState().isNotLessThan(State.FEASIBLE)); > } > } > > Does that seem correct to you? > > > Then, when I run the tests I only get a failure (failed test) with > test "2", and I get no error (like an NPE). > > Tests "2" and "5" takes very long - long enough to suggest that > there is something the algorithm can't handle. > > I see no problem with test "1", "3", "4" or "6". > > Do you agree with this? > > > Have you tried setting different contexts for > "options.solutionContext"? I don't believe the > "options.problemContext" affect anything with the QuadraticSolver > (yet). > > /Anders > > > On 16 nov 2009, at 16.30, Pierre AUSLAENDER wrote: > > > > > Sure, here it is (and sorry, the NPE was not in the SVD, but in > the validateSolution) > > > > test1: OK > > test2: KO with STATE = FAILED, VALIDATION = TRUE > > test3: KO with NPE in ExpressionBasedModel.validateSolution > > test4: OK > > test5: KO with STATE = OPTIMAL, VALIDATION = FALSE > > test6: KO with NPE in ExpressionBasedModel.validateSolution > > > > /* > > * (c) Copyright Pictet & Cie 2009 > > */ > > package com.pictet.pam.bam.test; > > > > import java.math.BigDecimal; > > import java.util.Arrays; > > > > import org.junit.Assert; > > import org.junit.Test; > > import org.ojalgo.constant.BigMath; > > import org.ojalgo.optimisation.Expression; > > import org.ojalgo.optimisation.State; > > import org.ojalgo.optimisation.Variable; > > import org.ojalgo.optimisation.OptimisationSolver.Result; > > import org.ojalgo.optimisation.quadratic.QuadraticExpressionsModel; > > import org.ojalgo.optimisation.quadratic.QuadraticSolver; > > > > /** > > * @author pauslaender > > * > > */ > > public class OptimisationSolverTest { > > > > Variable[] vars; > > BigDecimal[] point; > > QuadraticExpressionsModel model; > > > > /** > > * > > * @param numberOfVars greater than 6 > > */ > > void setupModel(int numberOfVars) { > > if (numberOfVars < 6) { > > throw new > IllegalArgumentException("numberOfVars must be >= 6 !!!"); > > } > > // > > // variables > > // > > vars = new Variable[numberOfVars]; > > vars[0] = new Variable("x0").lower(new > BigDecimal(00.0)).upper(new BigDecimal(15.0)); > > vars[1] = new Variable("x1").lower(new > BigDecimal(17.0)).upper(new BigDecimal(27.0)); > > vars[2] = new Variable("x2").lower(new > BigDecimal(19.0)).upper(new BigDecimal(34.0)); > > vars[3] = new Variable("x3").lower(new > BigDecimal(25.0)).upper(new BigDecimal(48.0)); > > vars[4] = new Variable("x4").lower(new > BigDecimal(05.0)).upper(new BigDecimal(18.0)); > > vars[5] = new Variable("x5").lower(new > BigDecimal(02.0)).upper(new BigDecimal(09.0)); > > for (int i = 6; i < numberOfVars; ++i) { > > vars[i] = new Variable("x" + > i).level(BigMath.ZERO); > > } > > // > > // minimise distance to this point > > // > > point = new BigDecimal[numberOfVars]; > > point[0] = new BigDecimal(1.0); > > point[1] = new BigDecimal(25.0); > > point[2] = new BigDecimal(33.0); > > point[3] = new BigDecimal(29.0); > > point[4] = new BigDecimal(9.0); > > point[5] = new BigDecimal(2.0); > > for (int i = 6; i < numberOfVars; ++i) { > > point[i] = new BigDecimal(0.0); > > } > > // > > // model > > // > > model = new QuadraticExpressionsModel(vars); > > // > > // objective function > > // > > { > > Expression e = > model.addOffsetExpression("objective", Arrays.asList(point)); > > e.setContributionWeight(BigMath.HALF); > > } > > // > > // sum(xi) = 100.0 > > // > > { > > Expression e = > model.addSimpleWeightExpression("sum(xi) = 100.0"); > > e.level(BigMath.HUNDRED); > > } > > // > > // x1 + x2 <= 45 > > // > > { > > Expression e = > model.addEmptyLinearExpression("x1 + x2 <= 45.0"); > > e.setLinearFactor(1, BigMath.ONE); > > e.setLinearFactor(2, BigMath.ONE); > > e.lower(BigMath.ZERO).upper(new > BigDecimal(45.0)); > > } > > // > > // x4 - 2*x5 = 0 > > // > > { > > Expression e = > model.addEmptyLinearExpression("x4 - 2*x5 = 0"); > > e.setLinearFactor(4, BigMath.ONE); > > e.setLinearFactor(5, BigMath.TWO.negate()); > > e.level(BigMath.ZERO); > > } > > } > > > > @Test > > public void test1() { > > setupModel(6); > > solve(); > > } > > > > @Test > > public void test2() { > > setupModel(6); > > vars[3].level(new BigDecimal(40.0)); > > vars[4].level(new BigDecimal(18.0)); > > solve(); > > } > > > > @Test > > public void test3() { > > setupModel(6); > > vars[3].level(new BigDecimal(48.0)); > > vars[4].level(new BigDecimal(18.0)); > > vars[5].level(new BigDecimal(5.0)); > > solve(); > > } > > > > @Test > > public void test4() { > > setupModel(42); > > solve(); > > } > > > > @Test > > public void test5() { > > setupModel(42); > > vars[3].level(new BigDecimal(40.0)); > > vars[4].level(new BigDecimal(18.0)); > > solve(); > > } > > > > @Test > > public void test6() { > > setupModel(42); > > vars[3].level(new BigDecimal(40.0)); > > vars[4].level(new BigDecimal(18.0)); > > vars[5].level(new BigDecimal(5.0)); > > solve(); > > } > > > > void solve() { > > QuadraticSolver solver = new > QuadraticSolver.Builder(model).build(); > > // QuadraticSolver.DEBUG = true; > > // System.out.println(model); > > Result result = solver.solve(); > > // System.out.println(result); > > // > System.out.println(result.getSolution().toListOfElements()); > > boolean validated = > model.validateSolution(result.getSolution(), > solver.options.solutionContext); > > if (result.getState() == State.OPTIMAL) { > > String message = "State: " + > result.getState() + ", validated: " + validated; > > Assert.assertTrue(message, validated); > > } else { > > String message = "State: " + > result.getState() + ", validated: " + validated; > > Assert.assertFalse(message, validated); > > } > > } > > > > } > > > > > > > > "Anders Peterson" <an...@op...> > > 16.11.2009 15:42 > > Please respond to > > oja...@li... > > > > To > > oja...@li... > > cc > > Subject > > Re: [ojAlgo-user] QuadraticSolver with unsatisfiable constraints > > > > > > > > Warning: this message has been delivered from outside the Pictet > Group. If the content is related to banking matters, please apply > the same procedure as for an incoming Fax or letter. > > Attention: Ce message provient de l'extérieur du Groupe Pictet. > S'il possède un caractère bancaire, veuillez le traiter de la même > manière qu'un Fax ou une lettre entrante. > > > > > > > > > > > > Can you perhaps give me 2 test cases; 1 for the NPE case and 1 for > the > > incorrect state case. /Anders > > > > > > On 16 nov 2009, at 14.33, Pierre AUSLAENDER wrote: > > > > > > > > Hello Anders, > > > > > > Thanks a lot for the swift reply. > > > > > > I 've played with the latest snapshot and here are my findings: > > > - Most of the times now, when the constraints cannot be satisfied, > > > the solver terminates with the state FAILED, which is what one > would > > > expect, albeit after a great lot of iterations. > > > - Sometimes however, it terminates with the state OPTIMAL, and the > > > model validation returns FALSE, same as before I took the > snapshot. > > > - Sometimes as well, it throws a NPE in the SVD (after saying that > > > LU is not solvable). > > > > > > I'm trying to build a simple JUnit and send it to you. > > > > > > Kind regards, > > > Pierre > > > > > > > > > > > > "Anders Peterson" <an...@op...> > > > 16.11.2009 12:57 > > > Please respond to > > > oja...@li... > > > > > > > > > To > > > oja...@li... > > > cc > > > Subject > > > Re: [ojAlgo-user] QuadraticSolver with unsatisfiable constraints > > > > > > > > > > > > Warning: this message has been delivered from outside the Pictet > > > Group. If the content is related to banking matters, please apply > > > the same procedure as for an incoming Fax or letter. > > > Attention: Ce message provient de l'extérieur du Groupe Pictet. > S'il > > > possède un caractère bancaire, veuillez le traiter de la même > > > manière qu'un Fax ou une lettre entrante. > > > > > > > > > > > > > > > > > > On 16 nov 2009, at 11.06, Pierre AUSLAENDER wrote: > > > > > > > Hello, > > > > > > > > Thank you for this library, which is a joy to use - once I got > over > > > > the lack of documentation. > > > > > > > > I'm using the QuadraticSolver (ActiveSetSolver) on a least > squares > > > > minimisation problem, under a set of linear constraints. When > the > > > > constraints can be satisfied, it works perfectly. However, > when the > > > > constraints cannot be satisfied, the solver still returns with > an > > > > OPTIMAL state after a few iterations. I was puzzled until I > > > > discovered the QuadraticSolver.validateSolution(...) methods, > which > > > > I can use to effectively test the solution against the > constraints. > > > > > > > > Is this the way it is supposed to work, or did I do something > wrong? > > > > I would have expected the solver to FAIL instead. > > > > > > > > > I recently fixed a couple of bugs related to this. Get the latest > > > snapshot download from Source Forge, and try if that solves your > > > problem. If it doesn't I'd like you to give me (junit) test case. > > > > > > > > > > As an aside question, there is the following JavaDoc comment on > > > > ExpressionBasedModel.addConstraint(final String aName, ....): > Note > > > > that only the first constraint you add will be used by the > solver. > > > > What does this comment mean? Is it simply related to the > uniqueness > > > > of the constraint name, or has it wider implications? > > > > > > > > > That comment should have been removed. It was only relevant for > the > > > now removed/replaced KnapsckSolver. That solver could only use 1 > > > constraint - the first one you added. I'll correct the javadoc > comment > > > now. > > > > > > > > > /Anders > > > > > > > > > > > > > Thank you, > > > > Pierre > > > > > > > > ________________________________________________________________ > > > > > > > > PAM S.A Geneva Tel. +41 (0)58 323 3333 > > > > 60, route des Acacias Fax +41 (0)58 323 2324 > > > > CH-1211 GENEVE 73 http://www.pictet.com/ > > > > ________________________________________________________________ > > > > > > > > This document should only be read by those persons to whom it is > > > > addressed and is not intended to be relied upon by any person > > > > without subsequent written confirmation of its contents. If you > > > > have received this e-mail message in error, please destroy it > > > > and delete it from your computer. > > > > Any form of reproduction, dissemination, copying, disclosure, > > > > modification, distribution and/or publication of this E-mail > > > > message is strictly prohibited. > > > > ________________________________________________________________ > > > > > > > > Pictet reserves the right to monitor and record business and > > > > personal communications. By responding to an email or call from > > > > a Pictet employee you are taken to have consented to such > > > > monitoring and recording. > > > > ________________________________________________________________ > > > > > > > > > > > > ------------------------------------------------------------------------------ > > > > Let Crystal Reports handle the reporting - Free Crystal > Reports 2008 > > > > 30-Day > > > > trial. Simplify your report design, integration and deployment > - and > > > > focus on > > > > what you do best, core application coding. Discover what's new > with > > > > Crystal Reports now. http://p.sf.net/sfu/bobj-july_______________________________________________ > > > > ojAlgo-user mailing list > > > > ojA...@li... > > > > https://lists.sourceforge.net/lists/listinfo/ojalgo-user > > > > > > > > > > ------------------------------------------------------------------------------ > > > Let Crystal Reports handle the reporting - Free Crystal Reports > 2008 > > > 30-Day > > > trial. Simplify your report design, integration and deployment - > and > > > focus on > > > what you do best, core application coding. Discover what's new > with > > > Crystal Reports now. http://p.sf.net/sfu/bobj-july > > > _______________________________________________ > > > ojAlgo-user mailing list > > > ojA...@li... > > > https://lists.sourceforge.net/lists/listinfo/ojalgo-user > > > > > > > > > > > > ________________________________________________________________ > > > > > > PAM S.A Geneva Tel. +41 (0)58 323 3333 > > > 60, route des Acacias Fax +41 (0)58 323 2324 > > > CH-1211 GENEVE 73 http://www.pictet.com/ > > > ________________________________________________________________ > > > > > > This document should only be read by those persons to whom it is > > > addressed and is not intended to be relied upon by any person > > > without subsequent written confirmation of its contents. If you > > > have received this e-mail message in error, please destroy it > > > and delete it from your computer. > > > Any form of reproduction, dissemination, copying, disclosure, > > > modification, distribution and/or publication of this E-mail > > > message is strictly prohibited. > > > ________________________________________________________________ > > > > > > Pictet reserves the right to monitor and record business and > > > personal communications. By responding to an email or call from > > > a Pictet employee you are taken to have consented to such > > > monitoring and recording. > > > ________________________________________________________________ > > > > > > > ------------------------------------------------------------------------------ > > > Let Crystal Reports handle the reporting - Free Crystal Reports > 2008 > > > 30-Day > > > trial. Simplify your report design, integration and deployment - > and > > > focus on > > > what you do best, core application coding. Discover what's new > with > > > Crystal Reports now. http://p.sf.net/sfu/bobj-july_______________________________________________ > > > ojAlgo-user mailing list > > > ojA...@li... > > > https://lists.sourceforge.net/lists/listinfo/ojalgo-user > > > > > > > ------------------------------------------------------------------------------ > > Let Crystal Reports handle the reporting - Free Crystal Reports > 2008 30-Day > > trial. Simplify your report design, integration and deployment - > and focus on > > what you do best, core application coding. Discover what's new with > > Crystal Reports now. http://p.sf.net/sfu/bobj-july > > _______________________________________________ > > ojAlgo-user mailing list > > ojA...@li... > > https://lists.sourceforge.net/lists/listinfo/ojalgo-user > > > > > > > > ________________________________________________________________ > > > > PAM S.A Geneva Tel. +41 (0)58 323 3333 > > 60, route des Acacias Fax +41 (0)58 323 2324 > > CH-1211 GENEVE 73 http://www.pictet.com/ > > ________________________________________________________________ > > > > This document should only be read by those persons to whom it is > > addressed and is not intended to be relied upon by any person > > without subsequent written confirmation of its contents. If you > > have received this e-mail message in error, please destroy it > > and delete it from your computer. > > Any form of reproduction, dissemination, copying, disclosure, > > modification, distribution and/or publication of this E-mail > > message is strictly prohibited. > > ________________________________________________________________ > > > > Pictet reserves the right to monitor and record business and > > personal communications. By responding to an email or call from > > a Pictet employee you are taken to have consented to such > > monitoring and recording. > > ________________________________________________________________ > > > > > > > ------------------------------------------------------------------------------ > > Let Crystal Reports handle the reporting - Free Crystal Reports > 2008 30-Day > > trial. Simplify your report design, integration and deployment - > and focus on > > what you do best, core application coding. Discover what's new with > > Crystal Reports now. http://p.sf.net/sfu/bobj-july_______________________________________________ > > ojAlgo-user mailing list > > ojA...@li... > > https://lists.sourceforge.net/lists/listinfo/ojalgo-user > > > ------------------------------------------------------------------------------ > Let Crystal Reports handle the reporting - Free Crystal Reports 2008 > 30-Day > trial. Simplify your report design, integration and deployment - and > focus on > what you do best, core application coding. Discover what's new with > Crystal Reports now. http://p.sf.net/sfu/bobj-july > _______________________________________________ > ojAlgo-user mailing list > ojA...@li... > https://lists.sourceforge.net/lists/listinfo/ojalgo-user > > > > ________________________________________________________________ > > PAM S.A Geneva Tel. +41 (0)58 323 3333 > 60, route des Acacias Fax +41 (0)58 323 2324 > CH-1211 GENEVE 73 http://www.pictet.com/ > ________________________________________________________________ > > This document should only be read by those persons to whom it is > addressed and is not intended to be relied upon by any person > without subsequent written confirmation of its contents. If you > have received this e-mail message in error, please destroy it > and delete it from your computer. > Any form of reproduction, dissemination, copying, disclosure, > modification, distribution and/or publication of this E-mail > message is strictly prohibited. > ________________________________________________________________ > > Pictet reserves the right to monitor and record business and > personal communications. By responding to an email or call from > a Pictet employee you are taken to have consented to such > monitoring and recording. > ________________________________________________________________ > > ------------------------------------------------------------------------------ > Let Crystal Reports handle the reporting - Free Crystal Reports 2008 > 30-Day > trial. Simplify your report design, integration and deployment - and > focus on > what you do best, core application coding. Discover what's new with > Crystal Reports now. http://p.sf.net/sfu/bobj-july_______________________________________________ > ojAlgo-user mailing list > ojA...@li... > https://lists.sourceforge.net/lists/listinfo/ojalgo-user |
From: Pierre A. <pau...@pi...> - 2009-11-17 08:47:44
|
Yes, that seems perfectly correct. Sorry I didn't notice the null solution. However I still have two failures, tests 2 and 5. In the following code, I use the LinearSolver to get an initialIterationPoint for the QuandraticSolver, or to detect that the system is not feasible. The LinearSolver seems to behave very well. It seems to work and I get rid of the two remaining failures. Do you think this is generally safe and not too time consuming? Would you suggest I can use this in my code to get initial iteration points and also to check if the system is feasible? /* * (c) Copyright Pictet & Cie 2009 */ package com.pictet.pam.bam.test; import java.math.BigDecimal; import java.util.Arrays; import org.junit.Assert; import org.junit.Test; import org.ojalgo.constant.BigMath; import org.ojalgo.matrix.store.MatrixStore; import org.ojalgo.optimisation.Expression; import org.ojalgo.optimisation.State; import org.ojalgo.optimisation.Variable; import org.ojalgo.optimisation.OptimisationSolver.Result; import org.ojalgo.optimisation.linear.LinearExpressionsModel; import org.ojalgo.optimisation.linear.LinearSolver; import org.ojalgo.optimisation.quadratic.QuadraticExpressionsModel; import org.ojalgo.optimisation.quadratic.QuadraticSolver; /** * @author pauslaender * */ public class OptimisationSolverTest { Variable[] vars; BigDecimal[] point; QuadraticExpressionsModel quadraticModel; LinearExpressionsModel linearModel; /** * * @param numberOfVars * greater than 6 */ void setupModels(int numberOfVars) { if (numberOfVars < 6) { throw new IllegalArgumentException("numberOfVars must be >= 6 !!!"); } // // variables // vars = new Variable[numberOfVars]; vars[0] = new Variable("x0").lower(new BigDecimal(00.0)).upper(new BigDecimal(15.0)); vars[1] = new Variable("x1").lower(new BigDecimal(17.0)).upper(new BigDecimal(27.0)); vars[2] = new Variable("x2").lower(new BigDecimal(19.0)).upper(new BigDecimal(34.0)); vars[3] = new Variable("x3").lower(new BigDecimal(25.0)).upper(new BigDecimal(48.0)); vars[4] = new Variable("x4").lower(new BigDecimal(05.0)).upper(new BigDecimal(18.0)); vars[5] = new Variable("x5").lower(new BigDecimal(02.0)).upper(new BigDecimal(09.0)); for (int i = 6; i < numberOfVars; ++i) { vars[i] = new Variable("x" + i).level(BigMath.ZERO); } // // minimize distance to this point // point = new BigDecimal[numberOfVars]; point[0] = new BigDecimal(1.0); point[1] = new BigDecimal(25.0); point[2] = new BigDecimal(33.0); point[3] = new BigDecimal(29.0); point[4] = new BigDecimal(9.0); point[5] = new BigDecimal(2.0); for (int i = 6; i < numberOfVars; ++i) { point[i] = new BigDecimal(0.0); } // // models // linearModel = new LinearExpressionsModel(vars); quadraticModel = new QuadraticExpressionsModel(vars); // // objective function - only assigned to the quadratic model // { Expression e = quadraticModel.addOffsetExpression("objective", Arrays.asList(point)); e.setContributionWeight(BigMath.HALF); } // // sum(xi) = 100.0 // { Expression e = quadraticModel.addSimpleWeightExpression("sum(xi) = 100.0"); e.level(BigMath.HUNDRED); e = linearModel.addSimpleWeightExpression("sum(xi) = 100.0"); e.level(BigMath.HUNDRED); } // // x1 + x2 <= 45 // { Expression e = quadraticModel.addEmptyLinearExpression("x1 + x2 <= 45.0"); e.setLinearFactor(1, BigMath.ONE); e.setLinearFactor(2, BigMath.ONE); e.lower(BigMath.ZERO).upper(new BigDecimal(45.0)); e = linearModel.addEmptyLinearExpression("x1 + x2 <= 45.0"); e.setLinearFactor(1, BigMath.ONE); e.setLinearFactor(2, BigMath.ONE); e.lower(BigMath.ZERO).upper(new BigDecimal(45.0)); } // // x4 - 2*x5 = 0 // { Expression e = quadraticModel.addEmptyLinearExpression("x4 - 2*x5 = 0"); e.setLinearFactor(4, BigMath.ONE); e.setLinearFactor(5, BigMath.TWO.negate()); e.level(BigMath.ZERO); e = linearModel.addEmptyLinearExpression("x4 - 2*x5 = 0"); e.setLinearFactor(4, BigMath.ONE); e.setLinearFactor(5, BigMath.TWO.negate()); e.level(BigMath.ZERO); } } @Test public void test1() { setupModels(6); MatrixStore<Double> initialIterationPoint = solveLinear(); Assert.assertTrue(initialIterationPoint != null); MatrixStore<Double> solution = solveQuadratic(initialIterationPoint); Assert.assertTrue(solution != null); } @Test public void test2() { setupModels(6); vars[3].level(new BigDecimal(40.0)); vars[4].level(new BigDecimal(18.0)); MatrixStore<Double> initialIterationPoint = solveLinear(); Assert.assertFalse(initialIterationPoint != null); MatrixStore<Double> solution = solveQuadratic(initialIterationPoint); Assert.assertFalse(solution != null); } @Test public void test3() { setupModels(6); vars[3].level(new BigDecimal(48.0)); vars[4].level(new BigDecimal(18.0)); vars[5].level(new BigDecimal(5.0)); MatrixStore<Double> initialIterationPoint = solveLinear(); Assert.assertFalse(initialIterationPoint != null); MatrixStore<Double> solution = solveQuadratic(initialIterationPoint); Assert.assertFalse(solution != null); } @Test public void test4() { setupModels(42); MatrixStore<Double> initialIterationPoint = solveLinear(); Assert.assertTrue(initialIterationPoint != null); MatrixStore<Double> solution = solveQuadratic(initialIterationPoint); Assert.assertTrue(solution != null); } @Test public void test5() { setupModels(42); vars[3].level(new BigDecimal(40.0)); vars[4].level(new BigDecimal(18.0)); MatrixStore<Double> initialIterationPoint = solveLinear(); Assert.assertFalse(initialIterationPoint != null); MatrixStore<Double> solution = solveQuadratic(initialIterationPoint); Assert.assertFalse(solution != null); } @Test public void test6() { setupModels(42); vars[3].level(new BigDecimal(40.0)); vars[4].level(new BigDecimal(18.0)); vars[5].level(new BigDecimal(5.0)); MatrixStore<Double> initialIterationPoint = solveLinear(); Assert.assertFalse(initialIterationPoint != null); MatrixStore<Double> solution = solveQuadratic(initialIterationPoint); Assert.assertFalse(solution != null); } MatrixStore<Double> solveLinear() { final LinearSolver solver = new LinearSolver.Builder(linearModel).build(); final Result result = solver.solve(); if (result.getSolution() != null) { if (result.getState().isNotLessThan(State.FEASIBLE)) { if (linearModel.validateSolution(result.getSolution(), solver.options.solutionContext)) { return result.getSolution().toPrimitiveStore(); } } } return null; } MatrixStore<Double> solveQuadratic(final MatrixStore<Double> initialIterationPoint) { if (initialIterationPoint == null) { return null; } final QuadraticSolver solver = new QuadraticSolver.Builder(quadraticModel).build(); final Result result = solver.solve(); if (result.getSolution() != null) { final boolean validated = quadraticModel.validateSolution(result.getSolution(), solver.options.solutionContext); if (result.getState().isNotLessThan(State.FEASIBLE)) { final String message = "State: " + result.getState() + ", validated: " + validated; Assert.assertTrue(message, validated); if (validated) { return result.getSolution().toPrimitiveStore(); } } else { final String message = "State: " + result.getState() + ", validated: " + validated; Assert.assertFalse(message, validated); } } else { Assert.assertFalse("No solution but state FEASIBLE", result.getState().isNotLessThan(State.FEASIBLE)); } return null; } } Thanks, Pierre "Anders Peterson" <an...@op...> 16.11.2009 22:10 Please respond to oja...@li... To oja...@li... cc Subject Re: [ojAlgo-user] QuadraticSolver with unsatisfiable constraints Warning: this message has been delivered from outside the Pictet Group. If the content is related to banking matters, please apply the same procedure as for an incoming Fax or letter. Attention: Ce message provient de l'extérieur du Groupe Pictet. S'il possède un caractère bancaire, veuillez le traiter de la même manière qu'un Fax ou une lettre entrante. I modified the solve-method to be void solve() { final QuadraticSolver solver = new QuadraticSolver.Builder(model).build(); final Result result = solver.solve(); if (result.getSolution() != null) { final boolean validated = model.validateSolution(result.getSolution(), solver.options.solutionContext); if (result.getState().isNotLessThan(State.FEASIBLE)) { final String message = "State: " + result.getState() + ", validated: " + validated; Assert.assertTrue(message, validated); } else { final String message = "State: " + result.getState() + ", validated: " + validated; Assert.assertFalse(message, validated); } } else { Assert.assertFalse("No solution but state FEASIBLE", result.getState().isNotLessThan(State.FEASIBLE)); } } Does that seem correct to you? Then, when I run the tests I only get a failure (failed test) with test "2", and I get no error (like an NPE). Tests "2" and "5" takes very long - long enough to suggest that there is something the algorithm can't handle. I see no problem with test "1", "3", "4" or "6". Do you agree with this? Have you tried setting different contexts for "options.solutionContext"? I don't believe the "options.problemContext" affect anything with the QuadraticSolver (yet). /Anders On 16 nov 2009, at 16.30, Pierre AUSLAENDER wrote: > > Sure, here it is (and sorry, the NPE was not in the SVD, but in the validateSolution) > > test1: OK > test2: KO with STATE = FAILED, VALIDATION = TRUE > test3: KO with NPE in ExpressionBasedModel.validateSolution > test4: OK > test5: KO with STATE = OPTIMAL, VALIDATION = FALSE > test6: KO with NPE in ExpressionBasedModel.validateSolution > > /* > * (c) Copyright Pictet & Cie 2009 > */ > package com.pictet.pam.bam.test; > > import java.math.BigDecimal; > import java.util.Arrays; > > import org.junit.Assert; > import org.junit.Test; > import org.ojalgo.constant.BigMath; > import org.ojalgo.optimisation.Expression; > import org.ojalgo.optimisation.State; > import org.ojalgo.optimisation.Variable; > import org.ojalgo.optimisation.OptimisationSolver.Result; > import org.ojalgo.optimisation.quadratic.QuadraticExpressionsModel; > import org.ojalgo.optimisation.quadratic.QuadraticSolver; > > /** > * @author pauslaender > * > */ > public class OptimisationSolverTest { > > Variable[] vars; > BigDecimal[] point; > QuadraticExpressionsModel model; > > /** > * > * @param numberOfVars greater than 6 > */ > void setupModel(int numberOfVars) { > if (numberOfVars < 6) { > throw new IllegalArgumentException("numberOfVars must be >= 6 !!!"); > } > // > // variables > // > vars = new Variable[numberOfVars]; > vars[0] = new Variable("x0").lower(new BigDecimal(00.0)).upper(new BigDecimal(15.0)); > vars[1] = new Variable("x1").lower(new BigDecimal(17.0)).upper(new BigDecimal(27.0)); > vars[2] = new Variable("x2").lower(new BigDecimal(19.0)).upper(new BigDecimal(34.0)); > vars[3] = new Variable("x3").lower(new BigDecimal(25.0)).upper(new BigDecimal(48.0)); > vars[4] = new Variable("x4").lower(new BigDecimal(05.0)).upper(new BigDecimal(18.0)); > vars[5] = new Variable("x5").lower(new BigDecimal(02.0)).upper(new BigDecimal(09.0)); > for (int i = 6; i < numberOfVars; ++i) { > vars[i] = new Variable("x" + i).level(BigMath.ZERO); > } > // > // minimise distance to this point > // > point = new BigDecimal[numberOfVars]; > point[0] = new BigDecimal(1.0); > point[1] = new BigDecimal(25.0); > point[2] = new BigDecimal(33.0); > point[3] = new BigDecimal(29.0); > point[4] = new BigDecimal(9.0); > point[5] = new BigDecimal(2.0); > for (int i = 6; i < numberOfVars; ++i) { > point[i] = new BigDecimal(0.0); > } > // > // model > // > model = new QuadraticExpressionsModel(vars); > // > // objective function > // > { > Expression e = model.addOffsetExpression("objective", Arrays.asList(point)); > e.setContributionWeight(BigMath.HALF); > } > // > // sum(xi) = 100.0 > // > { > Expression e = model.addSimpleWeightExpression("sum(xi) = 100.0"); > e.level(BigMath.HUNDRED); > } > // > // x1 + x2 <= 45 > // > { > Expression e = model.addEmptyLinearExpression("x1 + x2 <= 45.0"); > e.setLinearFactor(1, BigMath.ONE); > e.setLinearFactor(2, BigMath.ONE); > e.lower(BigMath.ZERO).upper(new BigDecimal(45.0)); > } > // > // x4 - 2*x5 = 0 > // > { > Expression e = model.addEmptyLinearExpression("x4 - 2*x5 = 0"); > e.setLinearFactor(4, BigMath.ONE); > e.setLinearFactor(5, BigMath.TWO.negate()); > e.level(BigMath.ZERO); > } > } > > @Test > public void test1() { > setupModel(6); > solve(); > } > > @Test > public void test2() { > setupModel(6); > vars[3].level(new BigDecimal(40.0)); > vars[4].level(new BigDecimal(18.0)); > solve(); > } > > @Test > public void test3() { > setupModel(6); > vars[3].level(new BigDecimal(48.0)); > vars[4].level(new BigDecimal(18.0)); > vars[5].level(new BigDecimal(5.0)); > solve(); > } > > @Test > public void test4() { > setupModel(42); > solve(); > } > > @Test > public void test5() { > setupModel(42); > vars[3].level(new BigDecimal(40.0)); > vars[4].level(new BigDecimal(18.0)); > solve(); > } > > @Test > public void test6() { > setupModel(42); > vars[3].level(new BigDecimal(40.0)); > vars[4].level(new BigDecimal(18.0)); > vars[5].level(new BigDecimal(5.0)); > solve(); > } > > void solve() { > QuadraticSolver solver = new QuadraticSolver.Builder(model).build(); > // QuadraticSolver.DEBUG = true; > // System.out.println(model); > Result result = solver.solve(); > // System.out.println(result); > // System.out.println(result.getSolution().toListOfElements()); > boolean validated = model.validateSolution(result.getSolution(), solver.options.solutionContext); > if (result.getState() == State.OPTIMAL) { > String message = "State: " + result.getState() + ", validated: " + validated; > Assert.assertTrue(message, validated); > } else { > String message = "State: " + result.getState() + ", validated: " + validated; > Assert.assertFalse(message, validated); > } > } > > } > > > > "Anders Peterson" <an...@op...> > 16.11.2009 15:42 > Please respond to > oja...@li... > > To > oja...@li... > cc > Subject > Re: [ojAlgo-user] QuadraticSolver with unsatisfiable constraints > > > > Warning: this message has been delivered from outside the Pictet Group. If the content is related to banking matters, please apply the same procedure as for an incoming Fax or letter. > Attention: Ce message provient de l'extérieur du Groupe Pictet. S'il possède un caractère bancaire, veuillez le traiter de la même manière qu'un Fax ou une lettre entrante. > > > > > > Can you perhaps give me 2 test cases; 1 for the NPE case and 1 for the > incorrect state case. /Anders > > > On 16 nov 2009, at 14.33, Pierre AUSLAENDER wrote: > > > > > Hello Anders, > > > > Thanks a lot for the swift reply. > > > > I 've played with the latest snapshot and here are my findings: > > - Most of the times now, when the constraints cannot be satisfied, > > the solver terminates with the state FAILED, which is what one would > > expect, albeit after a great lot of iterations. > > - Sometimes however, it terminates with the state OPTIMAL, and the > > model validation returns FALSE, same as before I took the snapshot. > > - Sometimes as well, it throws a NPE in the SVD (after saying that > > LU is not solvable). > > > > I'm trying to build a simple JUnit and send it to you. > > > > Kind regards, > > Pierre > > > > > > > > "Anders Peterson" <an...@op...> > > 16.11.2009 12:57 > > Please respond to > > oja...@li... > > > > > > To > > oja...@li... > > cc > > Subject > > Re: [ojAlgo-user] QuadraticSolver with unsatisfiable constraints > > > > > > > > Warning: this message has been delivered from outside the Pictet > > Group. If the content is related to banking matters, please apply > > the same procedure as for an incoming Fax or letter. > > Attention: Ce message provient de l'extérieur du Groupe Pictet. S'il > > possède un caractère bancaire, veuillez le traiter de la même > > manière qu'un Fax ou une lettre entrante. > > > > > > > > > > > > On 16 nov 2009, at 11.06, Pierre AUSLAENDER wrote: > > > > > Hello, > > > > > > Thank you for this library, which is a joy to use - once I got over > > > the lack of documentation. > > > > > > I'm using the QuadraticSolver (ActiveSetSolver) on a least squares > > > minimisation problem, under a set of linear constraints. When the > > > constraints can be satisfied, it works perfectly. However, when the > > > constraints cannot be satisfied, the solver still returns with an > > > OPTIMAL state after a few iterations. I was puzzled until I > > > discovered the QuadraticSolver.validateSolution(...) methods, which > > > I can use to effectively test the solution against the constraints. > > > > > > Is this the way it is supposed to work, or did I do something wrong? > > > I would have expected the solver to FAIL instead. > > > > > > I recently fixed a couple of bugs related to this. Get the latest > > snapshot download from Source Forge, and try if that solves your > > problem. If it doesn't I'd like you to give me (junit) test case. > > > > > > > As an aside question, there is the following JavaDoc comment on > > > ExpressionBasedModel.addConstraint(final String aName, ....): Note > > > that only the first constraint you add will be used by the solver. > > > What does this comment mean? Is it simply related to the uniqueness > > > of the constraint name, or has it wider implications? > > > > > > That comment should have been removed. It was only relevant for the > > now removed/replaced KnapsckSolver. That solver could only use 1 > > constraint - the first one you added. I'll correct the javadoc comment > > now. > > > > > > /Anders > > > > > > > > > Thank you, > > > Pierre > > > > > > ________________________________________________________________ > > > > > > PAM S.A Geneva Tel. +41 (0)58 323 3333 > > > 60, route des Acacias Fax +41 (0)58 323 2324 > > > CH-1211 GENEVE 73 http://www.pictet.com/ > > > ________________________________________________________________ > > > > > > This document should only be read by those persons to whom it is > > > addressed and is not intended to be relied upon by any person > > > without subsequent written confirmation of its contents. If you > > > have received this e-mail message in error, please destroy it > > > and delete it from your computer. > > > Any form of reproduction, dissemination, copying, disclosure, > > > modification, distribution and/or publication of this E-mail > > > message is strictly prohibited. > > > ________________________________________________________________ > > > > > > Pictet reserves the right to monitor and record business and > > > personal communications. By responding to an email or call from > > > a Pictet employee you are taken to have consented to such > > > monitoring and recording. > > > ________________________________________________________________ > > > > > > > > ------------------------------------------------------------------------------ > > > Let Crystal Reports handle the reporting - Free Crystal Reports 2008 > > > 30-Day > > > trial. Simplify your report design, integration and deployment - and > > > focus on > > > what you do best, core application coding. Discover what's new with > > > Crystal Reports now. http://p.sf.net/sfu/bobj-july_______________________________________________ > > > ojAlgo-user mailing list > > > ojA...@li... > > > https://lists.sourceforge.net/lists/listinfo/ojalgo-user > > > > > > ------------------------------------------------------------------------------ > > Let Crystal Reports handle the reporting - Free Crystal Reports 2008 > > 30-Day > > trial. Simplify your report design, integration and deployment - and > > focus on > > what you do best, core application coding. Discover what's new with > > Crystal Reports now. http://p.sf.net/sfu/bobj-july > > _______________________________________________ > > ojAlgo-user mailing list > > ojA...@li... > > https://lists.sourceforge.net/lists/listinfo/ojalgo-user > > > > > > > > ________________________________________________________________ > > > > PAM S.A Geneva Tel. +41 (0)58 323 3333 > > 60, route des Acacias Fax +41 (0)58 323 2324 > > CH-1211 GENEVE 73 http://www.pictet.com/ > > ________________________________________________________________ > > > > This document should only be read by those persons to whom it is > > addressed and is not intended to be relied upon by any person > > without subsequent written confirmation of its contents. If you > > have received this e-mail message in error, please destroy it > > and delete it from your computer. > > Any form of reproduction, dissemination, copying, disclosure, > > modification, distribution and/or publication of this E-mail > > message is strictly prohibited. > > ________________________________________________________________ > > > > Pictet reserves the right to monitor and record business and > > personal communications. By responding to an email or call from > > a Pictet employee you are taken to have consented to such > > monitoring and recording. > > ________________________________________________________________ > > > > ------------------------------------------------------------------------------ > > Let Crystal Reports handle the reporting - Free Crystal Reports 2008 > > 30-Day > > trial. Simplify your report design, integration and deployment - and > > focus on > > what you do best, core application coding. Discover what's new with > > Crystal Reports now. http://p.sf.net/sfu/bobj-july_______________________________________________ > > ojAlgo-user mailing list > > ojA...@li... > > https://lists.sourceforge.net/lists/listinfo/ojalgo-user > > > ------------------------------------------------------------------------------ > Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day > trial. Simplify your report design, integration and deployment - and focus on > what you do best, core application coding. Discover what's new with > Crystal Reports now. http://p.sf.net/sfu/bobj-july > _______________________________________________ > ojAlgo-user mailing list > ojA...@li... > https://lists.sourceforge.net/lists/listinfo/ojalgo-user > > > > ________________________________________________________________ > > PAM S.A Geneva Tel. +41 (0)58 323 3333 > 60, route des Acacias Fax +41 (0)58 323 2324 > CH-1211 GENEVE 73 http://www.pictet.com/ > ________________________________________________________________ > > This document should only be read by those persons to whom it is > addressed and is not intended to be relied upon by any person > without subsequent written confirmation of its contents. If you > have received this e-mail message in error, please destroy it > and delete it from your computer. > Any form of reproduction, dissemination, copying, disclosure, > modification, distribution and/or publication of this E-mail > message is strictly prohibited. > ________________________________________________________________ > > Pictet reserves the right to monitor and record business and > personal communications. By responding to an email or call from > a Pictet employee you are taken to have consented to such > monitoring and recording. > ________________________________________________________________ > > > ------------------------------------------------------------------------------ > Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day > trial. Simplify your report design, integration and deployment - and focus on > what you do best, core application coding. Discover what's new with > Crystal Reports now. http://p.sf.net/sfu/bobj-july_______________________________________________ > ojAlgo-user mailing list > ojA...@li... > https://lists.sourceforge.net/lists/listinfo/ojalgo-user ------------------------------------------------------------------------------ Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day trial. Simplify your report design, integration and deployment - and focus on what you do best, core application coding. Discover what's new with Crystal Reports now. http://p.sf.net/sfu/bobj-july _______________________________________________ ojAlgo-user mailing list ojA...@li... https://lists.sourceforge.net/lists/listinfo/ojalgo-user ________________________________________________________________ PAM S.A Geneva Tel. +41 (0)58 323 3333 60, route des Acacias Fax +41 (0)58 323 2324 CH-1211 GENEVE 73 http://www.pictet.com/ ________________________________________________________________ This document should only be read by those persons to whom it is addressed and is not intended to be relied upon by any person without subsequent written confirmation of its contents. If you have received this e-mail message in error, please destroy it and delete it from your computer. Any form of reproduction, dissemination, copying, disclosure, modification, distribution and/or publication of this E-mail message is strictly prohibited. ________________________________________________________________ Pictet reserves the right to monitor and record business and personal communications. By responding to an email or call from a Pictet employee you are taken to have consented to such monitoring and recording. ________________________________________________________________ |
From: Anders P. <an...@op...> - 2009-11-16 21:10:38
|
I modified the solve-method to be void solve() { final QuadraticSolver solver = new QuadraticSolver.Builder(model).build(); final Result result = solver.solve(); if (result.getSolution() != null) { final boolean validated = model.validateSolution(result.getSolution(), solver.options.solutionContext); if (result.getState().isNotLessThan(State.FEASIBLE)) { final String message = "State: " + result.getState() + ", validated: " + validated; Assert.assertTrue(message, validated); } else { final String message = "State: " + result.getState() + ", validated: " + validated; Assert.assertFalse(message, validated); } } else { Assert.assertFalse("No solution but state FEASIBLE", result.getState().isNotLessThan(State.FEASIBLE)); } } Does that seem correct to you? Then, when I run the tests I only get a failure (failed test) with test "2", and I get no error (like an NPE). Tests "2" and "5" takes very long - long enough to suggest that there is something the algorithm can't handle. I see no problem with test "1", "3", "4" or "6". Do you agree with this? Have you tried setting different contexts for "options.solutionContext"? I don't believe the "options.problemContext" affect anything with the QuadraticSolver (yet). /Anders On 16 nov 2009, at 16.30, Pierre AUSLAENDER wrote: > > Sure, here it is (and sorry, the NPE was not in the SVD, but in the validateSolution) > > test1: OK > test2: KO with STATE = FAILED, VALIDATION = TRUE > test3: KO with NPE in ExpressionBasedModel.validateSolution > test4: OK > test5: KO with STATE = OPTIMAL, VALIDATION = FALSE > test6: KO with NPE in ExpressionBasedModel.validateSolution > > /* > * (c) Copyright Pictet & Cie 2009 > */ > package com.pictet.pam.bam.test; > > import java.math.BigDecimal; > import java.util.Arrays; > > import org.junit.Assert; > import org.junit.Test; > import org.ojalgo.constant.BigMath; > import org.ojalgo.optimisation.Expression; > import org.ojalgo.optimisation.State; > import org.ojalgo.optimisation.Variable; > import org.ojalgo.optimisation.OptimisationSolver.Result; > import org.ojalgo.optimisation.quadratic.QuadraticExpressionsModel; > import org.ojalgo.optimisation.quadratic.QuadraticSolver; > > /** > * @author pauslaender > * > */ > public class OptimisationSolverTest { > > Variable[] vars; > BigDecimal[] point; > QuadraticExpressionsModel model; > > /** > * > * @param numberOfVars greater than 6 > */ > void setupModel(int numberOfVars) { > if (numberOfVars < 6) { > throw new IllegalArgumentException("numberOfVars must be >= 6 !!!"); > } > // > // variables > // > vars = new Variable[numberOfVars]; > vars[0] = new Variable("x0").lower(new BigDecimal(00.0)).upper(new BigDecimal(15.0)); > vars[1] = new Variable("x1").lower(new BigDecimal(17.0)).upper(new BigDecimal(27.0)); > vars[2] = new Variable("x2").lower(new BigDecimal(19.0)).upper(new BigDecimal(34.0)); > vars[3] = new Variable("x3").lower(new BigDecimal(25.0)).upper(new BigDecimal(48.0)); > vars[4] = new Variable("x4").lower(new BigDecimal(05.0)).upper(new BigDecimal(18.0)); > vars[5] = new Variable("x5").lower(new BigDecimal(02.0)).upper(new BigDecimal(09.0)); > for (int i = 6; i < numberOfVars; ++i) { > vars[i] = new Variable("x" + i).level(BigMath.ZERO); > } > // > // minimise distance to this point > // > point = new BigDecimal[numberOfVars]; > point[0] = new BigDecimal(1.0); > point[1] = new BigDecimal(25.0); > point[2] = new BigDecimal(33.0); > point[3] = new BigDecimal(29.0); > point[4] = new BigDecimal(9.0); > point[5] = new BigDecimal(2.0); > for (int i = 6; i < numberOfVars; ++i) { > point[i] = new BigDecimal(0.0); > } > // > // model > // > model = new QuadraticExpressionsModel(vars); > // > // objective function > // > { > Expression e = model.addOffsetExpression("objective", Arrays.asList(point)); > e.setContributionWeight(BigMath.HALF); > } > // > // sum(xi) = 100.0 > // > { > Expression e = model.addSimpleWeightExpression("sum(xi) = 100.0"); > e.level(BigMath.HUNDRED); > } > // > // x1 + x2 <= 45 > // > { > Expression e = model.addEmptyLinearExpression("x1 + x2 <= 45.0"); > e.setLinearFactor(1, BigMath.ONE); > e.setLinearFactor(2, BigMath.ONE); > e.lower(BigMath.ZERO).upper(new BigDecimal(45.0)); > } > // > // x4 - 2*x5 = 0 > // > { > Expression e = model.addEmptyLinearExpression("x4 - 2*x5 = 0"); > e.setLinearFactor(4, BigMath.ONE); > e.setLinearFactor(5, BigMath.TWO.negate()); > e.level(BigMath.ZERO); > } > } > > @Test > public void test1() { > setupModel(6); > solve(); > } > > @Test > public void test2() { > setupModel(6); > vars[3].level(new BigDecimal(40.0)); > vars[4].level(new BigDecimal(18.0)); > solve(); > } > > @Test > public void test3() { > setupModel(6); > vars[3].level(new BigDecimal(48.0)); > vars[4].level(new BigDecimal(18.0)); > vars[5].level(new BigDecimal(5.0)); > solve(); > } > > @Test > public void test4() { > setupModel(42); > solve(); > } > > @Test > public void test5() { > setupModel(42); > vars[3].level(new BigDecimal(40.0)); > vars[4].level(new BigDecimal(18.0)); > solve(); > } > > @Test > public void test6() { > setupModel(42); > vars[3].level(new BigDecimal(40.0)); > vars[4].level(new BigDecimal(18.0)); > vars[5].level(new BigDecimal(5.0)); > solve(); > } > > void solve() { > QuadraticSolver solver = new QuadraticSolver.Builder(model).build(); > // QuadraticSolver.DEBUG = true; > // System.out.println(model); > Result result = solver.solve(); > // System.out.println(result); > // System.out.println(result.getSolution().toListOfElements()); > boolean validated = model.validateSolution(result.getSolution(), solver.options.solutionContext); > if (result.getState() == State.OPTIMAL) { > String message = "State: " + result.getState() + ", validated: " + validated; > Assert.assertTrue(message, validated); > } else { > String message = "State: " + result.getState() + ", validated: " + validated; > Assert.assertFalse(message, validated); > } > } > > } > > > > "Anders Peterson" <an...@op...> > 16.11.2009 15:42 > Please respond to > oja...@li... > > To > oja...@li... > cc > Subject > Re: [ojAlgo-user] QuadraticSolver with unsatisfiable constraints > > > > Warning: this message has been delivered from outside the Pictet Group. If the content is related to banking matters, please apply the same procedure as for an incoming Fax or letter. > Attention: Ce message provient de l'extérieur du Groupe Pictet. S'il possède un caractère bancaire, veuillez le traiter de la même manière qu'un Fax ou une lettre entrante. > > > > > > Can you perhaps give me 2 test cases; 1 for the NPE case and 1 for the > incorrect state case. /Anders > > > On 16 nov 2009, at 14.33, Pierre AUSLAENDER wrote: > > > > > Hello Anders, > > > > Thanks a lot for the swift reply. > > > > I 've played with the latest snapshot and here are my findings: > > - Most of the times now, when the constraints cannot be satisfied, > > the solver terminates with the state FAILED, which is what one would > > expect, albeit after a great lot of iterations. > > - Sometimes however, it terminates with the state OPTIMAL, and the > > model validation returns FALSE, same as before I took the snapshot. > > - Sometimes as well, it throws a NPE in the SVD (after saying that > > LU is not solvable). > > > > I'm trying to build a simple JUnit and send it to you. > > > > Kind regards, > > Pierre > > > > > > > > "Anders Peterson" <an...@op...> > > 16.11.2009 12:57 > > Please respond to > > oja...@li... > > > > > > To > > oja...@li... > > cc > > Subject > > Re: [ojAlgo-user] QuadraticSolver with unsatisfiable constraints > > > > > > > > Warning: this message has been delivered from outside the Pictet > > Group. If the content is related to banking matters, please apply > > the same procedure as for an incoming Fax or letter. > > Attention: Ce message provient de l'extérieur du Groupe Pictet. S'il > > possède un caractère bancaire, veuillez le traiter de la même > > manière qu'un Fax ou une lettre entrante. > > > > > > > > > > > > On 16 nov 2009, at 11.06, Pierre AUSLAENDER wrote: > > > > > Hello, > > > > > > Thank you for this library, which is a joy to use - once I got over > > > the lack of documentation. > > > > > > I'm using the QuadraticSolver (ActiveSetSolver) on a least squares > > > minimisation problem, under a set of linear constraints. When the > > > constraints can be satisfied, it works perfectly. However, when the > > > constraints cannot be satisfied, the solver still returns with an > > > OPTIMAL state after a few iterations. I was puzzled until I > > > discovered the QuadraticSolver.validateSolution(...) methods, which > > > I can use to effectively test the solution against the constraints. > > > > > > Is this the way it is supposed to work, or did I do something wrong? > > > I would have expected the solver to FAIL instead. > > > > > > I recently fixed a couple of bugs related to this. Get the latest > > snapshot download from Source Forge, and try if that solves your > > problem. If it doesn't I'd like you to give me (junit) test case. > > > > > > > As an aside question, there is the following JavaDoc comment on > > > ExpressionBasedModel.addConstraint(final String aName, ....): Note > > > that only the first constraint you add will be used by the solver. > > > What does this comment mean? Is it simply related to the uniqueness > > > of the constraint name, or has it wider implications? > > > > > > That comment should have been removed. It was only relevant for the > > now removed/replaced KnapsckSolver. That solver could only use 1 > > constraint - the first one you added. I'll correct the javadoc comment > > now. > > > > > > /Anders > > > > > > > > > Thank you, > > > Pierre > > > > > > ________________________________________________________________ > > > > > > PAM S.A Geneva Tel. +41 (0)58 323 3333 > > > 60, route des Acacias Fax +41 (0)58 323 2324 > > > CH-1211 GENEVE 73 http://www.pictet.com/ > > > ________________________________________________________________ > > > > > > This document should only be read by those persons to whom it is > > > addressed and is not intended to be relied upon by any person > > > without subsequent written confirmation of its contents. If you > > > have received this e-mail message in error, please destroy it > > > and delete it from your computer. > > > Any form of reproduction, dissemination, copying, disclosure, > > > modification, distribution and/or publication of this E-mail > > > message is strictly prohibited. > > > ________________________________________________________________ > > > > > > Pictet reserves the right to monitor and record business and > > > personal communications. By responding to an email or call from > > > a Pictet employee you are taken to have consented to such > > > monitoring and recording. > > > ________________________________________________________________ > > > > > > > > ------------------------------------------------------------------------------ > > > Let Crystal Reports handle the reporting - Free Crystal Reports 2008 > > > 30-Day > > > trial. Simplify your report design, integration and deployment - and > > > focus on > > > what you do best, core application coding. Discover what's new with > > > Crystal Reports now. http://p.sf.net/sfu/bobj-july_______________________________________________ > > > ojAlgo-user mailing list > > > ojA...@li... > > > https://lists.sourceforge.net/lists/listinfo/ojalgo-user > > > > > > ------------------------------------------------------------------------------ > > Let Crystal Reports handle the reporting - Free Crystal Reports 2008 > > 30-Day > > trial. Simplify your report design, integration and deployment - and > > focus on > > what you do best, core application coding. Discover what's new with > > Crystal Reports now. http://p.sf.net/sfu/bobj-july > > _______________________________________________ > > ojAlgo-user mailing list > > ojA...@li... > > https://lists.sourceforge.net/lists/listinfo/ojalgo-user > > > > > > > > ________________________________________________________________ > > > > PAM S.A Geneva Tel. +41 (0)58 323 3333 > > 60, route des Acacias Fax +41 (0)58 323 2324 > > CH-1211 GENEVE 73 http://www.pictet.com/ > > ________________________________________________________________ > > > > This document should only be read by those persons to whom it is > > addressed and is not intended to be relied upon by any person > > without subsequent written confirmation of its contents. If you > > have received this e-mail message in error, please destroy it > > and delete it from your computer. > > Any form of reproduction, dissemination, copying, disclosure, > > modification, distribution and/or publication of this E-mail > > message is strictly prohibited. > > ________________________________________________________________ > > > > Pictet reserves the right to monitor and record business and > > personal communications. By responding to an email or call from > > a Pictet employee you are taken to have consented to such > > monitoring and recording. > > ________________________________________________________________ > > > > ------------------------------------------------------------------------------ > > Let Crystal Reports handle the reporting - Free Crystal Reports 2008 > > 30-Day > > trial. Simplify your report design, integration and deployment - and > > focus on > > what you do best, core application coding. Discover what's new with > > Crystal Reports now. http://p.sf.net/sfu/bobj-july_______________________________________________ > > ojAlgo-user mailing list > > ojA...@li... > > https://lists.sourceforge.net/lists/listinfo/ojalgo-user > > > ------------------------------------------------------------------------------ > Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day > trial. Simplify your report design, integration and deployment - and focus on > what you do best, core application coding. Discover what's new with > Crystal Reports now. http://p.sf.net/sfu/bobj-july > _______________________________________________ > ojAlgo-user mailing list > ojA...@li... > https://lists.sourceforge.net/lists/listinfo/ojalgo-user > > > > ________________________________________________________________ > > PAM S.A Geneva Tel. +41 (0)58 323 3333 > 60, route des Acacias Fax +41 (0)58 323 2324 > CH-1211 GENEVE 73 http://www.pictet.com/ > ________________________________________________________________ > > This document should only be read by those persons to whom it is > addressed and is not intended to be relied upon by any person > without subsequent written confirmation of its contents. If you > have received this e-mail message in error, please destroy it > and delete it from your computer. > Any form of reproduction, dissemination, copying, disclosure, > modification, distribution and/or publication of this E-mail > message is strictly prohibited. > ________________________________________________________________ > > Pictet reserves the right to monitor and record business and > personal communications. By responding to an email or call from > a Pictet employee you are taken to have consented to such > monitoring and recording. > ________________________________________________________________ > > > ------------------------------------------------------------------------------ > Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day > trial. Simplify your report design, integration and deployment - and focus on > what you do best, core application coding. Discover what's new with > Crystal Reports now. http://p.sf.net/sfu/bobj-july_______________________________________________ > ojAlgo-user mailing list > ojA...@li... > https://lists.sourceforge.net/lists/listinfo/ojalgo-user |
From: Pierre A. <pau...@pi...> - 2009-11-16 15:32:04
|
Sure, here it is (and sorry, the NPE was not in the SVD, but in the validateSolution) test1: OK test2: KO with STATE = FAILED, VALIDATION = TRUE test3: KO with NPE in ExpressionBasedModel.validateSolution test4: OK test5: KO with STATE = OPTIMAL, VALIDATION = FALSE test6: KO with NPE in ExpressionBasedModel.validateSolution /* * (c) Copyright Pictet & Cie 2009 */ package com.pictet.pam.bam.test; import java.math.BigDecimal; import java.util.Arrays; import org.junit.Assert; import org.junit.Test; import org.ojalgo.constant.BigMath; import org.ojalgo.optimisation.Expression; import org.ojalgo.optimisation.State; import org.ojalgo.optimisation.Variable; import org.ojalgo.optimisation.OptimisationSolver.Result; import org.ojalgo.optimisation.quadratic.QuadraticExpressionsModel; import org.ojalgo.optimisation.quadratic.QuadraticSolver; /** * @author pauslaender * */ public class OptimisationSolverTest { Variable[] vars; BigDecimal[] point; QuadraticExpressionsModel model; /** * * @param numberOfVars greater than 6 */ void setupModel(int numberOfVars) { if (numberOfVars < 6) { throw new IllegalArgumentException("numberOfVars must be >= 6 !!!"); } // // variables // vars = new Variable[numberOfVars]; vars[0] = new Variable("x0").lower(new BigDecimal(00.0)).upper(new BigDecimal(15.0)); vars[1] = new Variable("x1").lower(new BigDecimal(17.0)).upper(new BigDecimal(27.0)); vars[2] = new Variable("x2").lower(new BigDecimal(19.0)).upper(new BigDecimal(34.0)); vars[3] = new Variable("x3").lower(new BigDecimal(25.0)).upper(new BigDecimal(48.0)); vars[4] = new Variable("x4").lower(new BigDecimal(05.0)).upper(new BigDecimal(18.0)); vars[5] = new Variable("x5").lower(new BigDecimal(02.0)).upper(new BigDecimal(09.0)); for (int i = 6; i < numberOfVars; ++i) { vars[i] = new Variable("x" + i).level(BigMath.ZERO); } // // minimise distance to this point // point = new BigDecimal[numberOfVars]; point[0] = new BigDecimal(1.0); point[1] = new BigDecimal(25.0); point[2] = new BigDecimal(33.0); point[3] = new BigDecimal(29.0); point[4] = new BigDecimal(9.0); point[5] = new BigDecimal(2.0); for (int i = 6; i < numberOfVars; ++i) { point[i] = new BigDecimal(0.0); } // // model // model = new QuadraticExpressionsModel(vars); // // objective function // { Expression e = model.addOffsetExpression("objective", Arrays.asList(point)); e.setContributionWeight(BigMath.HALF); } // // sum(xi) = 100.0 // { Expression e = model.addSimpleWeightExpression("sum(xi) = 100.0"); e.level(BigMath.HUNDRED); } // // x1 + x2 <= 45 // { Expression e = model.addEmptyLinearExpression("x1 + x2 <= 45.0"); e.setLinearFactor(1, BigMath.ONE); e.setLinearFactor(2, BigMath.ONE); e.lower(BigMath.ZERO).upper(new BigDecimal(45.0)); } // // x4 - 2*x5 = 0 // { Expression e = model.addEmptyLinearExpression("x4 - 2*x5 = 0"); e.setLinearFactor(4, BigMath.ONE); e.setLinearFactor(5, BigMath.TWO.negate()); e.level(BigMath.ZERO); } } @Test public void test1() { setupModel(6); solve(); } @Test public void test2() { setupModel(6); vars[3].level(new BigDecimal(40.0)); vars[4].level(new BigDecimal(18.0)); solve(); } @Test public void test3() { setupModel(6); vars[3].level(new BigDecimal(48.0)); vars[4].level(new BigDecimal(18.0)); vars[5].level(new BigDecimal(5.0)); solve(); } @Test public void test4() { setupModel(42); solve(); } @Test public void test5() { setupModel(42); vars[3].level(new BigDecimal(40.0)); vars[4].level(new BigDecimal(18.0)); solve(); } @Test public void test6() { setupModel(42); vars[3].level(new BigDecimal(40.0)); vars[4].level(new BigDecimal(18.0)); vars[5].level(new BigDecimal(5.0)); solve(); } void solve() { QuadraticSolver solver = new QuadraticSolver.Builder(model).build(); // QuadraticSolver.DEBUG = true; // System.out.println(model); Result result = solver.solve(); // System.out.println(result); // System.out.println(result.getSolution().toListOfElements()); boolean validated = model.validateSolution(result.getSolution(), solver.options.solutionContext); if (result.getState() == State.OPTIMAL) { String message = "State: " + result.getState() + ", validated: " + validated; Assert.assertTrue(message, validated); } else { String message = "State: " + result.getState() + ", validated: " + validated; Assert.assertFalse(message, validated); } } } "Anders Peterson" <an...@op...> 16.11.2009 15:42 Please respond to oja...@li... To oja...@li... cc Subject Re: [ojAlgo-user] QuadraticSolver with unsatisfiable constraints Warning: this message has been delivered from outside the Pictet Group. If the content is related to banking matters, please apply the same procedure as for an incoming Fax or letter. Attention: Ce message provient de l'extérieur du Groupe Pictet. S'il possède un caractère bancaire, veuillez le traiter de la même manière qu'un Fax ou une lettre entrante. Can you perhaps give me 2 test cases; 1 for the NPE case and 1 for the incorrect state case. /Anders On 16 nov 2009, at 14.33, Pierre AUSLAENDER wrote: > > Hello Anders, > > Thanks a lot for the swift reply. > > I 've played with the latest snapshot and here are my findings: > - Most of the times now, when the constraints cannot be satisfied, > the solver terminates with the state FAILED, which is what one would > expect, albeit after a great lot of iterations. > - Sometimes however, it terminates with the state OPTIMAL, and the > model validation returns FALSE, same as before I took the snapshot. > - Sometimes as well, it throws a NPE in the SVD (after saying that > LU is not solvable). > > I'm trying to build a simple JUnit and send it to you. > > Kind regards, > Pierre > > > > "Anders Peterson" <an...@op...> > 16.11.2009 12:57 > Please respond to > oja...@li... > > > To > oja...@li... > cc > Subject > Re: [ojAlgo-user] QuadraticSolver with unsatisfiable constraints > > > > Warning: this message has been delivered from outside the Pictet > Group. If the content is related to banking matters, please apply > the same procedure as for an incoming Fax or letter. > Attention: Ce message provient de l'extérieur du Groupe Pictet. S'il > possède un caractère bancaire, veuillez le traiter de la même > manière qu'un Fax ou une lettre entrante. > > > > > > On 16 nov 2009, at 11.06, Pierre AUSLAENDER wrote: > > > Hello, > > > > Thank you for this library, which is a joy to use - once I got over > > the lack of documentation. > > > > I'm using the QuadraticSolver (ActiveSetSolver) on a least squares > > minimisation problem, under a set of linear constraints. When the > > constraints can be satisfied, it works perfectly. However, when the > > constraints cannot be satisfied, the solver still returns with an > > OPTIMAL state after a few iterations. I was puzzled until I > > discovered the QuadraticSolver.validateSolution(...) methods, which > > I can use to effectively test the solution against the constraints. > > > > Is this the way it is supposed to work, or did I do something wrong? > > I would have expected the solver to FAIL instead. > > > I recently fixed a couple of bugs related to this. Get the latest > snapshot download from Source Forge, and try if that solves your > problem. If it doesn't I'd like you to give me (junit) test case. > > > > As an aside question, there is the following JavaDoc comment on > > ExpressionBasedModel.addConstraint(final String aName, ....): Note > > that only the first constraint you add will be used by the solver. > > What does this comment mean? Is it simply related to the uniqueness > > of the constraint name, or has it wider implications? > > > That comment should have been removed. It was only relevant for the > now removed/replaced KnapsckSolver. That solver could only use 1 > constraint - the first one you added. I'll correct the javadoc comment > now. > > > /Anders > > > > > Thank you, > > Pierre > > > > ________________________________________________________________ > > > > PAM S.A Geneva Tel. +41 (0)58 323 3333 > > 60, route des Acacias Fax +41 (0)58 323 2324 > > CH-1211 GENEVE 73 http://www.pictet.com/ > > ________________________________________________________________ > > > > This document should only be read by those persons to whom it is > > addressed and is not intended to be relied upon by any person > > without subsequent written confirmation of its contents. If you > > have received this e-mail message in error, please destroy it > > and delete it from your computer. > > Any form of reproduction, dissemination, copying, disclosure, > > modification, distribution and/or publication of this E-mail > > message is strictly prohibited. > > ________________________________________________________________ > > > > Pictet reserves the right to monitor and record business and > > personal communications. By responding to an email or call from > > a Pictet employee you are taken to have consented to such > > monitoring and recording. > > ________________________________________________________________ > > > > > ------------------------------------------------------------------------------ > > Let Crystal Reports handle the reporting - Free Crystal Reports 2008 > > 30-Day > > trial. Simplify your report design, integration and deployment - and > > focus on > > what you do best, core application coding. Discover what's new with > > Crystal Reports now. http://p.sf.net/sfu/bobj-july_______________________________________________ > > ojAlgo-user mailing list > > ojA...@li... > > https://lists.sourceforge.net/lists/listinfo/ojalgo-user > > > ------------------------------------------------------------------------------ > Let Crystal Reports handle the reporting - Free Crystal Reports 2008 > 30-Day > trial. Simplify your report design, integration and deployment - and > focus on > what you do best, core application coding. Discover what's new with > Crystal Reports now. http://p.sf.net/sfu/bobj-july > _______________________________________________ > ojAlgo-user mailing list > ojA...@li... > https://lists.sourceforge.net/lists/listinfo/ojalgo-user > > > > ________________________________________________________________ > > PAM S.A Geneva Tel. +41 (0)58 323 3333 > 60, route des Acacias Fax +41 (0)58 323 2324 > CH-1211 GENEVE 73 http://www.pictet.com/ > ________________________________________________________________ > > This document should only be read by those persons to whom it is > addressed and is not intended to be relied upon by any person > without subsequent written confirmation of its contents. If you > have received this e-mail message in error, please destroy it > and delete it from your computer. > Any form of reproduction, dissemination, copying, disclosure, > modification, distribution and/or publication of this E-mail > message is strictly prohibited. > ________________________________________________________________ > > Pictet reserves the right to monitor and record business and > personal communications. By responding to an email or call from > a Pictet employee you are taken to have consented to such > monitoring and recording. > ________________________________________________________________ > > ------------------------------------------------------------------------------ > Let Crystal Reports handle the reporting - Free Crystal Reports 2008 > 30-Day > trial. Simplify your report design, integration and deployment - and > focus on > what you do best, core application coding. Discover what's new with > Crystal Reports now. http://p.sf.net/sfu/bobj-july_______________________________________________ > ojAlgo-user mailing list > ojA...@li... > https://lists.sourceforge.net/lists/listinfo/ojalgo-user ------------------------------------------------------------------------------ Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day trial. Simplify your report design, integration and deployment - and focus on what you do best, core application coding. Discover what's new with Crystal Reports now. http://p.sf.net/sfu/bobj-july _______________________________________________ ojAlgo-user mailing list ojA...@li... https://lists.sourceforge.net/lists/listinfo/ojalgo-user ________________________________________________________________ PAM S.A Geneva Tel. +41 (0)58 323 3333 60, route des Acacias Fax +41 (0)58 323 2324 CH-1211 GENEVE 73 http://www.pictet.com/ ________________________________________________________________ This document should only be read by those persons to whom it is addressed and is not intended to be relied upon by any person without subsequent written confirmation of its contents. If you have received this e-mail message in error, please destroy it and delete it from your computer. Any form of reproduction, dissemination, copying, disclosure, modification, distribution and/or publication of this E-mail message is strictly prohibited. ________________________________________________________________ Pictet reserves the right to monitor and record business and personal communications. By responding to an email or call from a Pictet employee you are taken to have consented to such monitoring and recording. ________________________________________________________________ |
From: Anders P. <an...@op...> - 2009-11-16 14:42:32
|
Can you perhaps give me 2 test cases; 1 for the NPE case and 1 for the incorrect state case. /Anders On 16 nov 2009, at 14.33, Pierre AUSLAENDER wrote: > > Hello Anders, > > Thanks a lot for the swift reply. > > I 've played with the latest snapshot and here are my findings: > - Most of the times now, when the constraints cannot be satisfied, > the solver terminates with the state FAILED, which is what one would > expect, albeit after a great lot of iterations. > - Sometimes however, it terminates with the state OPTIMAL, and the > model validation returns FALSE, same as before I took the snapshot. > - Sometimes as well, it throws a NPE in the SVD (after saying that > LU is not solvable). > > I'm trying to build a simple JUnit and send it to you. > > Kind regards, > Pierre > > > > "Anders Peterson" <an...@op...> > 16.11.2009 12:57 > Please respond to > oja...@li... > > > To > oja...@li... > cc > Subject > Re: [ojAlgo-user] QuadraticSolver with unsatisfiable constraints > > > > Warning: this message has been delivered from outside the Pictet > Group. If the content is related to banking matters, please apply > the same procedure as for an incoming Fax or letter. > Attention: Ce message provient de l'extérieur du Groupe Pictet. S'il > possède un caractère bancaire, veuillez le traiter de la même > manière qu'un Fax ou une lettre entrante. > > > > > > On 16 nov 2009, at 11.06, Pierre AUSLAENDER wrote: > > > Hello, > > > > Thank you for this library, which is a joy to use - once I got over > > the lack of documentation. > > > > I'm using the QuadraticSolver (ActiveSetSolver) on a least squares > > minimisation problem, under a set of linear constraints. When the > > constraints can be satisfied, it works perfectly. However, when the > > constraints cannot be satisfied, the solver still returns with an > > OPTIMAL state after a few iterations. I was puzzled until I > > discovered the QuadraticSolver.validateSolution(...) methods, which > > I can use to effectively test the solution against the constraints. > > > > Is this the way it is supposed to work, or did I do something wrong? > > I would have expected the solver to FAIL instead. > > > I recently fixed a couple of bugs related to this. Get the latest > snapshot download from Source Forge, and try if that solves your > problem. If it doesn't I'd like you to give me (junit) test case. > > > > As an aside question, there is the following JavaDoc comment on > > ExpressionBasedModel.addConstraint(final String aName, ....): Note > > that only the first constraint you add will be used by the solver. > > What does this comment mean? Is it simply related to the uniqueness > > of the constraint name, or has it wider implications? > > > That comment should have been removed. It was only relevant for the > now removed/replaced KnapsckSolver. That solver could only use 1 > constraint - the first one you added. I'll correct the javadoc comment > now. > > > /Anders > > > > > Thank you, > > Pierre > > > > ________________________________________________________________ > > > > PAM S.A Geneva Tel. +41 (0)58 323 3333 > > 60, route des Acacias Fax +41 (0)58 323 2324 > > CH-1211 GENEVE 73 http://www.pictet.com/ > > ________________________________________________________________ > > > > This document should only be read by those persons to whom it is > > addressed and is not intended to be relied upon by any person > > without subsequent written confirmation of its contents. If you > > have received this e-mail message in error, please destroy it > > and delete it from your computer. > > Any form of reproduction, dissemination, copying, disclosure, > > modification, distribution and/or publication of this E-mail > > message is strictly prohibited. > > ________________________________________________________________ > > > > Pictet reserves the right to monitor and record business and > > personal communications. By responding to an email or call from > > a Pictet employee you are taken to have consented to such > > monitoring and recording. > > ________________________________________________________________ > > > > > ------------------------------------------------------------------------------ > > Let Crystal Reports handle the reporting - Free Crystal Reports 2008 > > 30-Day > > trial. Simplify your report design, integration and deployment - and > > focus on > > what you do best, core application coding. Discover what's new with > > Crystal Reports now. http://p.sf.net/sfu/bobj-july_______________________________________________ > > ojAlgo-user mailing list > > ojA...@li... > > https://lists.sourceforge.net/lists/listinfo/ojalgo-user > > > ------------------------------------------------------------------------------ > Let Crystal Reports handle the reporting - Free Crystal Reports 2008 > 30-Day > trial. Simplify your report design, integration and deployment - and > focus on > what you do best, core application coding. Discover what's new with > Crystal Reports now. http://p.sf.net/sfu/bobj-july > _______________________________________________ > ojAlgo-user mailing list > ojA...@li... > https://lists.sourceforge.net/lists/listinfo/ojalgo-user > > > > ________________________________________________________________ > > PAM S.A Geneva Tel. +41 (0)58 323 3333 > 60, route des Acacias Fax +41 (0)58 323 2324 > CH-1211 GENEVE 73 http://www.pictet.com/ > ________________________________________________________________ > > This document should only be read by those persons to whom it is > addressed and is not intended to be relied upon by any person > without subsequent written confirmation of its contents. If you > have received this e-mail message in error, please destroy it > and delete it from your computer. > Any form of reproduction, dissemination, copying, disclosure, > modification, distribution and/or publication of this E-mail > message is strictly prohibited. > ________________________________________________________________ > > Pictet reserves the right to monitor and record business and > personal communications. By responding to an email or call from > a Pictet employee you are taken to have consented to such > monitoring and recording. > ________________________________________________________________ > > ------------------------------------------------------------------------------ > Let Crystal Reports handle the reporting - Free Crystal Reports 2008 > 30-Day > trial. Simplify your report design, integration and deployment - and > focus on > what you do best, core application coding. Discover what's new with > Crystal Reports now. http://p.sf.net/sfu/bobj-july_______________________________________________ > ojAlgo-user mailing list > ojA...@li... > https://lists.sourceforge.net/lists/listinfo/ojalgo-user |
From: Pierre A. <pau...@pi...> - 2009-11-16 13:49:41
|
Hello Anders, Thanks a lot for the swift reply. I 've played with the latest snapshot and here are my findings: - Most of the times now, when the constraints cannot be satisfied, the solver terminates with the state FAILED, which is what one would expect, albeit after a great lot of iterations. - Sometimes however, it terminates with the state OPTIMAL, and the model validation returns FALSE, same as before I took the snapshot. - Sometimes as well, it throws a NPE in the SVD (after saying that LU is not solvable). I'm trying to build a simple JUnit and send it to you. Kind regards, Pierre "Anders Peterson" <an...@op...> 16.11.2009 12:57 Please respond to oja...@li... To oja...@li... cc Subject Re: [ojAlgo-user] QuadraticSolver with unsatisfiable constraints Warning: this message has been delivered from outside the Pictet Group. If the content is related to banking matters, please apply the same procedure as for an incoming Fax or letter. Attention: Ce message provient de l'extérieur du Groupe Pictet. S'il possède un caractère bancaire, veuillez le traiter de la même manière qu'un Fax ou une lettre entrante. On 16 nov 2009, at 11.06, Pierre AUSLAENDER wrote: > Hello, > > Thank you for this library, which is a joy to use - once I got over > the lack of documentation. > > I'm using the QuadraticSolver (ActiveSetSolver) on a least squares > minimisation problem, under a set of linear constraints. When the > constraints can be satisfied, it works perfectly. However, when the > constraints cannot be satisfied, the solver still returns with an > OPTIMAL state after a few iterations. I was puzzled until I > discovered the QuadraticSolver.validateSolution(...) methods, which > I can use to effectively test the solution against the constraints. > > Is this the way it is supposed to work, or did I do something wrong? > I would have expected the solver to FAIL instead. I recently fixed a couple of bugs related to this. Get the latest snapshot download from Source Forge, and try if that solves your problem. If it doesn't I'd like you to give me (junit) test case. > As an aside question, there is the following JavaDoc comment on > ExpressionBasedModel.addConstraint(final String aName, ....): Note > that only the first constraint you add will be used by the solver. > What does this comment mean? Is it simply related to the uniqueness > of the constraint name, or has it wider implications? That comment should have been removed. It was only relevant for the now removed/replaced KnapsckSolver. That solver could only use 1 constraint - the first one you added. I'll correct the javadoc comment now. /Anders > Thank you, > Pierre > > ________________________________________________________________ > > PAM S.A Geneva Tel. +41 (0)58 323 3333 > 60, route des Acacias Fax +41 (0)58 323 2324 > CH-1211 GENEVE 73 http://www.pictet.com/ > ________________________________________________________________ > > This document should only be read by those persons to whom it is > addressed and is not intended to be relied upon by any person > without subsequent written confirmation of its contents. If you > have received this e-mail message in error, please destroy it > and delete it from your computer. > Any form of reproduction, dissemination, copying, disclosure, > modification, distribution and/or publication of this E-mail > message is strictly prohibited. > ________________________________________________________________ > > Pictet reserves the right to monitor and record business and > personal communications. By responding to an email or call from > a Pictet employee you are taken to have consented to such > monitoring and recording. > ________________________________________________________________ > > ------------------------------------------------------------------------------ > Let Crystal Reports handle the reporting - Free Crystal Reports 2008 > 30-Day > trial. Simplify your report design, integration and deployment - and > focus on > what you do best, core application coding. Discover what's new with > Crystal Reports now. http://p.sf.net/sfu/bobj-july_______________________________________________ > ojAlgo-user mailing list > ojA...@li... > https://lists.sourceforge.net/lists/listinfo/ojalgo-user ------------------------------------------------------------------------------ Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day trial. Simplify your report design, integration and deployment - and focus on what you do best, core application coding. Discover what's new with Crystal Reports now. http://p.sf.net/sfu/bobj-july _______________________________________________ ojAlgo-user mailing list ojA...@li... https://lists.sourceforge.net/lists/listinfo/ojalgo-user ________________________________________________________________ PAM S.A Geneva Tel. +41 (0)58 323 3333 60, route des Acacias Fax +41 (0)58 323 2324 CH-1211 GENEVE 73 http://www.pictet.com/ ________________________________________________________________ This document should only be read by those persons to whom it is addressed and is not intended to be relied upon by any person without subsequent written confirmation of its contents. If you have received this e-mail message in error, please destroy it and delete it from your computer. Any form of reproduction, dissemination, copying, disclosure, modification, distribution and/or publication of this E-mail message is strictly prohibited. ________________________________________________________________ Pictet reserves the right to monitor and record business and personal communications. By responding to an email or call from a Pictet employee you are taken to have consented to such monitoring and recording. ________________________________________________________________ |
From: Anders P. <an...@op...> - 2009-11-16 11:57:49
|
On 16 nov 2009, at 11.06, Pierre AUSLAENDER wrote: > Hello, > > Thank you for this library, which is a joy to use - once I got over > the lack of documentation. > > I'm using the QuadraticSolver (ActiveSetSolver) on a least squares > minimisation problem, under a set of linear constraints. When the > constraints can be satisfied, it works perfectly. However, when the > constraints cannot be satisfied, the solver still returns with an > OPTIMAL state after a few iterations. I was puzzled until I > discovered the QuadraticSolver.validateSolution(...) methods, which > I can use to effectively test the solution against the constraints. > > Is this the way it is supposed to work, or did I do something wrong? > I would have expected the solver to FAIL instead. I recently fixed a couple of bugs related to this. Get the latest snapshot download from Source Forge, and try if that solves your problem. If it doesn't I'd like you to give me (junit) test case. > As an aside question, there is the following JavaDoc comment on > ExpressionBasedModel.addConstraint(final String aName, ....): Note > that only the first constraint you add will be used by the solver. > What does this comment mean? Is it simply related to the uniqueness > of the constraint name, or has it wider implications? That comment should have been removed. It was only relevant for the now removed/replaced KnapsckSolver. That solver could only use 1 constraint - the first one you added. I'll correct the javadoc comment now. /Anders > Thank you, > Pierre > > ________________________________________________________________ > > PAM S.A Geneva Tel. +41 (0)58 323 3333 > 60, route des Acacias Fax +41 (0)58 323 2324 > CH-1211 GENEVE 73 http://www.pictet.com/ > ________________________________________________________________ > > This document should only be read by those persons to whom it is > addressed and is not intended to be relied upon by any person > without subsequent written confirmation of its contents. If you > have received this e-mail message in error, please destroy it > and delete it from your computer. > Any form of reproduction, dissemination, copying, disclosure, > modification, distribution and/or publication of this E-mail > message is strictly prohibited. > ________________________________________________________________ > > Pictet reserves the right to monitor and record business and > personal communications. By responding to an email or call from > a Pictet employee you are taken to have consented to such > monitoring and recording. > ________________________________________________________________ > > ------------------------------------------------------------------------------ > Let Crystal Reports handle the reporting - Free Crystal Reports 2008 > 30-Day > trial. Simplify your report design, integration and deployment - and > focus on > what you do best, core application coding. Discover what's new with > Crystal Reports now. http://p.sf.net/sfu/bobj-july_______________________________________________ > ojAlgo-user mailing list > ojA...@li... > https://lists.sourceforge.net/lists/listinfo/ojalgo-user |
From: Pierre A. <pau...@pi...> - 2009-11-16 10:22:40
|
Hello, Thank you for this library, which is a joy to use - once I got over the lack of documentation. I'm using the QuadraticSolver (ActiveSetSolver) on a least squares minimisation problem, under a set of linear constraints. When the constraints can be satisfied, it works perfectly. However, when the constraints cannot be satisfied, the solver still returns with an OPTIMAL state after a few iterations. I was puzzled until I discovered the QuadraticSolver.validateSolution(...) methods, which I can use to effectively test the solution against the constraints. Is this the way it is supposed to work, or did I do something wrong? I would have expected the solver to FAIL instead. As an aside question, there is the following JavaDoc comment on ExpressionBasedModel.addConstraint(final String aName, ...): Note that only the first constraint you add will be used by the solver. What does this comment mean? Is it simply related to the uniqueness of the constraint name, or has it wider implications? Thank you, Pierre ________________________________________________________________ PAM S.A Geneva Tel. +41 (0)58 323 3333 60, route des Acacias Fax +41 (0)58 323 2324 CH-1211 GENEVE 73 http://www.pictet.com/ ________________________________________________________________ This document should only be read by those persons to whom it is addressed and is not intended to be relied upon by any person without subsequent written confirmation of its contents. If you have received this e-mail message in error, please destroy it and delete it from your computer. Any form of reproduction, dissemination, copying, disclosure, modification, distribution and/or publication of this E-mail message is strictly prohibited. ________________________________________________________________ Pictet reserves the right to monitor and record business and personal communications. By responding to an email or call from a Pictet employee you are taken to have consented to such monitoring and recording. ________________________________________________________________ |
From: Anders P. <an...@op...> - 2009-11-02 19:06:03
|
On 2 nov 2009, at 15.22, Alok Saldanha wrote: > I am having trouble getting started with ojAlgo. I am confused by > the class structure and am not sure how to do basic things. Are > there tutorials anywhere? None that I know of. > I would like to be able to do the following > > 1) Load historical data for two tech stocks and the nasdaq that I > have already downloaded from yahoo > 2) Calculate raw covariance matrix for the two tech stocks. > 3) Solve a MarkowitzModel using some expected excess return values > also stored on disk. > > In order to load historical data, I modified SymbolData so that > fetch could take a BufferedReader argument that I built off a file, > > public void fetch() { > BufferedReader tmpInput = myResourceLocator.getStreamReader(); > fetch(tmpInput); > } > public void fetch(BufferedReader tmpInput) { > ... existing code here... > } > > It is somewhat surprising to me that I had to modify the library in > order to load data off disk rather than over the network, but I > couldn't figure out an easier way. The code in the org.ojalgo.finance.data package is designed to load data from Yahoo or Google - that's what it does. The use case you describe never occurred to me. (I never store data in Yahoo/Google format on file.) > I have not been able to figure out how to calculate covariance; I am > able to populate a matrix with my data values as follows (nokPrices, > orclPrices and nasdaqPrices are of type List<HistoricalData>, which > I made public so I could call SymbolData.getHistoricalPrices()): What I do is call/use SymbolDataCache.getInstance().getPriceSeries(...) I'll think about making the changes you suggested. > MatrixStore<Double> a = PrimitiveDenseStore.FACTORY.makeZero > (3, > nokPrices.size()); > BasicMatrix b = new PrimitiveMatrix(a); > for (int i = 0; i < nokPrices.size(); i++) { > b.set(0, i, nokPrices.get(i).getAdjustedClose()); > b.set(1, i, orclPrices.get(i).getAdjustedClose()); > b.set(2, i, nasdaqPrices.get(i).getAdjustedClose()); > } > > However, I am not sure how to calculate the covariance, i.e. is > there a function for this in a util library, or if now how can I > copy the array, take the transpose, subtract the column/row means > and multiply the arrays (i.e. the operations necessary to calculate > covariance). Look at org.ojalgo.series.CoordinationSet, and then at org.ojalgo.random.SampleSet. Maybe there could be a util method that takes you straight from a CoordinationSet to a covariance matrix, but at this moment there is not. /Anders > Thanks, > Alok > > ------------------------------------------------------------------------------ > Come build with us! The BlackBerry(R) Developer Conference in SF, CA > is the only developer event you need to attend this year. Jumpstart > your > developing skills, take BlackBerry mobile applications to market and > stay > ahead of the curve. Join us from November 9 - 12, 2009. Register now! > http://p.sf.net/sfu/devconference_______________________________________________ > ojAlgo-user mailing list > ojA...@li... > https://lists.sourceforge.net/lists/listinfo/ojalgo-user |
From: Alok S. <alo...@gm...> - 2009-11-02 14:22:46
|
I am having trouble getting started with ojAlgo. I am confused by the class structure and am not sure how to do basic things. Are there tutorials anywhere? I would like to be able to do the following 1) Load historical data for two tech stocks and the nasdaq that I have already downloaded from yahoo 2) Calculate raw covariance matrix for the two tech stocks. 3) Solve a MarkowitzModel using some expected excess return values also stored on disk. In order to load historical data, I modified SymbolData so that fetch could take a BufferedReader argument that I built off a file, public void fetch() { BufferedReader tmpInput = myResourceLocator.getStreamReader(); fetch(tmpInput); } public void fetch(BufferedReader tmpInput) { ... existing code here... } It is somewhat surprising to me that I had to modify the library in order to load data off disk rather than over the network, but I couldn't figure out an easier way. I have not been able to figure out how to calculate covariance; I am able to populate a matrix with my data values as follows (nokPrices, orclPrices and nasdaqPrices are of type List<HistoricalData>, which I made public so I could call SymbolData.getHistoricalPrices()): MatrixStore<Double> a = PrimitiveDenseStore.FACTORY.makeZero(3, nokPrices.size()); BasicMatrix b = new PrimitiveMatrix(a); for (int i = 0; i < nokPrices.size(); i++) { b.set(0, i, nokPrices.get(i).getAdjustedClose()); b.set(1, i, orclPrices.get(i).getAdjustedClose()); b.set(2, i, nasdaqPrices.get(i).getAdjustedClose()); } However, I am not sure how to calculate the covariance, i.e. is there a function for this in a util library, or if now how can I copy the array, take the transpose, subtract the column/row means and multiply the arrays (i.e. the operations necessary to calculate covariance). Thanks, Alok |
From: David S. <d.s...@4c...> - 2009-10-19 12:34:25
|
> That sound s really good to me. How many of the variables are integer > variables? We had about 60~ (Boolean) integer variables. |