6

मैं उन्हें वर्गीकृत करके अपनी कक्षा में रैखिक प्रोग्रामिंग समस्याएं कर रहा हूं लेकिन मैं जानना चाहता हूं कि किसी विशेष समस्या के लिए मुझे प्रोग्राम को कैसे हल करना है। यदि बहुत सारे चर या बाधाएं हैं तो मैं इसे ग्राफिंग द्वारा कभी नहीं कर सकता।हाथ से एक रैखिक प्रोग्रामिंग अभ्यास कोड

उदाहरण समस्या, 5x + 3y को अधिकतम बाधाओं के साथ:

  • 5x - 2> = 0
  • x + y < = 7
  • एक्स < = 5
  • एक्स> = 0
  • y> = 0

मैंने इसे पकड़ लिया और एक दृश्यमान आर 3 कोनों के साथ egion। एक्स = 5 वाई = 2 इष्टतम बिंदु है।

मैं इसे कोड में कैसे बदलूं? मैं सरल विधि के बारे में जानता हूँ। और बहुत महत्वपूर्ण बात यह है कि क्या सभी एलपी समस्याओं को उसी संरचना में कोडित किया जाएगा? बलपूर्वक बल काम करेगा?

+2

सिंप्लेक्स पद्धति का उपयोग करके अपने खुद के मैट्रिक्स वर्ग बारे में है क्या आप चाहते हैं –

+0

यदि आप * इंटीजर * रैखिक प्रोग्रामिंग या * फ्रैक्शनल * रैखिक प्रोग्रामिंग (क्योंकि समस्याओं की जटिलता अलग है) – amit

+3

में [सीमेरिकल रेसिपीज़ सी] (http://apps.nrbook.com/ सी/index.html) ऑनलाइन, सेक्शन 10.8, आप विकिपीडिया के अनुसार सी –

उत्तर

5

वहां बहुत सारे सरल कार्यान्वयन हैं जो आप खोजते हैं तो आपको मिलेगा।

टिप्पणी (सी में न्यूमेरिकल व्यंजनों) में उल्लिखित एक के अलावा, आप भी पा सकते हैं:

  1. Google's own Simplex-Solver
  2. तो फिर वहाँ है COIN-OR
  3. जीएनयू अपनी ही GLPK
  4. है यदि आप सी ++ कार्यान्वयन चाहते हैं, तो Google Code में यह वास्तव में पहुंच योग्य है।
  5. boot package सहित आर में कई कार्यान्वयन हैं। (आर में, आप कोष्टक के बिना यह टाइप करके एक समारोह के कार्यान्वयन देख सकते हैं।)

अपने अन्य दो प्रश्नों को संबोधित करने के लिए:

  1. सभी LPs उसी तरह कोडित किया जाएगा? हां, किसी भी एलपी को लोड और हल करने के लिए एक सामान्य एलपी सॉल्वर लिखा जा सकता है। (वहाँ एल.पी. के mps की तरह और .lp

  2. बल काम जानवर हैं पढ़ने के लिए उद्योग मानक स्वरूप हैं? ध्यान रखें कि कई कंपनियों और बड़े संगठनों ट्यूनिंग पर एक लंबे समय के समाधानकर्ताओं खर्च करते हैं। वहाँ एल.पी. के दिलचस्प होती है गुण है कि कई समाधानकर्ताओं का फायदा उठाने की कोशिश करेंगे। इसके अलावा, कुछ संगणना समानांतर में हल किया जा सकता। एल्गोरिथ्म घातीय है, इसलिए चर/बाधाओं में से कुछ बड़ी संख्या में जानवर बल काम नहीं करेगा।

आशा है कि मदद करता है.

0

मैंने लिखा इस matlab कल, जो आसानी से सेल्सियस के लिए लिखित जा सकता है ++ यदि आप Eigen पुस्तकालय का उपयोग करें या एक std :: एक एसटीडी के वेक्टर :: वेक्टर

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 
संबंधित मुद्दे