Advertisement
Guest User

[C#] Linear Programming with Revised Simplex Method for Prof

a guest
Jun 16th, 2011
4,934
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.14 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Revised_Simplex
  4. {
  5. public class profit
  6. {
  7. public static void revisedSimplex(bool maximize, int n, int m, double[,] a, double epsilon, int[] basicvar)
  8. {
  9. int i, j, k, m2, p, idx = 0;
  10. double[] objcoeff = new double[n + 1];
  11. double[] varsum = new double[n + 1];
  12. double[] optbasicval = new double[m + 3];
  13. double[] aux = new double[m + 3];
  14. double[,] work = new double[m + 3, m + 3];
  15. double part, sum;
  16. bool infeasible, unbound, abort, outres, iterate;
  17. if (maximize)
  18. for (j = 1; j <= n; j++)
  19. a[0, j] = -a[0, j];
  20. infeasible = false;
  21. unbound = false;
  22. m2 = m + 2;
  23. p = m + 2;
  24. outres = true;
  25. k = m + 1;
  26. for (j = 1; j <= n; j++)
  27. {
  28. objcoeff[j] = a[0, j];
  29. sum = 0.0;
  30. for (i = 1; i <= m; i++)
  31. sum -= a[i, j];
  32. varsum[j] = sum;
  33. }
  34. sum = 0.0;
  35. for (i = 1; i <= m; i++)
  36. {
  37. basicvar[i] = n + i;
  38. optbasicval[i] = a[i, 0];
  39. sum -= a[i, 0];
  40. }
  41. optbasicval[k] = 0.0;
  42. optbasicval[m2] = sum;
  43. for (i = 1; i <= m2; i++)
  44. {
  45. for (j = 1; j <= m2; j++)
  46. work[i, j] = 0.0;
  47. work[i, i] = 1.0;
  48. }
  49. iterate = true;
  50. do
  51. {
  52. if ((optbasicval[m2] >= -epsilon) && outres)
  53. {
  54. outres = false;
  55. p = m + 1;
  56. }
  57. part = 0.0;
  58. for (j = 1; j <= n; j++)
  59. {
  60. sum = work[p,m+1] * objcoeff[j] + work[p,m+2] * varsum[j];
  61. for (i = 1; i <= m; i++)
  62. sum += work[p, i] * a[i, j];
  63. if (part > sum)
  64. {
  65. part = sum;
  66. k = j;
  67. }
  68. }
  69. if (part > -epsilon)
  70. {
  71. iterate = false;
  72. if (outres)
  73. infeasible = true;
  74. else
  75. a[0, 0] = -optbasicval[p];
  76. }
  77. else
  78. {
  79. for (i = 1; i <= p; i++)
  80. {
  81. sum = work[i,m+1] * objcoeff[k] + work[i,m+2] * varsum[k];
  82. for (j = 1; j <= m; j++)
  83. sum += work[i, j] * a[j, k];
  84. aux[i] = sum;
  85. }
  86. abort = true;
  87. for (i = 1; i <= m; i++)
  88. if (aux[i] >= epsilon)
  89. {
  90. sum = optbasicval[i] / aux[i];
  91. if (abort || (sum < part))
  92. {
  93. part = sum;
  94. idx = i;
  95. }
  96. abort = false;
  97. }
  98. if (abort)
  99. {
  100. unbound = true;
  101. iterate = false;
  102. }
  103. else
  104. {
  105. basicvar[idx] = k;
  106. sum = 1.0 / aux[idx];
  107. for (j = 1; j <= m; j++)
  108. work[idx, j] *= sum;
  109. i = ((idx == 1) ? 2 : 1);
  110. do
  111. {
  112. sum = aux[i];
  113. optbasicval[i] -= part * sum;
  114. for (j = 1; j <= m; j++)
  115. work[i, j] -= work[idx, j] * sum;
  116. i += ((i == idx - 1) ? 2 : 1);
  117. } while (i <= p);
  118. optbasicval[idx] = part;
  119. }
  120. }
  121. } while (iterate);
  122. // return results
  123. basicvar[m + 1] = (infeasible ? 1 : 0);
  124. basicvar[m + 2] = (unbound ? 1 : 0);
  125. for (i = 1; i <= m; i++)
  126. a[i, 0] = optbasicval[i];
  127. if (maximize)
  128. {
  129. for (j = 1; j <= n; j++)
  130. a[0, j] = -a[0, j];
  131. a[0, 0] = -a[0, 0];
  132. }
  133. }
  134.  
  135.  
  136. static public void Main ()
  137. {
  138. int n = 5;
  139. int m = 3;
  140. double eps = 1.0e-5;
  141. int[] basicvar = new int[m + 3];
  142.  
  143. double[,] a = { { 0, .42, .35, 0, 0, 0},
  144. {100, 6, 0, 1, 0, 0},
  145. { 60, 2, 7, 0, 1, 0},
  146. { 90, 2, 3, 0, 0, 1}};
  147.  
  148. revisedSimplex(true, n, m, a, eps, basicvar);
  149. Console.WriteLine("Suche optimale Mischung für maximalen Gewinn\n");
  150.  
  151. if (basicvar[m + 1] > 0)
  152. Console.WriteLine("Keine Lösung gefunden.");
  153. else
  154. {
  155. if (basicvar[m + 2] > 0)
  156. Console.WriteLine("Keine Lösung gefunden.");
  157. else
  158. {
  159. Console.WriteLine("Optimale Lösung gefunden.\nWerte von CHEWY und NUTTY und Slack-Variablen");
  160. for (int i = 1; i <= m; i++)
  161. Console.WriteLine("{0,10}\t {1,-10}", basicvar[i], a[i, 0]);
  162. Console.WriteLine("Optimaler Wert der Funktion = {0}\n", a[0, 0]);
  163. }
  164. }
  165. }
  166. }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement