Advertisement
Guest User

Untitled

a guest
Aug 28th, 2014
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.49 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. vector<vector<double>> SetSimplex( vector<double> maxFunction,
  4.        vector<vector<double>> A,
  5.        vector<double> b)
  6. {
  7.  vector<vector<double>> simplex;
  8.  
  9.  
  10.  int numVariables = maxFunction.size();
  11.  int numEquations = A.size();
  12.  int numCols = numVariables + numEquations + 1 + 1;
  13.  
  14.  
  15.  for(int iRow = 0; iRow < numEquations; iRow++)
  16.  {
  17.   vector<double> row(numCols, 0);
  18.   for(int iCol = 0; iCol < numVariables; iCol++)
  19.   {
  20.    row[iCol] = A[iRow][iCol];
  21.   }
  22.   row[numVariables + iRow] = 1;
  23.   row[numCols - 1] = b[iRow];
  24.  
  25.  
  26.   simplex.push_back( row );
  27.  }
  28.  
  29.  
  30.  vector<double> lastRow(numCols, 0);
  31.  for(int iVar = 0; iVar < numVariables; iVar++)
  32.  {
  33.   lastRow[iVar] = 0 - maxFunction[iVar];
  34.  }
  35.  lastRow[numVariables + numEquations] = 1;
  36.  simplex.push_back(lastRow);
  37.  
  38.  
  39.  return simplex;
  40. }
  41.  
  42.  
  43.  
  44.  
  45.  
  46. bool GetPivots(vector<vector<double>> simplex, int & pivotCol, int & pivotRow, bool & noSolution)
  47. {
  48.  int numRows = simplex.size();
  49.  int numCols = simplex[0].size();
  50.  int numVariables = numCols - numRows - 1;
  51.  
  52.  
  53.  noSolution = false;
  54.  
  55.  
  56.  double min = 0;
  57.  for(int iCol = 0; iCol < numCols - 2; iCol++)
  58.  {
  59.   double value = simplex[numRows - 1][iCol];
  60.   if(value < min)
  61.   {
  62.    pivotCol = iCol;
  63.    min = value;
  64.   }
  65.  }
  66.  
  67.  
  68.  if(min == 0)
  69.   return false;
  70.  
  71.  
  72.  double minRatio = 0.0;
  73.  bool first = true;
  74.  for(int iRow = 0; iRow < numRows - 1; iRow++)
  75.  {
  76.   double value = simplex[iRow][pivotCol];
  77.  
  78.   if(value > 0.0)
  79.   {
  80.    double colValue = simplex[iRow][numCols - 1];
  81.    double ratio = colValue / value;
  82.  
  83.  
  84.    if((first || ratio < minRatio) && ratio >= 0.0)
  85.    {
  86.     minRatio = ratio;
  87.     pivotRow = iRow;
  88.     first = false;
  89.    }
  90.   }
  91.  }
  92.  
  93.  
  94.  noSolution = first;
  95.  return !noSolution;
  96. }
  97.  
  98.  
  99. vector<double> DoSimplex(vector<vector<double>> simplex, double & max)
  100. {
  101.  int pivotCol, pivotRow;
  102.  int numRows = simplex.size();
  103.  int numCols = simplex[0].size();
  104.  
  105.  
  106.  bool noSolution = false;
  107.  while( GetPivots(simplex, pivotCol, pivotRow, noSolution) )
  108.  {
  109.   double pivot = simplex[pivotRow][pivotCol];
  110.  
  111.   for(int iCol = 0; iCol < numCols; iCol++)
  112.   {
  113.    simplex[pivotRow][iCol] /= pivot;
  114.   }
  115.  
  116.  
  117.   for(int iRow = 0; iRow < numRows; iRow++)
  118.   {
  119.    if(iRow != pivotRow)
  120.    {
  121.     double ratio =  -1 * simplex[iRow][pivotCol];
  122.     for(int iCol = 0; iCol < numCols; iCol++)
  123.     {
  124.      simplex[iRow][iCol] = simplex[pivotRow][iCol] * ratio + simplex[iRow][iCol];
  125.     }
  126.    }
  127.   }
  128.  }
  129.  
  130.  
  131.  if(noSolution)
  132.  {
  133.   vector<double> vec;
  134.   return vec;
  135.  }
  136.  
  137.  //optimo!!!
  138.  max = simplex[numRows-1][numCols-1];
  139.  int numVariables = numCols - numRows - 1;
  140.  vector<double> x(numVariables, 0);
  141.  
  142.  for(int iCol = 0; iCol < numVariables; iCol++)
  143.  {
  144.   bool isUnit = true;
  145.   bool first = true;
  146.   double value;
  147.   for(int j = 0; j < numRows; j++)
  148.   {
  149.    if(simplex[j][iCol] == 1.0 && first)
  150.    {
  151.     first = false;
  152.     value = simplex[j][numCols - 1];
  153.    }
  154.    else if(simplex[j][iCol] != 0.0)
  155.    {
  156.     isUnit = false;
  157.    }
  158.   }
  159.  
  160.  
  161.   if(isUnit && !first)
  162.    x[iCol] = value;
  163.   else
  164.    x[iCol] = 0.0;
  165.  }
  166.  
  167.  
  168.  return x;
  169. }
  170. string cleanSpaces(string str)
  171. {
  172.  string space = " ";
  173.  int pos;
  174.  while((pos = str.find(space)) != string::npos)
  175.  {
  176.   str = str.erase(pos, 1);
  177.  }
  178.  return str;
  179. }
  180.  
  181.  
  182. vector<double> commaSeparatedStringToDoubleVector(string str)
  183. {
  184.  vector<double> vec;
  185.  while(str.length() > 0)
  186.  {
  187.   int pos = str.find(",");
  188.   if (pos!=string::npos)
  189.   {
  190.    string strNum = str.substr(0, pos);
  191.    strNum = cleanSpaces(strNum);
  192.    vec.push_back(atof(strNum.c_str()));
  193.    str = str.substr(pos+1);
  194.   }
  195.   else
  196.   {
  197.    string strNum = cleanSpaces(str.c_str());
  198.    vec.push_back(atof(strNum.c_str()));
  199.    break;
  200.   }
  201.  }
  202.  return vec;
  203. }
  204. void Run1()
  205. {
  206.                //     x1    x2     x3
  207.  string maxFuncStr = "8.0 , 10.0 , 7.0";
  208.  vector<double> maxFunc = commaSeparatedStringToDoubleVector(maxFuncStr);
  209.  
  210.  vector<vector<double>> A;
  211.                                       //         x1     x2    x3
  212.  A.push_back(commaSeparatedStringToDoubleVector("1.0 , 3.0 , 2.0"));
  213.  A.push_back(commaSeparatedStringToDoubleVector("1.0 , 5.0 , 1.0"));
  214.  
  215.              //  b1     b2
  216.  string bStr = "10.0 , 8.0";
  217.  vector<double> b = commaSeparatedStringToDoubleVector(bStr);
  218.  
  219.  vector<vector<double>> simplex = SetSimplex(maxFunc, A, b);
  220.  
  221.  double max;
  222.  vector<double> result = DoSimplex(simplex, max);
  223.  
  224.  printf("Result: Max = %f\n", max);
  225.  for(int i = 0; i < result.size(); i++)
  226.  {
  227.   printf("x%d = %f ; ", i, result[i]);
  228.  }
  229.  printf("\n----------------------\n");
  230. }
  231.  
  232.  
  233.  
  234. void Run2()
  235. {
  236.          //           x1    x2    x3    x4
  237.  string maxFuncStr = "0.5 , 3.0 , 1.0 , 4.0";
  238.  vector<double> maxFunc = commaSeparatedStringToDoubleVector(maxFuncStr);
  239.  
  240.  vector<vector<double>> A;
  241.                                          //       x1     x2    x3    x4
  242.  A.push_back(commaSeparatedStringToDoubleVector(" 1.0 ,  1.0 , 1.0 ,  1.0"));
  243.  A.push_back(commaSeparatedStringToDoubleVector("-2.0 , -1.0 , 1.0 ,  1.0"));
  244.  A.push_back(commaSeparatedStringToDoubleVector(" 0.0 ,  1.0 , 0.0 , -1.0"));
  245.  
  246.              //  b1     b2     b3
  247.  string bStr = "40.0 , 10.0 , 10.0";
  248.  vector<double> b = commaSeparatedStringToDoubleVector(bStr);
  249.  
  250.  vector<vector<double>> simplex = SetSimplex(maxFunc, A, b);
  251.  
  252.  double max;
  253.  vector<double> result = DoSimplex(simplex, max);
  254.  
  255.  printf("Result: Max = %f\n", max);
  256.  for(int i = 0; i < result.size(); i++)
  257.  {
  258.   printf("x%d = %f ; ", i, result[i]);
  259.  }
  260.  printf("\n----------------------\n");
  261. }
  262.  
  263.  
  264. int main ()
  265. {
  266.  Run1();
  267.  Run2();
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement