Home > src > lpdemo.m

lpdemo

PURPOSE ^

SYNOPSIS ^

This is a script file.

DESCRIPTION ^

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 echo on
0002 
0003 % Script to demonstrate use of the lp_solve toolkit
0004 
0005 clc;
0006 lp=mxlpsolve('make_lp',0,4);
0007 mxlpsolve('add_constraint',lp,[3, 2, 2, 1],3,4);
0008 mxlpsolve('add_constraint',lp,[0, 4, 3, 1],2,3);
0009 mxlpsolve('set_obj_fn',lp,[2, 3, -2, 3]);
0010 result=mxlpsolve('solve',lp)
0011 obj=mxlpsolve('get_objective', lp)
0012 x=mxlpsolve('get_variables', lp)
0013 pause;
0014 
0015 % Change a single element, and maximize
0016 
0017 clc;
0018 mxlpsolve('set_mat',lp,2,1,0.5);
0019 mxlpsolve('set_maxim',lp);
0020 result=mxlpsolve('solve',lp)
0021 obj=mxlpsolve('get_objective', lp)
0022 x=mxlpsolve('get_variables', lp)
0023 pause;
0024 
0025 % Change RHS
0026 
0027 clc;
0028 mxlpsolve('set_rh',lp,1,7.45);
0029 result=mxlpsolve('solve',lp)
0030 obj=mxlpsolve('get_objective', lp)
0031 x=mxlpsolve('get_variables', lp)
0032 pause;
0033 
0034 % Set var 4 to an integer
0035 
0036 clc;
0037 mxlpsolve('set_int',lp,4,1)
0038 result=mxlpsolve('solve',lp)
0039 obj=mxlpsolve('get_objective', lp)
0040 x=mxlpsolve('get_variables', lp)
0041 pause;
0042 
0043 % Put in lower and upper bounds
0044 
0045 clc;
0046 mxlpsolve('set_lowbo',lp,2,2);
0047 mxlpsolve('set_upbo',lp,4,5.3);
0048 result=mxlpsolve('solve',lp)
0049 obj=mxlpsolve('get_objective', lp)
0050 x=mxlpsolve('get_variables', lp)
0051 pause;
0052 
0053 % Delete a constraint
0054 
0055 clc;
0056 mxlpsolve('del_constraint',lp,1);
0057 mxlpsolve('add_constraint',lp,[1, 2, 1, 4],3,8);
0058 result=mxlpsolve('solve',lp)
0059 obj=mxlpsolve('get_objective', lp)
0060 x=mxlpsolve('get_variables', lp)
0061 mxlpsolve('delete_lp',lp)
0062 pause;
0063 
0064 %%%%%%%%%%%%%
0065 
0066 % More examples
0067 
0068 % ex1.lp from the lp_solve distribution
0069 
0070 clc;
0071 lp=mxlpsolve('make_lp',2,2);
0072 mxlpsolve('set_mat',lp,[2, 1;-4, 4]);
0073 mxlpsolve('set_obj_fn',lp,[-1, 2]);
0074 mxlpsolve('set_int',lp,[1,1]);
0075 mxlpsolve('set_rh_vec',lp,[5, 5]);
0076 mxlpsolve('set_maxim',lp);
0077 result=mxlpsolve('solve',lp)
0078 obj=mxlpsolve('get_objective', lp)
0079 x=mxlpsolve('get_variables', lp)
0080 mxlpsolve('delete_lp',lp);
0081 pause;
0082 
0083 % Example 2
0084 
0085 clc;
0086 f = [50, 100];
0087 A = sparse([10, 5;4, 10; 1, 1.5]);
0088 b = [2500, 2000, 450];
0089 e = [-1, -1, -1];
0090 
0091 [m,n] = size(A);
0092 lp=mxlpsolve('make_lp',m,n);
0093 mxlpsolve('set_obj_fn',lp,f);
0094 mxlpsolve('set_mat',lp,A);
0095 mxlpsolve('set_rh_vec',lp,b);
0096 mxlpsolve('set_maxim',lp);
0097 result=mxlpsolve('solve',lp)
0098 obj=mxlpsolve('get_objective', lp)
0099 x=mxlpsolve('get_variables', lp)
0100 mxlpsolve('delete_lp',lp);
0101 pause;
0102 
0103 % Example 3
0104 
0105 clc;
0106 
0107 f = -[40, 36];
0108 vub = [8, 10];
0109 A = sparse([5, 3]);
0110 b = [45];
0111 e = 1;
0112 
0113 [m,n] = size(A);
0114 lp=mxlpsolve('make_lp',m,n);
0115 mxlpsolve('set_obj_fn',lp,f);
0116 mxlpsolve('set_mat',lp,A);
0117 mxlpsolve('set_rh_vec',lp,b);
0118 mxlpsolve('set_constr_type',lp,1,2);
0119 mxlpsolve('set_upbo',lp,1,8);
0120 mxlpsolve('set_upbo',lp,2,10);
0121 mxlpsolve('set_maxim',lp);
0122 result=mxlpsolve('solve',lp)
0123 obj=mxlpsolve('get_objective', lp)
0124 x=mxlpsolve('get_variables', lp)
0125 mxlpsolve('delete_lp',lp);
0126 pause;
0127 
0128 % L1 Data fitting example with integer constraint on the intercept
0129 
0130 % Generate data
0131 
0132 clc;
0133 n = 40;
0134 t = (0:n-1)';
0135 y = 3.5 -.2*t;
0136 y = y + 0.5*randn(size(y));
0137 
0138 m = [ones(n,1),t(:)];
0139 a = [m,-m,speye(n)];
0140 f = -[sum(m),sum(-m),2*ones(1,n)];
0141 e = ones(n,1);
0142 
0143 vub = [10, 10, 10, 10, 5*ones(1,n)];
0144 
0145 [v,x] = lp_solve(f,sparse(a),y,e,[],vub,[1,3]);
0146 p = x(1:2)-x(3:4);
0147 err = y-m*p;
0148 
0149 plot(t,y,'o',t,m*p);
0150 xlabel('t');
0151 ylabel('y');
0152 
0153 disp('Press any key to continue.');
0154 pause;
0155 
0156 clc;
0157 % Now solve bigger problem
0158 
0159 n = 200;
0160 m = 100;
0161 a = rand(m,n);
0162 idx = find(a<0.8);
0163 a(idx) = zeros(length(idx),1);
0164 a = sparse(a);
0165 z = rand(n,1);
0166 b = a*z;
0167 
0168 [v,x] = lp_solve(-ones(1,n),a,b,zeros(m,1));
0169 
0170 plot(a*x-b);
0171 title('Residuals');
0172 xlabel('Equation Number');
0173 
0174 echo off

Generated on Tue 31-May-2016 19:24:56 by m2html © 2005