User Tools

Site Tools


gams:linearize_a_non-linear_function

This is an old revision of the document!


How do I linearize a non-linear function?

Find below some approaches to linearize the cubic function y =e= x*sqr(x) - 7*sqr(x) + 14*x - 8

$offsymxref offsymlist
option limrow = 0,
       limcol = 0,
       solprint = off ;

variable y,x ;
equations eq ;
eq.. y =e= x*sqr(x) - 7*sqr(x) + 14*x - 8 ;

x.lo = 1 ; x.up = 10.5 ;
model cubic /all/ ;
solve cubic using nlp minimizing y ;
display y.l, x.l ;

The global solution to that problem is:

----     13 VARIABLE y.L                   =       -2.113  
            VARIABLE x.L                   =        3.215  

Using Binary variables to model a piecewise linear function in GAMS

 
$ontext
Using Binary variables to model a piecewise linear function in GAMS. 

This formulation uses points on the curve and is  particularly good for developing approximations of 
nonlinear functions 
 
Ref: "Integer and Combinatorial Optimization" by 
      Nemhauser, G.L., and Wolsey, L.A., John Wiley 
      and Sons. Page 11. 
$ontext 
option limrow = 0, 
       limcol = 0 
       ; 
 
* this contains the set of points 
 
set i / 
   1*20 
    /  ; 
 
set notlast(i) ; 
notlast(i)$(ord(i) lt card(i)) = yes ; 
 
* this contains the values of x-coordinates 
parameter xval(i) / 
  1    1 
  2    1.5 
  3    2 
  4    2.5 
  5    3 
  6    3.5 
  7    4 
  8    4.5 
  9    5 
 10    5.5 
 11    6 
 12    6.5 
 13    7 
 14    7.5 
 15    8 
 16    8.5 
 17    9 
 18    9.5 
 19   10 
 20   10.5 
    / ; 
 
* this contains the nonlinear function of x 
parameter yval(i) ; 
yval(i) = xval(i)*sqr(xval(i)) - 7*sqr(xval(i)) + 14*xval(i) - 8 ; 
 
variables x,y,lam(i),bin(i) ; 
positive variables lam(i) ; 
binary variables   bin(i) ; 
 
equations xdef, ydef, norm, lamdef(i), soslam ; 
 
xdef.. x =e= sum(i,lam(i)*xval(i)) ; 
ydef.. y =e= sum(i,lam(i)*yval(i)) ; 
 
norm.. sum(i,lam(i)) =e= 1 ; 
 
lamdef(i).. lam(i) =l= bin(i-1) + bin(i)$notlast(i) ; 
soslam..    sum(i$notlast(i),bin(i)) =e= 1 ; 
 
x.lo = smin(i,xval(i)) ; 
x.up = smax(i,xval(i)) ; 
 
model piece /all/ ; 
piece.optcr = 0.0 ; 
solve piece minimizing y using mip ; 
display x.l, y.l ; 
display lam.l, bin.l ; 

A SOS1 type formulation of a piecewise linear function in GAMS

 
$ontext
The SOS1 type formulation of a piecewise linear function in GAMS. 
 
This formulation uses points on the curve and is particularly good for developing approximations of nonlinear functions 
 
Ref: "Integer and Combinatorial Optimization" by 
      Nemhauser, G.L., and Wolsey, L.A., John Wiley 
      and Sons. Page 11. 
 
option limrow = 0, 
       limcol = 0 
       ; 
 
* this contains the set of points 
 
set i / 
   1*20 
    /  ; 
 
set notlast(i) ; 
notlast(i)$(ord(i) lt card(i)) = yes ; 
 
* this contains the values of x-coordinates 
parameter xval(i) / 
  1    1 
  2    1.5 
  3    2 
  4    2.5 
  5    3 
  6    3.5 
  7    4 
  8    4.5 
  9    5 
 10    5.5 
 11    6 
 12    6.5 
 13    7 
 14    7.5 
 15    8 
 16    8.5 
 17    9 
 18    9.5 
 19   10 
 20   10.5 
    / ; 
 
* this contains the nonlinear function of x 
parameter yval(i) ; 
yval(i) = xval(i)*sqr(xval(i)) - 7*sqr(xval(i)) + 14*xval(i) - 8 ; 
 
variables x,y,lam(i),bin(i) ; 
positive variables lam(i) ; 
sos1 variables   bin(i) ; 
 
equations xdef, ydef, norm, lamdef(i), soslam ; 
 
xdef.. x =e= sum(i,lam(i)*xval(i)) ; 
ydef.. y =e= sum(i,lam(i)*yval(i)) ; 
 
norm.. sum(i,lam(i)) =e= 1 ; 
 
lamdef(i).. lam(i) =l= bin(i-1) + bin(i)$notlast(i) ; 
soslam..    sum(i$notlast(i),bin(i)) =e= 1 ; 
 
x.lo = smin(i,xval(i)) ; 
x.up = smax(i,xval(i)) ; 
 
model piece /all/ ; 
piece.optcr = 0.0 ; 
solve piece minimizing y using mip ; 
display x.l, y.l ; 
display lam.l, bin.l ; 

A SOS2 type formulation of a piecewise linear function in GAMS

$ontext
The SOS2 type formulation of a piecewise linear  function in GAMS. 

This formulation uses points on the curve and is particularly good for developing approximations of  nonlinear functions 
 
Ref: "Integer and Combinatorial Optimization" by 
      Nemhauser, G.L., and Wolsey, L.A., John Wiley 
      and Sons. Page 11. 
 
option limrow = 0 
       limcol = 0 
       ; 
 
* this contains the set of points 
set i / 
   1*20 
    /  ; 
 
set notlast(i) ; 
notlast(i)$(ord(i) lt card(i)) = yes ; 
 
* this contains the values of x-coordinates 
parameter xval(i) / 
  1    1 
  2    1.5 
  3    2 
  4    2.5 
  5    3 
  6    3.5 
  7    4 
  8    4.5 
  9    5 
 10    5.5 
 11    6 
 12    6.5 
 13    7 
 14    7.5 
 15    8 
 16    8.5 
 17    9 
 18    9.5 
 19   10 
 20   10.5 
    / ; 
 
* this contains the nonlinear function of x 
parameter yval(i) ; 
yval(i) = xval(i)*sqr(xval(i)) - 7*sqr(xval(i)) + 14*xval(i) - 8 ; 
 
variables x,y,lam(i) ; 
sos2 variables lam(i) ; 
 
equations xdef, ydef, norm ; 
 
xdef.. x =e= sum(i,lam(i)*xval(i)) ; 
ydef.. y =e= sum(i,lam(i)*yval(i)) ; 
 
norm.. sum(i,lam(i)) =e= 1 ; 
 
x.lo = smin(i,xval(i)) ; 
x.up = smax(i,xval(i)) ; 
 
model piece /all/ ; 
piece.optcr = 0.0 ; 
solve piece minimizing y using mip ; 
display x.l, y.l ; 
display lam.l ; 
IMPRESSUM / LEGAL NOTICEPRIVACY POLICY gams/linearize_a_non-linear_function.1193760334.txt.gz · Last modified: 2007/10/30 17:05 by Franz Nelissen