diff options
author | Harpreet | 2016-01-25 01:05:02 +0530 |
---|---|---|
committer | Harpreet | 2016-01-25 01:05:02 +0530 |
commit | a2d9c2bfd6eb83d1a494821176388eb312d08254 (patch) | |
tree | 611fba3b340ba48b9d9d7435ce2f29b1ce0c12fa /demos | |
parent | dd3d72ae2cdb43311b4e501966f09694bbd3e505 (diff) | |
download | FOSSEE-Optimization-toolbox-a2d9c2bfd6eb83d1a494821176388eb312d08254.tar.gz FOSSEE-Optimization-toolbox-a2d9c2bfd6eb83d1a494821176388eb312d08254.tar.bz2 FOSSEE-Optimization-toolbox-a2d9c2bfd6eb83d1a494821176388eb312d08254.zip |
functions added
Diffstat (limited to 'demos')
-rw-r--r-- | demos/exmip1.mps | 70 | ||||
-rw-r--r-- | demos/fgoalattain.dem.sce | 26 | ||||
-rw-r--r-- | demos/fminbnd.dem.sce | 54 | ||||
-rw-r--r-- | demos/fmincon.dem.sce | 155 | ||||
-rw-r--r-- | demos/fminimax.dem.sce | 73 | ||||
-rw-r--r-- | demos/fminunc.dem.sce | 61 | ||||
-rwxr-xr-x | demos/linprog.dem.sce | 64 |
7 files changed, 503 insertions, 0 deletions
diff --git a/demos/exmip1.mps b/demos/exmip1.mps new file mode 100644 index 0000000..4f73ac5 --- /dev/null +++ b/demos/exmip1.mps @@ -0,0 +1,70 @@ +************************************************************************ +* +* The data in this file represents the following problem: +* +* Minimize or maximize Z = x1 + 2x5 - x8 +* +* Subject to: +* +* 2.5 <= 3x1 + x2 - 2x4 - x5 - x8 +* 2x2 + 1.1x3 <= 2.1 +* x3 + x6 = 4.0 +* 1.8 <= 2.8x4 -1.2x7 <= 5.0 +* 3.0 <= 5.6x1 + x5 + 1.9x8 <= 15.0 +* +* where: +* +* 2.5 <= x1 +* 0 <= x2 <= 4.1 +* 0 <= x3 +* 0 <= x4 +* 0.5 <= x5 <= 4.0 +* 0 <= x6 +* 0 <= x7 +* 0 <= x8 <= 4.3 +* +* x3, x4 are 0,1 variables. +* +************************************************************************ +NAME EXAMPLE +ROWS + N OBJ + G ROW01 + L ROW02 + E ROW03 + G ROW04 + L ROW05 +COLUMNS + COL01 OBJ 1.0 + COL01 ROW01 3.0 ROW05 5.6 + COL02 ROW01 1.0 ROW02 2.0 +* +* Mark COL03 and COL04 as integer variables. +* + INT1 'MARKER' 'INTORG' + COL03 ROW02 1.1 ROW03 1.0 + COL04 ROW01 -2.0 ROW04 2.8 + INT1END 'MARKER' 'INTEND' +* + COL05 OBJ 2.0 + COL05 ROW01 -1.0 ROW05 1.0 + COL06 ROW03 1.0 + COL07 ROW04 -1.2 + COL08 OBJ -1.0 + COL08 ROW01 -1.0 ROW05 1.9 +RHS + RHS1 ROW01 2.5 + RHS1 ROW02 2.1 + RHS1 ROW03 4.0 + RHS1 ROW04 1.8 + RHS1 ROW05 15.0 +RANGES + RNG1 ROW04 3.2 + RNG1 ROW05 12.0 +BOUNDS + LO BND1 COL01 2.5 + UP BND1 COL02 4.1 + LO BND1 COL05 0.5 + UP BND1 COL05 4.0 + UP BND1 COL08 4.3 +ENDATA diff --git a/demos/fgoalattain.dem.sce b/demos/fgoalattain.dem.sce new file mode 100644 index 0000000..89a5957 --- /dev/null +++ b/demos/fgoalattain.dem.sce @@ -0,0 +1,26 @@ +mode(1) +// +// Demo of fgoalattain.sci +// + +function f1 = fun(x) +f1(1)=2*x(1)*x(1)+x(2)*x(2)-48*x(1)-40*x(2)+304 +f1(2)=-x(1)*x(1)-3*x(2)*x(2) +f1(3)=x(1)+3*x(2)-18 +f1(4)=-x(1)-x(2) +f1(5)=x(1)+x(2)-8 +endfunction +x0=[-1,1]; +halt() // Press return to continue + +goal=[-5,-3,-2,-1,-4]; +weight=abs(goal) +//xopt = [-0.0000011 -63.999998 -2.0000002 -8 3.485D-08] +//fval = [4 3.99] +halt() // Press return to continue + +//Run fgoalattain +[xopt,fval,attainfactor,exitflag,output,lambda]=fgoalattain(fun,x0,goal,weight) +halt() // Press return to continue + +//========= E N D === O F === D E M O =========// diff --git a/demos/fminbnd.dem.sce b/demos/fminbnd.dem.sce new file mode 100644 index 0000000..a9b4865 --- /dev/null +++ b/demos/fminbnd.dem.sce @@ -0,0 +1,54 @@ +mode(1) +// +// Demo of fminbnd.sci +// + +//Find x in R^6 such that it minimizes: +//f(x)= sin(x1) + sin(x2) + sin(x3) + sin(x4) + sin(x5) + sin(x6) +//-2 <= x1,x2,x3,x4,x5,x6 <= 2 +//Objective function to be minimised +function y=f(x) +y=0 +for i =1:6 +y=y+sin(x(i)); +end +endfunction +//Variable bounds +x1 = [-2, -2, -2, -2, -2, -2]; +x2 = [2, 2, 2, 2, 2, 2]; +//Options +options=list("MaxIter",[1500],"CpuTime", [100],"TolX",[1e-6]) +//Calling Ipopt +[x,fval] =fminbnd(f, x1, x2, options) +halt() // Press return to continue + +//Find x in R such that it minimizes: +//f(x)= 1/x^2 +//0 <= x <= 1000 +//Objective function to be minimised +function y=f(x) +y=1/x^2 +endfunction +//Variable bounds +x1 = [0]; +x2 = [1000]; +//Calling Ipopt +[x,fval,exitflag,output,lambda] =fminbnd(f, x1, x2) +halt() // Press return to continue + +//The below problem is an unbounded problem: +//Find x in R^2 such that it minimizes: +//f(x)= -[(x1-1)^2 + (x2-1)^2] +//-inf <= x1,x2 <= inf +//Objective function to be minimised +function y=f(x) +y=-((x(1)-1)^2+(x(2)-1)^2); +endfunction +//Variable bounds +x1 = [-%inf , -%inf]; +x2 = []; +//Options +options=list("MaxIter",[1500],"CpuTime", [100],"TolX",[1e-6]) +//Calling Ipopt +[x,fval,exitflag,output,lambda] =fminbnd(f, x1, x2, options) +//========= E N D === O F === D E M O =========// diff --git a/demos/fmincon.dem.sce b/demos/fmincon.dem.sce new file mode 100644 index 0000000..790c67f --- /dev/null +++ b/demos/fmincon.dem.sce @@ -0,0 +1,155 @@ +mode(1) +// +// Demo of fmincon.sci +// + +//Find x in R^2 such that it minimizes: +//f(x)= -x1 -x2/3 +//x0=[0,0] +//constraint-1 (c1): x1 + x2 <= 2 +//constraint-2 (c2): x1 + x2/4 <= 1 +//constraint-3 (c3): x1 - x2 <= 2 +//constraint-4 (c4): -x1/4 - x2 <= 1 +//constraint-5 (c5): -x1 - x2 <= -1 +//constraint-6 (c6): -x1 + x2 <= 2 +//constraint-7 (c7): x1 + x2 = 2 +//Objective function to be minimised +function y=f(x) +y=-x(1)-x(2)/3; +endfunction +//Starting point, linear constraints and variable bounds +x0=[0 , 0]; +A=[1,1 ; 1,1/4 ; 1,-1 ; -1/4,-1 ; -1,-1 ; -1,1]; +b=[2;1;2;1;-1;2]; +Aeq=[1,1]; +beq=[2]; +lb=[]; +ub=[]; +nlc=[]; +//Gradient of objective function +function y= fGrad(x) +y= [-1,-1/3]; +endfunction +//Hessian of lagrangian +function y= lHess(x,obj,lambda) +y= obj*[0,0;0,0] +endfunction +//Options +options=list("GradObj", fGrad, "Hessian", lHess); +//Calling Ipopt +[x,fval,exitflag,output,lambda,grad,hessian] =fmincon(f, x0,A,b,Aeq,beq,lb,ub,nlc,options) +halt() // Press return to continue + +//Find x in R^3 such that it minimizes: +//f(x)= x1*x2 + x2*x3 +//x0=[0.1 , 0.1 , 0.1] +//constraint-1 (c1): x1^2 - x2^2 + x3^2 <= 2 +//constraint-2 (c2): x1^2 + x2^2 + x3^2 <= 10 +//Objective function to be minimised +function y=f(x) +y=x(1)*x(2)+x(2)*x(3); +endfunction +//Starting point, linear constraints and variable bounds +x0=[0.1 , 0.1 , 0.1]; +A=[]; +b=[]; +Aeq=[]; +beq=[]; +lb=[]; +ub=[]; +//Nonlinear constraints +function [c,ceq]=nlc(x) +c = [x(1)^2 - x(2)^2 + x(3)^2 - 2 , x(1)^2 + x(2)^2 + x(3)^2 - 10]; +ceq = []; +endfunction +//Gradient of objective function +function y= fGrad(x) +y= [x(2),x(1)+x(3),x(2)]; +endfunction +//Hessian of the Lagrange Function +function y= lHess(x,obj,lambda) +y= obj*[0,1,0;1,0,1;0,1,0] + lambda(1)*[2,0,0;0,-2,0;0,0,2] + lambda(2)*[2,0,0;0,2,0;0,0,2] +endfunction +//Gradient of Non-Linear Constraints +function [cg,ceqg] = cGrad(x) +cg=[2*x(1) , -2*x(2) , 2*x(3) ; 2*x(1) , 2*x(2) , 2*x(3)]; +ceqg=[]; +endfunction +//Options +options=list("MaxIter", [1500], "CpuTime", [500], "GradObj", fGrad, "Hessian", lHess,"GradCon", cGrad); +//Calling Ipopt +[x,fval,exitflag,output] =fmincon(f, x0,A,b,Aeq,beq,lb,ub,nlc,options) +halt() // Press return to continue + +//The below problem is an unbounded problem: +//Find x in R^3 such that it minimizes: +//f(x)= -(x1^2 + x2^2 + x3^2) +//x0=[0.1 , 0.1 , 0.1] +// x1 <= 0 +// x2 <= 0 +// x3 <= 0 +//Objective function to be minimised +function y=f(x) +y=-(x(1)^2+x(2)^2+x(3)^2); +endfunction +//Starting point, linear constraints and variable bounds +x0=[0.1 , 0.1 , 0.1]; +A=[]; +b=[]; +Aeq=[]; +beq=[]; +lb=[]; +ub=[0,0,0]; +//Options +options=list("MaxIter", [1500], "CpuTime", [500]); +//Calling Ipopt +[x,fval,exitflag,output,lambda,grad,hessian] =fmincon(f, x0,A,b,Aeq,beq,lb,ub,[],options) +halt() // Press return to continue + +//The below problem is an infeasible problem: +//Find x in R^3 such that in minimizes: +//f(x)=x1*x2 + x2*x3 +//x0=[1,1,1] +//constraint-1 (c1): x1^2 <= 1 +//constraint-2 (c2): x1^2 + x2^2 <= 1 +//constraint-3 (c3): x3^2 <= 1 +//constraint-4 (c4): x1^3 = 0.5 +//constraint-5 (c5): x2^2 + x3^2 = 0.75 +// 0 <= x1 <=0.6 +// 0.2 <= x2 <= inf +// -inf <= x3 <= 1 +//Objective function to be minimised +function y=f(x) +y=x(1)*x(2)+x(2)*x(3); +endfunction +//Starting point, linear constraints and variable bounds +x0=[1,1,1]; +A=[]; +b=[]; +Aeq=[]; +beq=[]; +lb=[0 0.2,-%inf]; +ub=[0.6 %inf,1]; +//Nonlinear constraints +function [c,ceq]=nlc(x) +c=[x(1)^2-1,x(1)^2+x(2)^2-1,x(3)^2-1]; +ceq=[x(1)^3-0.5,x(2)^2+x(3)^2-0.75]; +endfunction +//Gradient of objective function +function y= fGrad(x) +y= [x(2),x(1)+x(3),x(2)]; +endfunction +//Hessian of the Lagrange Function +function y= lHess(x,obj,lambda) +y= obj*[0,1,0;1,0,1;0,1,0] + lambda(1)*[2,0,0;0,0,0;0,0,0] + lambda(2)*[2,0,0;0,2,0;0,0,0] +lambda(3)*[0,0,0;0,0,0;0,0,2] + lambda(4)*[6*x(1 ),0,0;0,0,0;0,0,0] + lambda(5)*[0,0,0;0,2,0;0,0,2]; +endfunction +//Gradient of Non-Linear Constraints +function [cg,ceqg] = cGrad(x) +cg = [2*x(1),0,0;2*x(1),2*x(2),0;0,0,2*x(3)]; +ceqg = [3*x(1)^2,0,0;0,2*x(2),2*x(3)]; +endfunction +//Options +options=list("MaxIter", [1500], "CpuTime", [500], "GradObj", fGrad, "Hessian", lHess,"GradCon", cGrad); +//Calling Ipopt +[x,fval,exitflag,output,lambda,grad,hessian] =fmincon(f, x0,A,b,Aeq,beq,lb,ub,nlc,options) +//========= E N D === O F === D E M O =========// diff --git a/demos/fminimax.dem.sce b/demos/fminimax.dem.sce new file mode 100644 index 0000000..404af00 --- /dev/null +++ b/demos/fminimax.dem.sce @@ -0,0 +1,73 @@ +mode(1) +// +// Demo of fminimax.sci +// + +// A basic case : +// we provide only the objective function and the nonlinear constraint +// function +function f = myfun(x) +f(1)= 2*x(1)^2 + x(2)^2 - 48*x(1) - 40*x(2) + 304; //Objectives +f(2)= -x(1)^2 - 3*x(2)^2; +f(3)= x(1) + 3*x(2) -18; +f(4)= -x(1) - x(2); +f(5)= x(1) + x(2) - 8; +endfunction +// The initial guess +x0 = [0.1,0.1]; +// The expected solution : only 4 digits are guaranteed +//xopt = [4 4] +//fopt = [0 -64 -2 -8 0] +maxfopt = 0 +// Run fminimax +[xopt,fopt,maxfval,exitflag,output,lambda] = fminimax(myfun, x0) +// Press ENTER to continue +halt() // Press return to continue + +// A case where we provide the gradient of the objective +// functions and the Jacobian matrix of the constraints. +// The objective function and its gradient +function f = myfun(x) +f(1)= 2*x(1)^2 + x(2)^2 - 48*x(1) - 40*x(2) + 304; +f(2)= -x(1)^2 - 3*x(2)^2; +f(3)= x(1) + 3*x(2) -18; +f(4)= -x(1) - x(2); +f(5)= x(1) + x(2) - 8; +endfunction +// Defining gradient of myfun +function G = myfungrad(x) +G = [ 4*x(1) - 48, -2*x(1), 1, -1, 1; +2*x(2) - 40, -6*x(2), 3, -1, 1; ]' +endfunction +// The nonlinear constraints and the Jacobian +// matrix of the constraints +function [c,ceq] = confun(x) +// Inequality constraints +c = [1.5 + x(1)*x(2) - x(1) - x(2), -x(1)*x(2) - 10] +// No nonlinear equality constraints +ceq=[] +endfunction +// Defining gradient of confungrad +function [DC,DCeq] = cgrad(x) +// DC(:,i) = gradient of the i-th constraint +// DC = [ +// Dc1/Dx1 Dc1/Dx2 +// Dc2/Dx1 Dc2/Dx2 +// ] +DC= [ +x(2)-1, -x(2) +x(1)-1, -x(1) +]' +DCeq = []' +endfunction +// Test with both gradient of objective and gradient of constraints +minimaxOptions = list("GradObj",myfungrad,"GradCon",cgrad); +// The initial guess +x0 = [0,10]; +// The expected solution : only 4 digits are guaranteed +//xopt = [0.92791 7.93551] +//fopt = [6.73443 -189.778 6.73443 -8.86342 0.86342] +maxfopt = 6.73443 +// Run fminimax +[xopt,fopt,maxfval,exitflag,output] = fminimax(myfun,x0,[],[],[],[],[],[], confun, minimaxOptions) +//========= E N D === O F === D E M O =========// diff --git a/demos/fminunc.dem.sce b/demos/fminunc.dem.sce new file mode 100644 index 0000000..80eed72 --- /dev/null +++ b/demos/fminunc.dem.sce @@ -0,0 +1,61 @@ +mode(1) +// +// Demo of fminunc.sci +// + +//Find x in R^2 such that it minimizes the Rosenbrock function +//f = 100*(x2 - x1^2)^2 + (1-x1)^2 +//Objective function to be minimised +function y= f(x) +y= 100*(x(2) - x(1)^2)^2 + (1-x(1))^2; +endfunction +//Starting point +x0=[-1,2]; +//Gradient of objective function +function y= fGrad(x) +y= [-400*x(1)*x(2) + 400*x(1)^3 + 2*x(1)-2, 200*(x(2)-x(1)^2)]; +endfunction +//Hessian of Objective Function +function y= fHess(x) +y= [1200*x(1)^2- 400*x(2) + 2, -400*x(1);-400*x(1), 200 ]; +endfunction +//Options +options=list("MaxIter", [1500], "CpuTime", [500], "Gradient", fGrad, "Hessian", fHess); +//Calling Ipopt +[xopt,fopt,exitflag,output,gradient,hessian]=fminunc(f,x0,options) +halt() // Press return to continue + +//Find x in R^2 such that the below function is minimum +//f = x1^2 + x2^2 +//Objective function to be minimised +function y= f(x) +y= x(1)^2 + x(2)^2; +endfunction +//Starting point +x0=[2,1]; +//Calling Ipopt +[xopt,fopt]=fminunc(f,x0) +halt() // Press return to continue + +//The below problem is an unbounded problem: +//Find x in R^2 such that the below function is minimum +//f = - x1^2 - x2^2 +//Objective function to be minimised +function y= f(x) +y= -x(1)^2 - x(2)^2; +endfunction +//Starting point +x0=[2,1]; +//Gradient of objective function +function y= fGrad(x) +y= [-2*x(1),-2*x(2)]; +endfunction +//Hessian of Objective Function +function y= fHess(x) +y= [-2,0;0,-2]; +endfunction +//Options +options=list("MaxIter", [1500], "CpuTime", [500], "Gradient", fGrad, "Hessian", fHess); +//Calling Ipopt +[xopt,fopt,exitflag,output,gradient,hessian]=fminunc(f,x0,options) +//========= E N D === O F === D E M O =========// diff --git a/demos/linprog.dem.sce b/demos/linprog.dem.sce new file mode 100755 index 0000000..337a5dc --- /dev/null +++ b/demos/linprog.dem.sce @@ -0,0 +1,64 @@ +mode(1) +// +// Demo of linprog.sci +// + +//Optimal problems +//Linear program, linear inequality constraints +c=[-1,-1/3]' +A=[1,1;1,1/4;1,-1;-1/4,-1;-1,-1;-1,1] +b=[2,1,2,1,-1,2] +[xopt,fopt,exitflag,output,lambda]=linprog(c, A, b) +// Press ENTER to continue +halt() // Press return to continue + +//Linear program with Linear Inequalities and Equalities` +c=[-1,-1/3]' +A=[1,1;1,1/4;1,-1;-1/4,-1;-1,-1;-1,1] +b=[2,1,2,1,-1,2] +Aeq=[1,1/4] +beq=[1/2] +[xopt,fopt,exitflag,output,lambda]=linprog(c, A, b, Aeq, beq) +// Press ENTER to continue +halt() // Press return to continue + +//Linear program with all constraint types +c=[-1,-1/3]' +A=[1,1;1,1/4;1,-1;-1/4,-1;-1,-1;-1,1] +b=[2,1,2,1,-1,2] +Aeq=[1,1/4] +beq=[1/2] +lb=[-1,-0.5] +ub=[1.5,1.25] +[xopt,fopt,exitflag,output,lambda]=linprog(c, A, b, Aeq, beq, lb, ub) +// Press ENTER to continue +halt() // Press return to continue + +//Primal Infeasible Problem +c=[-1,-1,-1]' +A=[1,2,-1] +b=[-4] +Aeq=[1,5,3;1,1,0] +beq=[10,100] +lb=[0,0,0] +ub=[%inf,%inf,%inf] +[xopt,fopt,exitflag,output,lambda]= linprog(c,A,b,Aeq,beq,lb,ub) +// Press ENTER to continue +halt() // Press return to continue + +//Dual Infeasible Problem +c=[3,5,-7]' +A=[-1,-1,4;1,1,4] +b=[-8,5] +Aeq=[] +beq=[] +lb=[-%inf,-%inf,-%inf] +ub=[%inf,%inf,%inf] +[xopt,fopt,exitflag,output,lambda]= linprog(c,A,b,Aeq,beq,lb,ub) +// Press ENTER to continue +halt() // Press return to continue + +filepath = get_absolute_file_path('linprog.dem.sce'); +filepath = filepath + "exmip1.mps" +[xopt,fopt,exitflag,output,lambda] =linprog(filepath); +//========= E N D === O F === D E M O =========// |