Вы находитесь на странице: 1из 23

/*********************************************************

'*
SIMPLEX METHOD
*
'*
-------------*
'*
*
'* LIST OF MAIN VARIABLES:
*
'*
*
'* R:
MAXIMIZE = Y, MINIMIZE = N
*
'* NV:
NUMBER OF VARIABLES OF ECONOMIC FUNCTION *
'*
(TO MAXIMIZE OR MINIMIZE).
*
'* NC:
NUMBER OF CONSTRAINTS
*
'* TS:
SIMPLEX TABLE OF SIZE NC+1 x NV+1
*
'* R1:
=1 TO MAXIMIZE, =-1 TO MINIMIZE
*
'* R2:
AUXILIARY VARIABLE FOR INPUTS
*
'* NOPTIMAL
BOOLEAN IF FALSE, CONTINUE ITERATIONS
*
'* XMAX:
STORES GREATER COEFFICIENT OF ECONOMIC
*
'*
FUNCTION.
*
'* RAP
STORES SMALLEST RATIO > 0
*
'* V:
AUXILIARY VARIABLE
*
'* P1,P2:
LINE, COLUMN INDEX OF PIVOT
*
'* XERR:
BOOLEAN IF TRUE, NO SOLUTION.
*
'* ----------------------------------------------------- *
'* PROBLEM DESCRIPTION:
*
'* A builder of houses can make 3 kinds of them with
*
'* various profits: 15000$, 17000$ and 20000$.
*
'* Each kind must respect following conditions:
*
'* 1) for supply reasons, the number of houses of kind 2 *
'*
built each month must not exceed the number of
*
'*
kind 3 by more than two.
*
'* 2) for staff reasons, the buider can make each month *
'*
up to 5, 5 and 3, respectively of kinds 1, 2 and 3.*
'* 3) for organisation reasons, the builder can make up *
'*
to 8 houses monthly of kinds 1,2 and 3, respecti- *
'*
vely in the proportions of 3, 2 and 1.
*
'* The builder, having these data, wants to maximize his *
'* monthly profit by knowing the number oh houses of
*
'* each kind to build monthly.
*
'* ----------------------------------------------------- *
'* SAMPLE RUN:
*
'* (Maximize 15 X1 + 17 X2 + 20 X3 with conditions:
*
'*
X2 X3 <= 2
*
'*
3 X1 + 3 X2 + 5 X3 <= 15
*
'*
3 X1 + 2 X2 +
X3 <= 8
)
*
'*
*
'* LINEAR PROGRAMMING
*
'*
*
'* MAXIMIZE ? Y
*
'*
*
'* NUMBER OF VARIABLES OF ECONOMIC FUNCTION ? 3
*
'*
*
'* NUMBER OF CONSTRAINTS ? 3
*
'*
*
'* INPUT COEFFICIENTS OF ECONOMIC FUNCTION:
*
'*
#1 ? 15
*
'*
#2 ? 17
*
'*
#3 ? 20
*
'*
Right hand side ? 0
*
'*
*

'* CONSTRAINT #1:


*
'*
#1 ? 0
*
'*
#2 ? 1
*
'*
#3 ? -1
*
'*
Right hand side ? 2
*
'*
*
'* CONSTRAINT #2:
*
'*
#1 ? 3
*
'*
#2 ? 3
*
'*
#3 ? 5
*
'*
Right hand side ? 15
*
'*
*
'* CONSTRAINT #3:
*
'*
#1 ? 3
*
'*
#2 ? 2
*
'*
#3 ? 1
*
'*
Right hand side ? 8
*
'*
*
'* RESULTS:
*
'*
*
'*
VARIABLE #1: 0.333333
*
'*
VARIABLE #2: 3.000000
*
'*
VARIABLE #3: 1.000000
*
'*
*
'*
ECONOMIC FUNCTION: 76.000000
*
'*
*
'* (Building monthly 1/3, 3 and 1 house(s) of kinds 1,2, *
'* 3, the builder can make a monthly profit of 76000$). *
'* ----------------------------------------------------- *
'* REFERENCE:
*
'* Modles pratiques de dcision Tome 2, By Jean-Pierre *
'* Blanger, PSI Editions, France, 1982.
*
'*
*
'*
C++ Release 1.0 By J-P Moreau, Paris. *
'********************************************************/
#include <stdio.h>
#include <math.h>
#define CMAX
#define VMAX

10
10

//max. number of variables in economic function


//max. number of constraints

int NC, NV, NOPTIMAL,P1,P2,XERR;


double TS[CMAX][VMAX];
void Data() {
double R1,R2;
char R;
int I,J;
printf("\n LINEAR PROGRAMMING\n\n");
printf(" MAXIMIZE (Y/N) ? "); scanf("%c", &R);
printf("\n NUMBER OF VARIABLES OF ECONOMIC FUNCTION ? "); scanf("%d",
&NV);
printf("\n NUMBER OF CONSTRAINTS ? "); scanf("%d", &NC);
if (R == 'Y' || R=='y')
R1 = 1.0;
else

R1 = -1.0;
printf("\n INPUT COEFFICIENTS OF ECONOMIC FUNCTION:\n");
for (J = 1; J<=NV; J++) {
printf("
#%d ? ", J); scanf("%lf", &R2);
TS[1][J+1] = R2 * R1;
}
printf("
Right hand side ? "); scanf("%lf", &R2);
TS[1][1] = R2 * R1;
for (I = 1; I<=NC; I++) {
printf("\n CONSTRAINT #%d:\n", I);
for (J = 1; J<=NV; J++) {
printf("
#%d ? ", J); scanf("%lf", &R2);
TS[I + 1][J + 1] = -R2;
}
printf("
Right hand side ? "); scanf("%lf", &TS[I+1][1]);
}
printf("\n\n RESULTS:\n\n");
for(J=1; J<=NV; J++) TS[0][J+1] = J;
for(I=NV+1; I<=NV+NC; I++) TS[I-NV+1][0] = I;
}
void Pivot();
void Formula();
void Optimize();
void Simplex() {
e10: Pivot();
Formula();
Optimize();
if (NOPTIMAL == 1) goto e10;
}
void Pivot() {
double RAP,V,XMAX;
int I,J;
XMAX = 0.0;
for(J=2; J<=NV+1; J++) {
if (TS[1][J] > 0.0 && TS[1][J] > XMAX) {
XMAX = TS[1][J];
P2 = J;
}
}
RAP = 999999.0;
for (I=2; I<=NC+1; I++) {
if (TS[I][P2] >= 0.0) goto e10;
V = fabs(TS[I][1] / TS[I][P2]);
if (V < RAP) {
RAP = V;
P1 = I;
}
e10:;}
V = TS[0][P2]; TS[0][P2] = TS[P1][0]; TS[P1][0] = V;
}
void Formula() {;

//Labels: e60,e70,e100,e110;
int I,J;
for (I=1; I<=NC+1; I++) {
if (I == P1) goto e70;
for (J=1; J<=NV+1; J++) {
if (J == P2) goto e60;
TS[I][J] -= TS[P1][J] * TS[I][P2] / TS[P1][P2];
e60:;}
e70:;}
TS[P1][P2] = 1.0 / TS[P1][P2];
for (J=1; J<=NV+1; J++) {
if (J == P2) goto e100;
TS[P1][J] *= fabs(TS[P1][P2]);
e100:;}
for (I=1; I<=NC+1; I++) {
if (I == P1) goto e110;
TS[I][P2] *= TS[P1][P2];
e110:;}
}
void Optimize() {
int I,J;
for (I=2; I<=NC+1; I++)
if (TS[I][1] < 0.0) XERR = 1;
NOPTIMAL = 0;
if (XERR == 1) return;
for (J=2; J<=NV+1; J++)
if (TS[1][J] > 0.0) NOPTIMAL = 1;
}
void Results() {
//Labels: e30,e70,e100;
int I,J;
if (XERR == 0) goto e30;
printf(" NO SOLUTION.\n"); goto e100;
e30:for (I=1; I<=NV; I++)
for (J=2; J<=NC+1; J++) {
if (TS[J][0] != 1.0*I) goto e70;
printf("
VARIABLE #%d: %f\n", I, TS[J][1]);
e70: ;}
printf("\n
ECONOMIC FUNCTION: %f\n", TS[1][1]);
e100:printf("\n");
}
void main()

Data();
Simplex();
Results();
}
//end of file simplex.cpp

/***************************************************************
*
LINEAR PROGRAMMING: THE SIMPLEX METHOD
*
* ------------------------------------------------------------ *
* SAMPLE RUN:
*
* Maximize z = x1 + x2 + 3x3 -0.5x4 with conditions:
*
*
x1 + 2x3 <= 740
*
*
2x2 - 7x4 <= 0
*
*
x2 - x3 + 2x4 >= 0.5
*
*
x1 + x2 + x3 +x4 = 9
*
*
and all x's >=0.
*
*
*
* Number of variables in E.F.: 4
*
* Number of <= inequalities..: 2
*
* Number of >= inequalities..: 1
*
* Number of = equalities.....: 1
*
* Input Economic Function:
*
* Coefficient # 1: 1
*
* Coefficient # 2: 1
*
* Coefficient # 3: 3
*
* Coefficient # 4: -0.5
*
* Constant term..: 0
*
* Input constraint # 1:
*
* Coefficient # 1: 1
*
* Coefficient # 2: 0
*
* Coefficient # 3: 2
*
* Coefficient # 4: 0
*
* Constant term..: 740
*
* Input constraint # 2:
*
* Coefficient # 1: 0
*
* Coefficient # 2: 2
*
* Coefficient # 3: 0
*
* Coefficient # 4: -7
*
* Constant term..: 0
*
* Input constraint # 3:
*
* Coefficient # 1: 0
*
* Coefficient # 2: 1
*
* Coefficient # 3: -1
*
* Coefficient # 4: 2
*
* Constant term..: 0.5
*
* Input constraint # 4:
*
* Coefficient # 1: 1
*
* Coefficient # 2: 1
*
* Coefficient # 3: 1
*
* Coefficient # 4: 1
*
* Constant term..: 9
*
*
*
* Input Table:
*
*
0.00
1.00
1.00
3.00
-0.50
*
* 740.00
-1.00
0.00
-2.00
0.00
*
*
0.00
0.00
-2.00
0.00
7.00
*
*
0.50
0.00
-1.00
1.00
-2.00
*
*
9.00
-1.00
-1.00
-1.00
-1.00
*
*
*
* Maximum of E.F. =
17.02500
*
* X1 =
0.000000
*
* X2 =
3.325000
*
* X3 =
4.725000
*

* X4 =
0.950000
*
*
*
* ------------------------------------------------------------ *
* Reference: "Numerical Recipes By W.H. Press, B. P. Flannery, *
*
S.A. Teukolsky and W.T. Vetterling, Cambridge
*
*
University Press, 1986" [BIBLI 08].
*
*
*
*
C++ Release 1.0 By J-P Moreau, Paris
*
***************************************************************/
#include <stdio.h>
#include <math.h>
#define
#define
#define

MMAX
NMAX
REAL

25
25
double

typedef REAL MAT[MMAX][NMAX];


MAT
int
int
REAL

A;
IPOSV[MMAX], IZROV[NMAX];
i,j,ICASE,N,M,M1,M2,M3;
R;

void simp1(MAT,int,int *,int,int,int *,REAL *);


void simp2(MAT,int,int,int *,int,int *,int,REAL *);
void simp3(MAT,int,int,int,int);
void simplx(MAT a,int m,int n,int m1,int m2,int m3,int *icase,int *izrov, int
*iposv) {
/*--------------------------------------------------------------------------------------USES simp1,simp2,simp3
Simplex method for linear programming. Input parameters a, m, n, mp, np, m1,
m2, and m3,
and output parameters a, icase, izrov, and iposv are described above (see
reference).
Parameters: MMAX is the maximum number of constraints expected; NMAX is the
maximum number
of variables expected; EPS is the absolute precision, which should be
adjusted to the
scale of your variables.
----------------------------------------------------------------------------------------*/
int i,ip,ir,is,k,kh,kp,m12,nl1,nl2,l1[NMAX],l2[MMAX],l3[MMAX];
REAL bmax,q1,EPS=1e-6;
if(m != m1+m2+m3) {
printf(" Bad input constraint counts in simplx.\n");
return;
}
nl1=n;
for (k=1; k<=n; k++) {
l1[k]=k;
//Initialize index list of columns admissible for exchange.
izrov[k]=k; //Initially make all variables right-hand.
}
nl2=m;

for (i=1; i<=m; i++) {


if (a[i+1][1] < 0.0) {
printf(" Bad input tableau in simplx, Constants bi must be
nonnegative.\n");
return;
}
l2[i]=i;
iposv[i]=n+i;
/*----------------------------------------------------------------------------------------------Initial left-hand variables. m1 type constraints are represented by having
their slackv ariable
initially left-hand, with no artificial variable. m2 type constraints have
their slack
variable initially left-hand, with a minus sign, and their artificial
variable handled implicitly
during their first exchange. m3 type constraints have their artificial
variable initially
left-hand.
-----------------------------------------------------------------------------------------------*/
}
for (i=1; i<=m2; i++) l3[i]=1;
ir=0;
if(m2+m3 == 0) goto e30; //The origin is a feasible starting solution. Go
to phase two.
ir=1;
for (k=1; k<=n+1; k++) { //Compute the auxiliary objective function.
q1=0.0;
for (i=m1+1; i<=m; i++) q1 += a[i+1][k];
a[m+2][k]=-q1;
}
e10: simp1(a,m+1,l1,nl1,0,&kp,&bmax);
//Find max. coeff. of auxiliary
objective fn
if(bmax <= EPS && a[m+2][1] < -EPS) {
*icase=-1;
//Auxiliary objective function is still negative and cant
be improved,
return;
//hence no feasible solution exists.
}
else if (bmax <= EPS && a[m+2][1] <= EPS) {
//Auxiliary objective function is zero and cant be improved; we have a
feasible starting vector.
//Clean out the artificial variables corresponding to any remaining equality
constraints by
//goto 1s and then move on to phase two by goto 30.
m12=m1+m2+1;
if (m12 <= m)
for (ip=m12; ip<=m; ip++)
if(iposv[ip] == ip+n) {
//Found an artificial variable for
an equalityconstraint.
simp1(a,ip,l1,nl1,1,&kp,&bmax);
if(bmax > EPS) goto e1; //Exchange with column corresponding to
maximum
}
//pivot element in row.
ir=0;
m12=m12-1;
if (m1+1 > m12) goto e30;

for (i=m1+1; i<=m1+m2; i++)


constraints
if(l3[i-m1] == 1)
for (k=1; k<=n+1; k++)
a[i+1][k] *= -1.0;
goto e30;
}

//Change sign of row for any m2


//still present from the initial basis.
//Go to phase two.

simp2(a,m,n,l2,nl2,&ip,kp,&q1); //Locate a pivot element (phase one).


if(ip == 0) {
//Maximum of auxiliary objective
function is
*icase=-1;
//unbounded, so no feasible solution
exists.
return;
}
e1: simp3(a,m+1,n,ip,kp);
//Exchange a left- and a right-hand variable (phase one), then update lists.
if(iposv[ip] >= n+m1+m2+1) { //Exchanged out an artificial variable for an
//equality constraint. Make sure it stays
//out by removing it from the l1 list.
for (k=1; k<=nl1; k++)
if(l1[k] == kp) goto e2;
e2: nl1=nl1-1;
for (is=k; is<=nl1; is++) l1[is]=l1[is+1];
}
else {
if(iposv[ip] < n+m1+1) goto e20;
kh=iposv[ip]-m1-n;
if(l3[kh] == 0) goto e20; //Exchanged out an m2 type constraint.
l3[kh]=0;
//If its the first time, correct the pivot
column
//or the minus sign and the implicit
//artificial variable.
}
a[m+2][kp+1] += 1.0;
for (i=1; i<=m+2; i++) a[i][kp+1] *= -1.0;
e20: is=izrov[kp];
//Update lists of left- and right-hand
variables.
izrov[kp]=iposv[ip];
iposv[ip]=is;
if (ir != 0) goto e10;
//if still in phase one, go back to 10.
//End of phase one code for finding an initial feasible solution. Now, in
phase two, optimize it.
e30: simp1(a,0,l1,nl1,0,&kp,&bmax); //Test the z-row for doneness.
if(bmax <= EPS) {
//Done. Solution found. Return with the good
news.
*icase=0;
return;
}
simp2(a,m,n,l2,nl2,&ip,kp,&q1);
//Locate a pivot element (phase two).
if(ip == 0) {
//Objective function is unbounded. Report and
return.
*icase=1;
return;
}

simp3(a,m,n,ip,kp);
(phase two),
goto e20;
and
}

//Exchange a left- and a right-hand variable


//update lists of left- and right-hand variables
//return for another iteration.

// The preceding routine makes use of the following utility subroutines:


void simp1(MAT a,int mm,int *ll,int nll,int iabf,int *kp,REAL *bmax) {
//Determines the maximum of those elements whose index is contained in the
supplied list
//ll, either with or without taking the absolute value, as flagged by iabf.
int k;
REAL test;
*kp=ll[1];
*bmax=a[mm+1][*kp+1];
if (nll < 2) return;
for (k=2; k<=nll; k++) {
if(iabf == 0)
test=a[mm+1][ll[k]+1]-(*bmax);
else
test=fabs(a[mm+1][ll[k]+1])-fabs(*bmax);
if(test > 0.0) {
*bmax=a[mm+1][ll[k]+1];
*kp=ll[k];
}
}
return;
}
void simp2(MAT a, int m,int n,int *l2,int nl2,int *ip,int kp,REAL *q1) {
REAL EPS=1e-6;
//Locate a pivot element, taking degeneracy into account.
int i,ii,k;
REAL q,q0,qp;
*ip=0;
if(nl2 < 1) return;
for (i=1; i<=nl2; i++)
if (a[i+1][kp+1] < -EPS) goto e2;
return; //No possible pivots. Return with message.
e2: *q1=-a[l2[i]+1][1]/a[l2[i]+1][kp+1];
*ip=l2[i];
if (i+1 > nl2) return;
for (i=i+1; i<=nl2; i++) {
ii=l2[i];
if(a[ii+1][kp+1] < -EPS) {
q=-a[ii+1][1]/a[ii+1][kp+1];
if (q < *q1) {
*ip=ii;
*q1=q;
}
else if (q == *q1) { //We have a degeneracy.
for (k=1; k<=n; k++) {
qp=-a[*ip+1][k+1]/a[*ip+1][kp+1];
q0=-a[ii+1][k+1]/a[ii+1][kp+1];
if (q0 != qp) goto e6;
}

e6:

if (q0 < qp) *ip=ii;


}

}
}
return;
}
void simp3(MAT a,int i1,int k1,int ip,int kp) {
//Matrix operations to exchange a left-hand and right-hand variable (see
text).
int ii,kk;
REAL piv;
piv=1.0/a[ip+1][kp+1];
if (i1 >= 0)
for (ii=1; ii<=i1+1; ii++)
if (ii-1 != ip) {
a[ii][kp+1] *= piv;
for (kk=1; kk<=k1+1; kk++)
if (kk-1 != kp)
a[ii][kk] -= a[ip+1][kk]*a[ii][kp+1];
}
for (kk=1; kk<=k1+1; kk++)
if(kk-1 != kp) a[ip+1][kk] =-a[ip+1][kk]*piv;
a[ip+1][kp+1]=piv;
return;
}
void main() {
printf("\n");
printf(" Number
printf(" Number
printf(" Number
printf(" Number
M=M1+M2+M3;

of
of
of
of

variables in E.F.:
<= inequalities..:
>= inequalities..:
= equalities.....:

");
");
");
");

scanf("%d",
scanf("%d",
scanf("%d",
scanf("%d",

// Total number of constraints

for (i=1; i<=M+2; i++)


for (j=1; j<=N+1; j++)
A[i][j]=0.0;
printf(" Input Economic Function:\n");
for (i=2; i<=N+1; i++) {
printf(" Coefficient #%d: ", i-1);
scanf("%lf", &A[1][i]);
}
printf(" Constant term : ");
scanf("%lf", &A[1][1]);
// input constraints
for (i=1; i<=M; i++) {
printf(" Input constraint #%d: \n", i);
for (j=2; j<=N+1; j++) {
printf(" Coefficient #%d: ", j-1);
scanf("%lf", &R);
A[i+1][j] = -R;
}
printf(" Constant term : ");

&N);
&M1);
&M2);
&M3);

scanf("%lf", &A[i+1][1]);
}
printf("\n Input Table:\n");
for (i=1; i<=M+1; i++) {
for (j=1; j<=N+1; j++)
printf("%8.2f", A[i][j]);
printf("\n");
}
simplx(A,M,N,M1,M2,M3,&ICASE,IZROV,IPOSV);
if (ICASE==0) { //result ok.
printf("\n Maximum of E.F. = %f\n", A[1][1]);
for (i=1; i<=N; i++) {
for (j=1; j<=M; j++)
if (IPOSV[j] == i) {
printf(" X%d = %f\n", i, A[j+1][1]);
goto e3;
}
printf(" X%d = %f\n", i, 0.0);
e3:;}
}
else
printf(" No solution (error code = %d).\n", ICASE);
printf("\n");
}
// end of file tsimplex.cpp

# include <iostream.h>
void main () {
long X1 , X2,, f;
long X1result,X2result,,MaxF;
MaxF= initialize with the minimum possible value of f;
for (X1= minimum value of X1; X1< =maximum value of X1; X1 ++)
for (X2= minimum value of X2; X2< =maximum value of X2; X2 ++)

if (Constraint1 && Constraint2 && )


{
f=Objective function with substituting current values of X1 ,X2, ;
If (f> MaxF)
{
X1result = X1;
X2result = X2;

MaxF= f;
}
}
cout <<endl <<Maximum f =<<MaxF;
cout <<endl <<X1 solution =<<X1s;
cout <<endl <<X2 solution =<<X2s;

#include <iostream>
#include <lemon/lp.h>
using namespace lemon;
int main()
{
// Create an instance of the default LP solver class
// (it will represent an "empty" problem at first)
Lp lp;
// Add two columns (variables) to the problem
Lp::Col x1 = lp.addCol();
Lp::Col x2 = lp.addCol();
// Add rows (constraints) to the problem
lp.addRow(x1 - 5 <= x2);
lp.addRow(0 <= 2 * x1 + x2 <= 25);
// Set lower and upper bounds for the columns (variables)
lp.colLowerBound(x1, 0);
lp.colUpperBound(x2, 10);
// Specify the objective function
lp.max();
lp.obj(5 * x1 + 3 * x2);
// Solve the problem using the underlying LP solver
lp.solve();
// Print the results
if (lp.primalType() == Lp::OPTIMAL) {
std::cout << "Objective function value: " << lp.primal() << std::endl;
std::cout << "x1 = " << lp.primal(x1) << std::endl;
std::cout << "x2 = " << lp.primal(x2) << std::endl;
} else {
std::cout << "Optimal solution not found." << std::endl;
}
return 0;
}

#include <stdio.h>
#include <conio.h>
#define INFINITY 999
#define N 3
#define M 6
/************************************************************/
/***** Solves the LPP by "SIMPLEX" method i.e. by table *****/
/************************************************************/
void minimum(float *arr,int *arrminpos,int n);
/* Calculates the minimum valued position among the array arr having n
elements. */
void display (float c[],float b[],float a[][M],int basic[]);
/* Display the table */
void displayframe(float c[M]);
/* Displays the frame of the table */
void calctemp(float *,float [][M],float [],int []);
/* Calculates Zj-Cj */
/*-------------------------------------------------------------------------*\
Cj
5
4
3
0
0
0
miniRatio
cB
xB
b
a1
a2
a3
a4
a5
a6
bi/aij
0
x4
5
2
3
1
1
0
0
2.5
0
x5
11
4
1
2
0
1
0
2.75
0
x6
8
3
4
2
0
0
1
2.66
---------------------------------------------------------------------------Zj-Cj
-5
-4
-3
0
0
0
---------------------------------------------------------------------------5
x1
2.5
1
1.5
0.5
0.5
0
0
5
0
x5
1
0
-5
0
-2
1
0
infinity
0
x6
105
0
-0.5
0.5
-1.5
0
1
1
---------------------------------------------------------------------------Zj-Cj
0
3.5
-0.5
2.5
0
0
---------------------------------------------------------------------------5
x1
2
1
2
0
2
0
-1
0
x5
1
0
-5
0
-2
1
0
3
x3
1
0
-1
1
-3
0
2
---------------------------------------------------------------------------Zj-Cj
0
3
0
1
0
1
---------------------------------------------------------------------------So the solution is :x1=2
x2=0
x3=1
x4=0
x5=1
x6=0
max(z) = 5*2 + 4*0 + 3*1 = 13.
\*-------------------------------------------------------------------------*/
void main()
{
float c[M]={{5},{4},{3},{0},{0},{0}};
/* Stores co-efficient of the objective function Max(z) */

float a[N][M]={
{2,3,1,1,0,0},
{4,1,2,0,1,0},
{3,4,2,0,0,1}
};
/* Stores the co-efficent of the constraints */
float b[N]={{5},{11},{8}};
/* Stores the values on RHS of constraints */
float temp[M]={{0},{0},{0},{0},{0},{0}};
/* Stores the values of Zj-Cj*/
int tempminpos;
/* Stores the minimum valued position
of {Zj-Cj} i.e. coming in variable */
float miniratio[N];
/* Stores the value of the ratio b[i]/a[i][j] */
int miniratiominpos; /* Stores the minimum valued position of
b[i]/a[i][j] i.e. going out variable */
float key;
/* Stores the key element */
int gooutcol;
/* Stores the column number which goes out */
float z;
/* Stores the value of the objective function */
float x[M];
/* Stores the value of the variables */
int i,j;
/* Loop variables */
int basic[N];
/* Stores the basic variable */
int nonbasic[N];
/* Stores the non-basic variable */
int flag=0;
/* Terminating variable */
//clrscr();
/*** Initializing basic variables to 3,4,5 i.e. x4,x5,x6 ***/
for(i=0;i<N;i++)
{
basic[i]=(i+N);
nonbasic[i]=i;
}
printf("\nMax z = c1x1 + c2x2 + c3x3\n");
printf("\na11x1 + a12x2 + a13x3 <= b1\n");
printf("\na21x1 + a22x2 + a23x3 <= b2\n");
printf("\na31x1 + a31x2 + a32x3 <= b3\n");
printf("\nEnter values of ci's\n");
/*** Inputing requisite amount of data ***/
for(i=0;i<N;i++)
{
printf("\nEnter c[%d]\t",i+1);
scanf("%f",&c[i]);
}
printf("\nEnter values of ai's\n");
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
printf("\nEnter a[%d][%d]\t",i+1,j+1);
scanf("%f",&a[i][j]);
}
}
printf("\nEnter values of bi's\n");
for(i=0;i<N;i++)
{
printf("\nEnter b[%d]\t",i+1);
scanf("%f",&b[i]);
}

/*** Calculation for actual table ***/


while(flag==0)
{
z=0;
calctemp(temp,a,c,basic);
printf("\n");
/*** Determining the incoming column ***/
minimum(temp,&tempminpos,M);
display(c,b,a,basic);
printf("\nZj-Cj\t\t\t");
for(i=0;i<M;i++)
printf("%.4g\t",temp[i]);
printf("\n\n");
for(i=0;i<N;i++)
{
x[basic[i]]=b[i];
x[nonbasic[i]]=0;
printf("x[%d]=%g\n",basic[i]+1,b[i]);
}
for(i=0;i<N;i++)
z=z+c[i]*x[i];
printf("Max(z) = %g",z);
/*** Determining the outgoing column ***/
for(i=0;i<N;i++)
{
if(a[i][tempminpos]==0)
{
miniratio[i]=INFINITY;
continue;
}
if(a[i][tempminpos]<0)
{
miniratio[i]=INFINITY;
continue;
}
miniratio[i]=b[i]/a[i][tempminpos];
}
minimum(miniratio,&miniratiominpos,N);
for(i=0;i<N;i++)
if(miniratiominpos==i)
gooutcol=basic[i];
printf("\nComing in variable = X%d\t",tempminpos+1);
printf("Going out variable = X%d\n",gooutcol+1);
/*** Changing the basic and non-basic variable ***/
basic[miniratiominpos]=tempminpos;
nonbasic[tempminpos]=gooutcol;
/*** Performing the operations to bring similar expressions in
in-coming variable as out-going variable by row operations ***/

key=a[miniratiominpos][tempminpos];
b[miniratiominpos]=b[miniratiominpos]/key;
for(i=0;i<M;i++)
a[miniratiominpos][i]=a[miniratiominpos][i]/key;
for(i=0;i<N;i++)
{
if(miniratiominpos==i)
continue;
key=a[i][tempminpos];
for(j=0;j<M;j++)
{
a[i][j]=a[i][j]-a[miniratiominpos][j]*key;
}
b[i]=b[i]-b[miniratiominpos]*key;
}
getch();
/*** Terminating condition ***/
for(i=0;i<M;i++)
{
flag=1;
if(temp[i]<0)
{
flag=0;
break;
}
}
}
printf("\nPress any key to exit...\n");
getch();
}
void calctemp(float *temp,float a[N][M],float c[M],int basic[N])
{
int i,j;
for(i=0;i<M;i++)
{
temp[i]=0;
for(j=0;j<N;j++)
temp[i]=temp[i]+c[basic[j]]*a[j][i];
temp[i]=temp[i]-c[i];
}
}
void minimum(float *arr,int *arrminpos, int n)
{
int i;
float arrmin;
arrmin=arr[0];
*arrminpos=0;
for(i=0;i<n;i++)
if(arr[i]<arrmin)
{
arrmin=arr[i];
*arrminpos=i;
}
printf("\n%d\n",*arrminpos);

}
void display (float c[N],float b[N],float a[N][M],int basic[N])
{
int i,j;
displayframe(c);
for(i=0;i<N;i++)
{
printf("\n%.4g\tX%d\t%.4g\t",c[basic[i]],basic[i]+1,b[i]);
for(j=0;j<M;j++)
printf("%.4g\t",a[i][j]);
printf("\n");
}
}
void displayframe(float c[M])
{
printf("\t\tc[j]\t");
printf("%g\t%g\t%g\t%g\t%g\t%g\n",c[0],c[1],c[2],c[3],c[4],c[5]);
printf("\nc[b]\tB\tb\ta1\ta2\ta3\ta4\ta5\ta6\n");
}

#include <stdio.h>
#include <conio.h>
#define INFINITY -999
#define N 10
#define M 20
/***************************************************/
/***** Solves the LPP by "DUAL SIMPLEX" method *****/
/***************************************************/
void minimum(float *arr,int *arrminpos,int n);
/* Calculates the minimum valued position among the array arr having n
elements. */
void maximum(float *arr,int *arrminpos,int n);
/* Calculates the minimum valued position among the array arr having n
elements. */
void display (float c[],float b[],float a[][M],int basic[]);
/* Display the table */
void displayframe(float c[M]);
/* Displays the frame of the table */
void calctemp(float *,float [][M],float [],int []);
/* Calculates Zj-Cj */
int countmaxzterms;
int constraint;
void main()
{
float c[M];
/* Stores co-efficient of the objective function Max(z) */
float a[N][M];
/* Stores the co-efficent of the constraints */
float b[N];
/* Stores the values on RHS of constraints */
float temp[M];
/* Stores the values of Zj-Cj*/
int bminpos;
/* Stores the minimum valued position
of {Zj-Cj} i.e. coming in variable */
float maxratio[M];
/* Stores the value of the ratio Zj-Cj/a[i][j] */
int maxratiomaxpos; /* Stores the minimum valued position of
b[i]/a[i][j] i.e. going out variable */
float key;
/* Stores the key element */
int gooutcol;
/* Stores the column number which goes out */
int incomingcol;
float z;
/* Stores the value of the objective function */
float x[M];
/* Stores the value of the variables */
int i,j;
/* Loop variables */
int basic[N];
/* Stores the basic variable */
int flag=0;
/* Terminating variable */
/*** Initializing basic variables ***/
for(i=0;i<M;i++)
c[i]=x[i]=temp[i]=0;
for(i=0;i<N;i++)
for(j=0;j<M;j++)
a[i][j]=0;
/*** Inputing requisite amount of data ***/
printf("\nEnter number of terms in objective function\n");

scanf("%d",&countmaxzterms);
printf("\nEnter the co-efficient\n");
for(i=0;i<countmaxzterms;i++)
scanf("%f",&c[i]);
printf("\nYou have entered the function as follows:-\n");
printf("\nMax z = ");
for(i=0;i<countmaxzterms;i++)
{
if(i==0)
printf("%g*x%d",c[i],i+1);
else
printf(" + %g*x%d",c[i],i+1);
}
printf("\nEnter number of constraint\n");
scanf("%d",&constraint);
printf("\nEnter the co-efficient of constraints\n");
for(i=0;i<constraint;i++)
for(j=0;j<countmaxzterms;j++)
scanf("%f",&a[i][j]);
for(i=0;i<constraint;i++)
a[i][j++]=1;
printf("\nEnter values of bi's\n");
for(i=0;i<constraint;i++)
scanf("%f",&b[i]);
for(i=0;i<countmaxzterms+constraint;i++)
basic[i]=(i+countmaxzterms);
printf("\nYou have entered the function as follows:-\n");
for(i=0;i<constraint;i++)
{
for(j=0;j<countmaxzterms;j++)
{
if(j==0)
printf(" %g*x%d ",a[i][j],j+1);
else
printf(" + %g*x%d ",a[i][j],j+1);
}
printf(" <= %g\n",b[i]);
}
getch();
/*** Calculation for actual table ***/
do
{
/*** Terminating condition ***/
for(i=0;i<constraint;i++)
{
flag=1;
if(b[i]<=0)
{
flag=0;
break;
}
}

z=0;
calctemp(temp,a,c,basic);
printf("\n");
display(c,b,a,basic);
printf("\n\tZj-Cj\t\t");
for(i=0;i<constraint+countmaxzterms;i++)
printf("%0.3g\t",temp[i]);
printf("\n\n");
/*** Determining the outgoing column ***/
minimum(b,&bminpos,constraint);
gooutcol=basic[bminpos];
/*** Determining the incoming column ***/
for(i=0;i<M;i++)
maxratio[i]=INFINITY;
for(i=0;i<constraint+countmaxzterms;i++)
{
if(a[bminpos][i]==0)
{
maxratio[i]=INFINITY;
continue;
}
if(a[bminpos][i]>0)
{
maxratio[i]=INFINITY;
continue;
}
maxratio[i]=temp[i]/a[bminpos][i];
}
maximum(maxratio,&maxratiomaxpos,2*constraint);
incomingcol=maxratiomaxpos;
for(i=0;i<constraint+countmaxzterms;i++)
x[i]=0;
for(i=0;i<constraint;i++)
{
x[basic[i]]=b[i];
printf("x[%d]=%0.3g\n",basic[i]+1,b[i]);
}
for(i=0;i<constraint;i++)
z=z+c[i]*x[i];
printf("Max(z) = %g",z);
printf("\nComing in variable = X%d\t",incomingcol+1);
printf("Going out variable = X%d\n",gooutcol+1);
/*** Changing the basic and non-basic variable ***/
basic[bminpos]=incomingcol;
/*** Performing the operations to bring similar expressions in
in-coming variable as out-going variable by row operations ***/

key=a[bminpos][incomingcol];
b[bminpos]=b[bminpos]/key;
for(i=0;i<constraint+countmaxzterms;i++)
a[bminpos][i]=a[bminpos][i]/key;
for(i=0;i<constraint;i++)
{
if(bminpos==i)
continue;
key=a[i][incomingcol];
for(j=0;j<(constraint+countmaxzterms);j++)
a[i][j]=a[i][j]-a[bminpos][j]*key;
b[i]=b[i]-b[bminpos]*key;
}
getch();
}while(flag==0);
printf("\nPress any key to exit...\n");
getch();
}
void calctemp(float *temp,float a[N][M],float c[M],int basic[N])
{
int i,j;
for(i=0;i<constraint+countmaxzterms;i++)
{
temp[i]=0;
for(j=0;j<constraint;j++)
temp[i]=temp[i]+c[basic[j]]*a[j][i];
temp[i]=temp[i]-c[i];
}
}
void maximum(float *arr,int *arrmaxpos, int n)
{
int i;
int arrmax;
arrmax=arr[0];
*arrmaxpos=0;
for(i=0;i<n;i++)
if(arr[i]>arrmax)
{
arrmax=arr[i];
*arrmaxpos=i;
}
}
void minimum(float *arr,int *arrminpos, int n)
{
int i;
int arrmin;
arrmin=arr[0];
*arrminpos=0;
for(i=0;i<n;i++)
if(arr[i]<arrmin)
{
arrmin=arr[i];
*arrminpos=i;

}
}
void display (float c[N],float b[N],float a[N][M],int basic[N])
{
int i,j;
displayframe(c);
for(i=0;i<constraint;i++)
{
printf("\n%0.3g\tX%d\t%0.3g\t",c[basic[i]],basic[i]+1,b[i]);
for(j=0;j<constraint+countmaxzterms;j++)
printf("%0.3g\t",a[i][j]);
printf("\n");
}
}
void displayframe(float c[M])
{
int i;
printf("\t\tc[j]\t");
for(i=0;i<constraint+countmaxzterms;i++)
printf("%0.2g\t",c[i]);
printf("\n");
printf("\nc[b]\tB\tb\t");
for(i=0;i<constraint+countmaxzterms;i++)
printf("a[%d]\t",i+1);
printf("\n");
}

Вам также может понравиться