Solve the linear programming problem using the simple form method (C ++ implementation code)

Source: Internet
Author: User

Solve the linear programming problem using the simple form method (C ++ implementation code)
1 simple form Method

(1) The simple shape method is an important method to solve the linear programming problem.
The basic framework is as follows:
Step 1: Change LP linear planning to a standard type to determine an Initial Feasible Solution (vertex ).
Step 2: determine the optimum of the initial feasible solution. If the optimal solution is used, stop the process. Otherwise, go to the next step.
Step 3: Change the initial base feasible solution to the adjacent base feasible solution (vertex), and improve the target value-increase the value of the target function, repeat steps 2 and 3 until the optimal solution is found.
(2) Before using a program for computation, convert the target function and the constraint equation into a standard form.

The following transformations must be performed in non-standard forms:
A) when the target function is min z = CX, it is converted to the form of max z =-CX;
B) when there is a constraint equation "≤", add a relaxation variable;
C) when the constraint equation contains "≥", subtract a relaxation variable and add an artificial variable;
D) add an artificial variable when the constraint equation contains "=;
E) for all the manual variables, the target function coefficient of the relaxed variable is set to 0.
(3) Linear Programming in the standard form. The block diagram of the calculation step is calculated using the simple form method.

2 program testing and results:

The linear programming problems are as follows:
Max z = 2 * x1-3 * x2 + 3x3;
X1 + x2-x3 <= 7;
X1-x2 + x3 <=-7;
X1-2 * x2 + 2 * x3 <= 4;
X1, x2, x3> = 0;

3 C ++ implementation code
// Simplex. cpp: defines the entry point of the console application. //// ********************************* --------------------------------- Simple method for Solving Linear Programming (C ++ implementation code) ----------------------------------- Author: Muzhi, Date: 2014 Email: bzhou84@163.com **********************************/# include stdafx. h # include
  
   
# Include
   
    
Using namespace std; # define M 10000 // large global variable M float juzhen [11] [31]; // core matrix table int m = 0, n = 0, t = 0; // m: Number of structure vectors // n: Number of constrained inequalities // t: Target Function Type:-1 indicates minimum value, 1 represents the maximum value void input () // input interface function {int I, j; cout <---------- input the parameter NUMBER OF THE SIMPLE METHOD --------- <
    
     
> M; cout <
     
      
> N; for (I = 0; I <= n + 1; I ++) for (j = 0; j <= m + n; j ++) juzhen [I] [j] = 0; // initialization matrix, all elements are 0 // read constraints cout <
      
        =): <
       
         > Juzhen [I] [j];} for (I = 1; I <= n; I ++) {juzhen [I] [0] = juzhen [I] [m + 2]; juzhen [I] [m + 2] = 0;} // read target condition cout <
        
          > Juzhen [0] [I]; cin> t; // adjust the matrix if (t =-1) for (I = 1; I <= m; I ++) juzhen [0] [I] = (-1) * juzhen [0] [I]; for (I = 1; I <= n; I ++) {juzhen [I] [m + I] = juzhen [I] [m + 1]; if (I! = 1) juzhen [I] [m + 1] = 0 ;}// algorithm function void comput () {int I, j, flag, temp1, temp2, h, k = 0, temp3 [10]; float a, B [11], temp, temp4 [11], temp5 [11], f = 0, aa, d, c; // initialize for (I = 1; I <= n; I ++) temp3 [I] = 0; for (I = 0; I <11; I ++) {temp4 [I] = 0; temp5 [I] = 0;} for (I = 1; I <= n; I ++) {if (juzhen [I] [m + I] =-1) {juzhen [I] [m + n + I] = 1; juzhen [0] [m + n + I] = M; temp3 [I] = m + n + I;} else temp3 [I] = m + I ;} for (I = 1; I <= n; I ++) temp4 [I] = juzhen [0] [temp3 [I]; // cyclically solve do {for (I = 1; I <= m + n; I ++) {a = 0; for (j = 1; j <= n; j ++) a + = juzhen [j] [I] * temp4 [j]; juzhen [n + 1] [I] = juzhen [0] [I]-a;} for (I = 1; I <= m + n; I ++) {if (juzhen [n + 1] [I]> = 0) flag = 1; else {flag =-1; break ;}} if (flag = 1) {for (I = 1; I <= n; I ++) {if (temp3 [I] <= m + n) temp1 = 1; else {temp1 =-1; break ;}/// output result cout <
         
        
       
      
     
    
   
  

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.