User Tools

Site Tools


gams:linearize_a_non-linear_function

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
gams:linearize_a_non-linear_function [2007/10/30 16:58]
Franz Nelissen created
— (current)
Line 1: Line 1:
-====== 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''​ 
-<​code>​ 
-$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 ; 
-</​code>​ 
-The global solution to that problem is:  
-<​code>​ 
-----     13 VARIABLE y.L                   ​= ​      ​-2.113  ​ 
-            VARIABLE x.L                   ​= ​       3.215  ​ 
-</​code>​ 
- 
-===== Using Binary variables to model a piecewise linear function in GAMS. ===== 
-<​code> ​ 
-$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 ;  
-</​code>​ 
- 
- 
- 
- 
  
IMPRESSUM / LEGAL NOTICEPRIVACY POLICY gams/linearize_a_non-linear_function.1193759908.txt.gz ยท Last modified: 2007/10/30 16:58 by Franz Nelissen