Previous Up Next

6.54.6  Résolution d’un problème d’optimisation linéaire : lpsolve

lpsolve a au plus 4 arguments qui sont dans l’ordre :

lpsolve résout un problème d’optimisation linéaire de fonction objectif obj sous contraintes optionnelles constr.
lpsolve trouve le maximum ou le minimum d’une fonction linéaire de plusieurs variables assujétit à vérifier des contraintes qui sont égalités ou des inégalités linéaires mixed integer problems

La valeur renvoyée est de la forme [optimum,soln]optimum is est la valeu du minimum/maximum de la fonction objectif et soln est la liste des coordonnées correspondant au point pour lequel la valeur optimale est atteinte. Si il n’y a pas de solution, If there is no feasible solution,une liste vide est is retournée. Lorsque la the fonction objectif n’est pas bornée, optimum est +infinity (pour les problèmes de maximisation) ou -infinity (pour les problèmes de minimisation), soln est généralement sans signification.

If obj is given as constant (for example, zero) then only a feasible point is returned as a list, if one exists. If the problem is infeasible, an empty list is returned. This may be used as a test to check whether a set of linear constraints is feasible or not.

The given objective function is minimized by default. To maximize it, include the option lp_maximize=true or simply lp_maximize.

By default, all variables are considered continuous and unrestricted in sign.

The solver combines the two-phase simplex method and the dual simplex method to find the optimal solution.

Problèmes à variables continues

Par exemple, pour résoudre le problem définit en (1), On tape :

constr:=[x<=1,y>=2,x+3y-z=2,3x-y+z<=8,-x+y<=5];
lpsolve(2x+y-z+4,constr)

On obtient :

[-4,[x=0,y=5,z=13]]

Donc, le minimum de f(x,y,z)=2 x+yz+4 est egal à −4 lorsqu’on impose comme contraintes x leq 1,y geq 2,x+3yz=2,3xy+z leq 8,−x+y leq 5 .
La valeur minimum est atteinte au point (x,y,z)=(0,5,13).

Les Contraintes peuvent aussi s’écrire expr=a..b pour des expressions linéaires et bornées.
On tape :

lpsolve(x+2y+3z,[x+y=1..5,y+z+1=2..4,x>=0,y>=0])

On obtient :

[-2,[x=0,y=5,z=-4]]

On utilise l’option assume=lp_nonnegativecomme 3ième argument pour spécifier que toutes les variables sont nonnegatives : ce qui est plus simple que d’entrer explicitement des contraintes nonnegatives.
On tape :

lpsolve(-x-y,[y<=3x+1/2,y<=-5x+2],assume=lp_nonnegative)

On obtient :

[-5/4,[x=3/16,y=17/16]]

Les bornes peuvent être ajoutées séparément pour certaines variables.
On les met alors en 3ième argument. On tape :

constr:=[5x-10y<=20,2z-3y=6,-x+3y<=3];
lpsolve(-6x+4y+z,constr,x=1..20,y=0..inf)

On obtient :

[-133/2,[x=18,y=7,z=27/2]]

Integer programming

Use the assume=integer or assume=lp_integer option to solve integer programming problems. The function lpsolve uses branch and bound method in such cases. The total number of investigated nodes is printed out before the function returns. To limit branch and bound tree depth, use the option :

lp_depthlimit=<positive integer>

In that case optimality of the solution can’t be guaranteed. Dans ce cas optimisation de la solution n’est pas garantie.
On tape :

lpsolve(-5x-7y,[7x+y<=35,-x+3y<=6],assume=integer)

On obtient :

[-41,[x=4,y=3]]

On utilise l’option assume=lp_binary pour spécifier que toutes les variables are binaires, i.e. les seules valeurs autorisées sont 0 et 1.
Généralement, les variables binaires representent vrai et faux, ce qui est compréhensible en logique. On tape :

lpsolve(8x1+11x2+6x3+4x4,[5x1+7x2+4x3+3x4<=14],assume=lp_binary,lp_maximize)

On obtient :

[21,[x1=0,x2=1,x3=1,x4=1]]

Options

lp_integervariables=<list of integer variables>

and

lp_binaryvariables=<list of binary variables>

are used for specifying mixed integer/binary programming problems. Also, the

lp_variables=<list of continuous variables>

option may be used, which overrides integer and binary settings. For example, a linear programming problem with mostly integer variables may be specified using the option assume=integer and specifying continuous variables with lp_variables, which overrides the global integer setting.

On tape :

lpsolve(x+3y+3z,[x+3y+2z<=7,2x+2y+z<=11],
assume=lp_nonnegative,lp_maximize, lp_integervariables=[x,z])

On obtient :

[10,[x=0,y=1/3,z=3]]

Use the assume=lp_nonnegint or assume=nonnegint option to get nonnegative integer values.

On tape :

lpsolve(2x+5y,[3x-y=1,x-y<=5],assume=nonnegint)

On obtient :

[12,[x=1,y=2]]

Entering linear programs in matrix form

The function lpsolve supports entering linear programming problems in matrix form, which is convenient for problems with relatively large number of variables and/or constraints.

To enter a problem in matrix form, the parameter obj must be a vector of coefficients c and constr, which is mandatory this case, must be a list [A,b,Aeq,beq] , where A,Aeq are matrices and b,beq are vectors of real numbers. Without any other parameters, this minimizes cT x under conditions A xb and Aeq x=beq . If a problem does not contain equality constraints, parameters Aeq and beq may be omitted. For a problem that does not contain inequality constraints, empty lists must be passed as A and b . Also, constr may be an empty list.

The parameter bd is entered as a list of two vectors bl and bu of the same length as the vector c such that blxbu . For unbounded variables use +infinity or -infinity.

When specifying mixed problems in matrix form, variables are entered as the corresponding indexes, which are 1-based, i.e. the first variable has index 1, the second variable has index 2 and so on. Other options for lpsolve are passed to a problem in matrix form in the same way as if it was in symbolic form.

Input :

c:=[-2,1];A:=[[-1,1],[1,1],[-1,0],[0,-1]];
b:=[3,5,0,0];lpsolve(c,[A,b])

Output :

[-10,[5,0]]

Input :

c:=[-2,5,-3];bl:=[2,3,1];bu:=[6,10,3.5];
lpsolve(c,[],[bl,bu])

Output :

[-7.5,[6.0,3.0,3.5]]

Input :

c:=[4,5];Aeq:=[[-1,1.5],[-3,2]];beq:=[2,3];
lpsolve(c,[[],[],Aeq,beq])

Output :

[5.2,[-0.2,1.2]]

Input :

c:=[2,-3,-5];A:=[[-5,4,-5],[2,5,7],[2,-3,4]];
b:=[3,1,-2];lpsolve(c,[A,b],lp_integervariables=[1,3])

Output :

[19,[1,3/4,-1]]

Previous Up Next