📄 ex.m
字号:
% Test M-file for lp_solveclc;echo on;% Test and Demonstrate the use of lp_solve.mex4echo off;disp('Press any key to continue.');pauseecho on;clc;% Example 1 from the lp_solve distributionf = [-1 2]';A = [2 1;-4 4];b = [5 5];e = -[1 1];xint = [1 2];[v,x] = lp_solve(f,sparse(A),b,e,[],[],xint)echo off;disp('End of Ex1. Expected Solution: v=3, x=[1, 2]. Press any key to continue.');pauseecho on;clc;% Example 2f = [50 100];A = sparse([10 5;4 10; 1 1.5]);b = [2500 2000 450];e = [-1 -1 -1];[v,x] = lp_solve(f,A,b,e)echo off;disp('End of Ex2. Press any key to continue.');pauseecho on;clc;% Example 3f = -[40 36];vub = [8 10];A = sparse([5 3]);b = [45];e = 1;[v,x] = lp_solve(f,A,b,e,[],vub)echo off;disp('End of Ex3. Press any key to continue.');pauseecho on;clc;% Example 4f = [10 6 4];A = [1 1 1;10 4 5;2 2 6];A = sparse(A);b = [100 600 300];e = [-1 -1 -1];xint = [2];[v,x] = lp_solve(f,A,b,e,[],[],xint)echo off;disp('End of Ex4. Press any key to continue.');pauseecho on;clc;% Example 5% Integer programming example, page 218 of Ecker & Kupferschmidf = -[3 -7 -12];a = [-3 6 8;6 -3 7;-6 3 3];a = sparse(a);b = [12 8 5];e = [-1 -1 -1];xint = [1 2 3];[v,x] = lp_solve(f,a,b,e,[],[],xint)echo off;disp('End of Ex5. Press any key to continue.');pauseecho on;clc;% Example 6% 0-1 programming example, page 228 233 of Ecker & Kupferschmidf = -[2 3 7 7];a = [1 1 -2 -5;-1 2 1 4];a = sparse(a);b = [2 -3];e = [1 1];xint = [1 2 3 4];vub = [1 1 1 1];[v,x] = lp_solve(f,a,b,e,[],vub,xint)echo off;disp('End of Ex6. Press any key to continue.');pauseecho on;clc;% Example 7% 0-1 programming example, page 238 of Ecker & Kupferschmidf = -[1 2 3 7 8 8];a = [5 -3 2 -3 -1 2; -1 0 2 1 3 -3;1 2 -1 0 5 -1];b = [-5 -1 3];e = [1 1 1];xint = [1 2 3 4 5 6];vub = [1 1 1 1 1 1];[v,x] = lp_solve(f,sparse(a),b,e,[],vub,xint)echo off;disp('End of Ex7. Press any key to continue.');pauseecho on;clc;% Example 8% A knapsack problem% The problem is to maximize the sum of objectsn = 5; % Number of objectsf = ones(n,1);% subject to a constraint not to fill up the baga = rand(1,n);b = [1];e = [-1];xint = 1:n;vub = ones(n,1);[v,x] = lp_solve(f,sparse(a),b,e,[],vub,xint)echo off;disp('End of Ex8. Press any key to continue.');pauseecho on;clc;% Example 9% L1 Data fitting example with integer constraint on the interceptn = 40;t = (0:n-1)';y = 3.5 -.2*t;y = y + 0.5*randn(size(y));m = [ones(n,1),t(:)];a = [m,-m,speye(n)];f = -[sum(m),sum(-m),2*ones(1,n)];e = ones(n,1);vub = [10 10, 10 10, 5*ones(1,n)];[v,x] = lp_solve(f,sparse(a),y,e,[],vub,[1,3]);p = x(1:2)-x(3:4);err = y-m*p;plot(t,y,'o',t,m*p);xlabel('t');ylabel('y');echo off;disp('End of Ex9. Press any key to continue.');pauseecho on;clc;% Example 10% Now solve bigger problemn = 200;m = 100;a = rand(m,n);idx = find(a<0.8);a(idx) = zeros(length(idx),1);a = sparse(a);z = rand(n,1);b = a*z;[v,x] = lp_solve(-ones(1,n),a,b,zeros(m,1));plot(a*x-b);title('Residuals');xlabel('Equation Number');echo off;disp('End of Ex10. Press any key to continue.');pauseecho on;clc;% Example 11% ex2.lp from the lp_solve distributionf=[8 15];a = [10 21;2 1];a = sparse(a);b = [156 22];e = [-1 -1];[v,x] = lp_solve(f,a,b,e)echo off;disp('End of Ex11. Expected Solution: v=119.625, x=[9.5625, 2.875]. Press any key to continue.');pauseecho on;clc;% Example 12% ex3.lp from the lp_solve distributionf=[3 13];a = [2 9;11 -8];a = sparse(a);b = [40 82];e = [-1 -1];[v,x] = lp_solve(f,a,b,e)echo off;disp('End of Ex12. Expected Solution: v=58.8, x=[9.2, 2.4]. Press any key to continue.');pauseecho on;clc;% Example 13% ex6.lp from the lp_solve distributionf=[592 381 273 55 48 37 23];a = [3534 2356 1767 589 528 451 304];a = sparse(a);b = [119567];e = [-1];xint = [1 2 3 4 5 6 7];vub = [];[v,x] = lp_solve(f,a,b,e,[],vub,xint)echo off;disp('End of Ex13. Expected Solution: v=19979, x=[32,2,1,0,0,0,0]. Press any key to continue.');pauseecho on;clc;% Example 14% ex7.lp from the lp_solve distribution% This works as a test for autoscale options, it will NOT% work properly without scalingf=[0,0,0,0,0,0,0,0,0,0,0,0,-1,-1,1,1];echo off;disp('a=[...large, 29x16...]');a=[0,0,0,0,0,0,0,+100,0,0,0,0,0,-406.9903088236362691532121971,...0,+406.9903088236362691532121971,;0,0,0,0,0,0,0,+100,0,0,0,0,...0,-319.0773778073742619199038018,0,+319.0773778073742619199038018,...;0,0,0,0,0,0,0,+99.9999999999999857891452848,0,0,0,0,0,-287.8545880453748964100668672,...0,+287.8545880453748964100668672,;0,0,0,0,0,0,0,+100,0,0,0,0,...0,-329.7778359794653511016804259,0,+329.7778359794653511016804259,...;0,0,0,0,0,0,0,+100,0,0,0,0,0,-329.7778359794653511016804259,...0,+329.7778359794653511016804259,;0,0,0,0,0,0,0,+100,0,0,0,0,...0,-352.0403706295338110976445023,0,+352.0403706295338110976445023,...;0,0,0,0,0,0,0,+99.9999999999999857891452848,0,0,0,0,0,-287.8545880453748964100668672,...0,+287.8545880453748964100668672,;0,0,0,0,0,0,0,+100,0,0,0,0,...0,-322.6669947786614898177504074,0,+322.6669947786614898177504074,...;0,0,0,0,0,0,0,+99.9999999999999857891452848,0,0,0,0,0,-287.8545880453748964100668672,...0,+287.8545880453748964100668672,;0,+100,+200.5050277777777978371887002,...+200.5050277777777978371887002,0,+100,+281.5962205081120259819726925,...0,0,+100,+281.5962205081120259819726925,+114.8098208928219747804178041,...-237.0370370370370380896929419,0,+237.0370370370370380896929419,...0,;+100,+133.3333333333333143855270464,+267.3400370370370069394994061,...+375.4616273441493490281573031,+100,+133.3333333333333143855270464,...
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -