2012-12-05 118 views
6

我一直在绘制线性编程问题,但我想知道如何编写一个特定问题的程序来为我解决这个问题。如果有太多的变量或限制,我永远无法通过绘图来做到这一点。手动编写线性编程练习

实施例的问题,最大化5X + 3Y与约束:

  • 5倍 - 2Y> = 0
  • X + Y < = 7
  • X < = 5
  • X> = 0
  • y> = 0

我绘制了这个图,得到了一个可见的r带3个角落的天体。 x = 5 y = 2是最佳点。

我该如何将它变成代码?我知道单纯的方法。而且非常重要的是,所有LP问题都将在相同的结构中编码?暴力行为会起作用吗?

+2

单纯形法编写自己的矩阵类是你想要的。 –

+0

如果你正在寻找*整数*线性规划或*分数*线性规划(因为问题的复杂性是不同的),所以答案是不一样的 – amit

+3

In [Numerical Recipes for C](http://apps.nrbook.com/ c/index.html)在线,第10.8节,您可以找到用C编写的Simplex算法的简单实现。 –

回答

5

如果您搜索,您会发现很多Simplex实现。

除了在注释(数字食谱在C)中提到的一个, 你还可以找到:

  1. Google's own Simplex-Solver
  2. 然后是COIN-OR
  3. GNU都有自己GLPK
  4. 如果你想要一个C++实现,这个在Google Code实际上是可以访问的。
  5. R中有许多实现,包括boot package。 (在R,您可以通过不括号键入它看到一个函数的实现。)

为了解决您的另外两个问题:

  1. 将所有有限合伙人可以以同样的方式编码?是的,可以编写一个通用的LP解算器来加载和解决任何LP。 (有行业标准格式读取LP的喜欢mps.lp

  2. 会蛮力工作?请记住,许多公司和大机构花了很长时间在微调的求解器。有LP的有有趣性质,许多求解器将尝试利用。此外,某些计算可以并行求解。该算法是指数的,因此在一些大量的变量/约束,蛮力将无法正常工作。

。希望帮助。

0

这个是我写的matlab昨天,这可以很容易地转录成C++,如果您使用本征库或使用STD的一个std ::矢量:: vector的

function [x, fval] = mySimplex(fun, A, B, lb, up) 

%Examples paramters to show that the function actually works 

% sample set 1 (works for this data set) 

% fun = [8 10 7]; 
% A = [1 3 2; 1 5 1]; 
% B = [10; 8]; 
% lb = [0; 0; 0]; 
% ub = [inf; inf; inf]; 

% sample set 2 (works for this data set) 

fun = [7 8 10]; 
A = [2 3 2; 1 1 2]; 
B = [1000; 800]; 
lb = [0; 0; 0]; 
ub = [inf; inf; inf]; 


% generate a new slack variable for every row of A 

numSlackVars = size(A,1); % need a new slack variables for every row of A 

% Set up tableau to store algorithm data 
tableau = [A; -fun]; 

tableau = [tableau, eye(numSlackVars + 1)]; 

lastCol = [B;0]; 

tableau = [tableau, lastCol]; 

% for convienience sake, assign the following: 

numRows = size(tableau,1); 
numCols = size(tableau,2); 

% do simplex algorithm 

% step 0: find num of negative entries in bottom row of tableau 

numNeg = 0; % the number of negative entries in bottom row 

for i=1:numCols 
    if(tableau(numRows,i) < 0) 
     numNeg = numNeg + 1; 
    end 
end 

% Remark: the number of negatives is exactly the number of iterations needed in the 
% simplex algorithm 

for iterations = 1:numNeg 
    % step 1: find minimum value in last row 
    minVal = 10000; % some big number 
    minCol = 1; % start by assuming min value is the first element 
    for i=1:numCols 
     if(tableau(numRows, i) < minVal) 
      minVal = tableau(size(tableau,1), i); 
      minCol = i; % update the index corresponding to the min element 
     end 
    end 

    % step 2: Find corresponding ratio vector in pivot column 
    vectorRatio = zeros(numRows -1, 1); 
    for i=1:(numRows-1) % the size of ratio vector is numCols - 1 
     vectorRatio(i, 1) = tableau(i, numCols) ./ tableau(i, minCol); 
    end 

    % step 3: Determine pivot element by finding minimum element in vector 
    % ratio 

    minVal = 10000; % some big number 
    minRatio = 1; % holds the element with the minimum ratio 

    for i=1:numRows-1 
     if(vectorRatio(i,1) < minVal) 
      minVal = vectorRatio(i,1); 
      minRatio = i; 
     end 
    end 

    % step 4: assign pivot element 

    pivotElement = tableau(minRatio, minCol); 

    % step 5: perform pivot operation on tableau around the pivot element 

    tableau(minRatio, :) = tableau(minRatio, :) * (1/pivotElement); 

    % step 6: perform pivot operation on rows (not including last row) 

    for i=1:size(vectorRatio,1)+1 % do last row last 
     if(i ~= minRatio) % we skip over the minRatio'th element of the tableau here 
      tableau(i, :) = -tableau(i,minCol)*tableau(minRatio, :) + tableau(i,:); 
     end 
    end 
end 

% Now we can interpret the algo tableau 

numVars = size(A,2); % the number of cols of A is the number of variables 

x = zeros(size(size(tableau,1), 1)); % for efficiency 

% Check for basicity 
for col=1:numVars 
    count_zero = 0; 
    count_one = 0; 
    for row = 1:size(tableau,1) 
     if(tableau(row,col) < 1e-2) 
      count_zero = count_zero + 1; 
     elseif(tableau(row,col) - 1 < 1e-2) 
      count_one = count_one + 1; 
      stored_row = row; % we store this (like in memory) column for later use 
     end 
    end 
    if(count_zero == (size(tableau,1) -1) && count_one == 1) % this is the case where it is basic 
     x(col,1) = tableau(stored_row, numCols); 
    else 
     x(col,1) = 0; % this is the base where it is not basic 
    end 
end 

% find function optimal value at optimal solution 
fval = x(1,1) * fun(1,1); % just needed for logic to work here 
for i=2:numVars 
    fval = fval + x(i,1) * fun(1,i); 
end 


end