In this article I show the resolution of simplex by matrix computation based on the revised simplex.
I start to explain some logic implementation of the simplex algorithm.
The idea is by example to :
Minimize x+3y
with the constraint
x+2y <= 10
2x+y <= 50
b the constraint value of the inequation (just the formula x+2y <= 10 -> 10)
cn the minimization parameters (Minimize x+3y -> 1,3)
Bindice basis index (Minimize x+3y -> 0,1)
Nindice non basis index (Minimize x+3y + 3 constraint -> 0,1, + 2,3,4)
The class
package com.mycompany.simplexe;
import Jama.Matrix;
public class simplexe {
boolean phase1(double[][] zn) {
for (double []val:zn)
if (val[0]<0) return false;
return true;
}
int phase2(double[][] zn) {
double min=Double.MAX_VALUE;
int minInt=0;
for (int i=0;i<zn.length;i++)
if (zn[i][0]<min) {
min=zn[i][0];
minInt=i;
}
return minInt;
}
double[][] phase3(double[][] B, double[][] N, int i) {
double ej[][]=new double[N[0].length][1];
ej[i][0]=1;
Matrix mat=new Matrix(B);
Matrix matInv=mat.inverse();
Matrix matInter=matInv.times(new Matrix (N));
return matInter.times(new Matrix(ej)).getArray() ;
}
int phase4(double[][] deltaXb, double[][] b) {
double maximum=Double.NEGATIVE_INFINITY;
int indice=-1;
for (int i=0;i<deltaXb.length;i++)
if (deltaXb[i][0]/b[i][0]>maximum)
{
maximum=deltaXb[i][0]/b[i][0];
indice=i;
}
return indice;
}
int phase5(int[] Bindice, int index) {
return Bindice[index];
}
double[][] phase6(double[][] B, double[][] N, int index) {
double ei[][]=new double[N.length][1];
ei[index][0]=1;
Matrix mat=new Matrix(B);
Matrix matInv=mat.inverse();
Matrix matInter=matInv.times(new Matrix (N));
matInv=matInter.transpose();
matInter=matInv.times(-1.0);
return matInter.times(new Matrix(ei)).getArray();
}
double phase7(double[][] deltaZn, double[][] zn, int index) {
return zn[index][0]/deltaZn[index][0];
}
double[][] phase8(double[][] xb,double t,double[][]deltaxb) {
Matrix matxb=new Matrix(xb);
Matrix mat=new Matrix(deltaxb);
mat=mat.times(-1.0*t);
return matxb.plus(mat).getArray();
}
int[] phase9a(int[] indiceList, int numberToreplace,int number) {
for(int i=0;i<indiceList.length;i++)
if (indiceList[i]==numberToreplace) {indiceList[i]=number; return indiceList;}
return indiceList;
}
double[][] phase9b(double[][] zn, int[] Bindice, int indexConv, double value) {
for(int i=0;i<Bindice.length;i++)
if (Bindice[i]==indexConv) {zn[i][0]=value; return zn;}
return zn;
}
double[][] generateFromIndice(double[][] A, int[] Bindice) {
double [][]result= new double[A.length][Bindice.length];
for(int i=0;i<Bindice.length;i++)
for(int j=0;j<A.length;j++)
result[j][i]=A[j][Bindice[i]];
return result;
}
simplexe(double[][] pA, double[][] pb, double[][] pcn,int[] pBindice,int[] pNindice) {
A=pA;
b=pb;
cn=pcn;
Bindice=new int[pBindice.length];
for (int i=0;i<pBindice.length;i++) Bindice[i]=pBindice[i];
Nindice=new int[pNindice.length];
for (int i=0;i<pNindice.length;i++) Nindice[i]=pNindice[i];
}
simplexe()
{}
private double[][] A;
private double[][] b;
private double[][] cn;
private double zn[][];
private double xb[][];
private int[] Bindice;
private int[] Nindice;
private double s;
private double t;
private double deltaXb[][];
private double deltaZn[][];
private int indexForT;
private int i;
private int j;
void init() {
Matrix znMat= new Matrix(cn);
znMat=znMat.times(-1.0);
zn=znMat.getArray();
xb= new Matrix(b).getArray();
}
boolean iterate() {
if (phase1(zn)) return false;
double B[][]=generateFromIndice(A,Bindice);
double N[][]=generateFromIndice(A,Nindice);
j=phase2(zn);
deltaXb=phase3(B,N,j);
indexForT=phase4(deltaXb,xb);
t=xb[indexForT][0]/deltaXb[indexForT][0];
i=phase5(Bindice,indexForT);
deltaZn=phase6(B,N,indexForT);
s=phase7(deltaZn,zn,j);
xb=phase8(xb,t,deltaXb);
zn=phase8(zn,s,deltaZn);
Bindice=phase9a(Bindice,i,indexForT);
Nindice=phase9a(Nindice,indexForT,i);
xb=phase9b(xb,Bindice,indexForT,t);
zn=phase9b(zn,Nindice,i,s);
return true;
}
int[] getIndexedB() {
return Bindice;
}
int[] getIndexedN() {
return Nindice;
}
double[][] getZn() {
return zn;
}
double[][] getXb() {
return xb;
}
double getS() {
return s;
}
double getT() {
return t;
}
double[][] getDeltaXb() {
return deltaXb;
}
double[][] getDeltaZn() {
return deltaZn;
}
int getI() {
return i;
}
int getJ() {
return j;
}
}
package com.mycompany.simplexe;
import junit.framework.Assert;
import junit.framework.TestCase;
public class simplexeTest extends TestCase {
public simplexeTest(String testName) {
super(testName);
}
@Override
protected void setUp() throws Exception {
super.setUp();
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
}
double [][]A= new double[3][5];
int Bindice[]= new int[3];
int Nindice[]= new int[2];
double [][]B= new double[3][3];
double [][]N= new double[3][2];
double b[][]=new double [3][1];
double xb[][]=null;
double cn[][]= new double[2][1];
double zn[][]= new double[2][1];
public void init(){
A[0][0]=1;
A[0][1]=-1;
A[0][2]=1;
A[0][3]=0;
A[0][4]=0;
A[1][0]=2;
A[1][1]=-1;
A[1][2]=0;
A[1][3]=1;
A[1][4]=0;
A[2][0]=0;
A[2][1]=1;
A[2][2]=0;
A[2][3]=0;
A[2][4]=1;
Bindice[0]=2;
Bindice[1]=3;
Bindice[2]=4;
Nindice[0]=0;
Nindice[1]=1;
b[0][0]=1;
b[1][0]=3;
b[2][0]=5;
cn[0][0]=4;
cn[1][0]=3;
zn[0][0]=-4;
zn[1][0]=-3;
xb=b;
B=new simplexe().generateFromIndice(A,Bindice);
N=new simplexe().generateFromIndice(A,Nindice);
}
public void testPhase1() {
init();
boolean isFinished=new simplexe().phase1(zn);
Assert.assertEquals(false, isFinished);
}
public void testPhase2() {
init();
int number=new simplexe().phase2(zn);
Assert.assertEquals(0, number);
}
public void testPhase3() {
init();
double resultat[][]=new simplexe().phase3(B,N,0);
Assert.assertEquals(3, resultat.length);
Assert.assertEquals(1, resultat[0].length);
Assert.assertEquals(1.0, resultat[0][0]);
Assert.assertEquals(2.0, resultat[1][0]);
Assert.assertEquals(0.0, resultat[2][0]);
}
public void testPhase4() {
init();
double deltaXb[][]=new simplexe().phase3(B,N,0);
int index=new simplexe().phase4(deltaXb,b);
Assert.assertEquals(0, index);
deltaXb=new double[3][1];
deltaXb[0][0]=-1.0;
deltaXb[1][0]=1.0;
deltaXb[2][0]=1.0;
double xb[][]=new double[3][1];
xb[0][0]=1.0;
xb[1][0]=1.0;
xb[2][0]=5.0;
int index2=new simplexe().phase4(deltaXb,xb);
Assert.assertEquals("index ",1, index2);
}
public void testPhase5() {
init();
double deltaXb[][]=new simplexe().phase3(B,N,0);
int index=new simplexe().phase4(deltaXb,b);
int indexConv=new simplexe().phase5(Bindice,index);
Assert.assertEquals(2, indexConv);
}
public void testPhase6() {
init();
double deltaXb[][]=new simplexe().phase3(B,N,0);
int index=new simplexe().phase4(deltaXb,b);
double deltaZn[][]=new simplexe().phase6(B,N,index);
Assert.assertEquals(2, deltaZn.length);
Assert.assertEquals(1, deltaZn[0].length);
Assert.assertEquals(-1.0, deltaZn[0][0]);
Assert.assertEquals(1.0, deltaZn[1][0]);
}
public void testPhase7() {
init();
double deltaXb[][]=new simplexe().phase3(B,N,0);
int index=new simplexe().phase4(deltaXb,b);
double deltaZn[][]=new simplexe().phase6(B,N,index);
double s=new simplexe().phase7(deltaZn,zn,index);
Assert.assertEquals(4.0, s);
}
public void testPhase8() {
init();
int number=new simplexe().phase2(zn);
double deltaXb[][]=new simplexe().phase3(B,N,0);
int index=new simplexe().phase4(deltaXb,b);
double deltaZn[][]=new simplexe().phase6(B,N,index);
double s=new simplexe().phase7(deltaZn,zn,index);
xb=new simplexe().phase8(xb,b[index][0]/deltaXb[index][0],deltaXb);
zn=new simplexe().phase8(zn,s,deltaZn);
Assert.assertEquals(3, xb.length);
Assert.assertEquals(1, xb[0].length);
Assert.assertEquals(0.0, xb[0][0]);
Assert.assertEquals(1.0, xb[1][0]);
Assert.assertEquals(5.0, xb[2][0]);
Assert.assertEquals(2, zn.length);
Assert.assertEquals(1, zn[0].length);
Assert.assertEquals(0.0, zn[0][0]);
Assert.assertEquals(-7.0, zn[1][0]);
}
public void testPhase9() {
init();
int number=new simplexe().phase2(zn);
double deltaXb[][]=new simplexe().phase3(B,N,0);
int index=new simplexe().phase4(deltaXb,b);
int indexConv=new simplexe().phase5(Bindice,index);
Assert.assertEquals(2, indexConv);
Assert.assertEquals(0, number);
Bindice=new simplexe().phase9a(Bindice,indexConv,number);
Nindice=new simplexe().phase9a(Nindice,number,indexConv);
zn=new simplexe().phase9b(zn,Bindice,number,4);
xb=new simplexe().phase9b(xb,Nindice,indexConv,1);
Assert.assertEquals(0, Bindice[0]);
Assert.assertEquals(3, Bindice[1]);
Assert.assertEquals(4, Bindice[2]);
Assert.assertEquals(2, Nindice[0]);
Assert.assertEquals(1, Nindice[1]);
Assert.assertEquals(4.0, zn[0][0]);
Assert.assertEquals(-3.0, zn[1][0]);
Assert.assertEquals(1.0, xb[0][0]);
Assert.assertEquals(3.0, xb[1][0]);
Assert.assertEquals(5.0, xb[2][0]);
}
public void testGenerateBN() {
init();
double B[][]=new simplexe().generateFromIndice(A,Bindice);
double N[][]=new simplexe().generateFromIndice(A,Nindice);
Assert.assertEquals(1.0, B[0][0]);
Assert.assertEquals(0.0, B[0][1]);
Assert.assertEquals(0.0, B[0][2]);
Assert.assertEquals(0.0, B[1][0]);
Assert.assertEquals(1.0, B[1][1]);
Assert.assertEquals(0.0, B[1][2]);
Assert.assertEquals(0.0, B[2][0]);
Assert.assertEquals(0.0, B[2][1]);
Assert.assertEquals(1.0, B[2][2]);
Assert.assertEquals(1.0, N[0][0]);
Assert.assertEquals(-1.0, N[0][1]);
Assert.assertEquals(2.0, N[1][0]);
Assert.assertEquals(-1.0, N[1][1]);
Assert.assertEquals(0.0, N[2][0]);
Assert.assertEquals(1.0, N[2][1]);
}
public void testOneIterationBN() {
init();
simplexe simplex= new simplexe(A,b,cn,Bindice,Nindice);
simplex.init();
double zn1[][]=simplex.getZn();
Assert.assertEquals(-4.0, zn[0][0]);
Assert.assertEquals(-3.0, zn[1][0]);
simplex.iterate();
double deltaxb[][]=simplex.getDeltaXb();
Assert.assertEquals("d0",1.0, deltaxb[0][0]);
Assert.assertEquals("d1",2.0, deltaxb[1][0]);
Assert.assertEquals("d2",0.0, deltaxb[2][0]);
double deltazn[][]=simplex.getDeltaZn();
Assert.assertEquals("dz0",-1.0, deltazn[0][0]);
Assert.assertEquals("dz1",1.0, deltazn[1][0]);
double s1=simplex.getS();
double newzn[][]=simplex.phase8(zn1,s1,deltazn);
Assert.assertEquals("newzn",0.0, newzn[0][0]);
Assert.assertEquals("newzn2",-7.0, newzn[1][0]);
int Bindice[]=simplex.getIndexedB();
int Nindice[]=simplex.getIndexedN();
Assert.assertEquals(0, Bindice[0]);
Assert.assertEquals(3, Bindice[1]);
Assert.assertEquals(4, Bindice[2]);
Assert.assertEquals(2, Nindice[0]);
Assert.assertEquals(1, Nindice[1]);
double zn[][]=simplex.getZn();
double xb[][]=simplex.getXb();
Assert.assertEquals(1.0, xb[0][0]);
Assert.assertEquals(1.0, xb[1][0]);
Assert.assertEquals(5.0, xb[2][0]);
Assert.assertEquals(4.0, zn[0][0]);
Assert.assertEquals(-7.0, zn[1][0]);
simplex.iterate();
Bindice=simplex.getIndexedB();
Nindice=simplex.getIndexedN();
Assert.assertEquals(0, Bindice[0]);
Assert.assertEquals(1, Bindice[1]);
Assert.assertEquals(4, Bindice[2]);
Assert.assertEquals(2, Nindice[0]);
Assert.assertEquals(3, Nindice[1]);
deltaxb=simplex.getDeltaXb();
Assert.assertEquals(-1.0, deltaxb[0][0]);
Assert.assertEquals("1",1.0, deltaxb[1][0]);
Assert.assertEquals("2",1.0, deltaxb[2][0]);
double s=simplex.getS();
double t=simplex.getT();
int i=simplex.getI();
int j=simplex.getJ();
Assert.assertEquals("t",1.0, t);
Assert.assertEquals("s",7.0,s);
zn=simplex.getZn();
xb=simplex.getXb();
Assert.assertEquals("e",2.0, xb[0][0]);
Assert.assertEquals(1.0, xb[1][0]);
Assert.assertEquals(4.0, xb[2][0]);
Assert.assertEquals(-10.0, zn[0][0]);
Assert.assertEquals("last",7.0, zn[1][0]);
simplex.iterate();
zn=simplex.getZn();
xb=simplex.getXb();
Assert.assertEquals(4.0, xb[0][0]);
Assert.assertEquals(5.0, xb[1][0]);
Assert.assertEquals("de",2.0, xb[2][0]);
Assert.assertEquals(5.0, zn[0][0]);
Assert.assertEquals("last",2.0, zn[1][0]);
Assert.assertFalse(simplex.iterate());
}
public void testOtherform() {
double [][]Anext= new double[3][6];
Anext[0][0]=2;
Anext[0][1]=3;
Anext[0][2]=1;
Anext[0][3]=1;
Anext[0][4]=0;
Anext[0][5]=0;
Anext[1][0]=4;
Anext[1][1]=1;
Anext[1][2]=2;
Anext[1][3]=0;
Anext[1][4]=1;
Anext[1][5]=0;
Anext[2][0]=3;
Anext[2][1]=4;
Anext[2][2]=2;
Anext[2][3]=0;
Anext[2][4]=0;
Anext[2][5]=1;
double bnext[][]=new double [3][1];
bnext[0][0]=5;
bnext[1][0]=11;
bnext[2][0]=8;
double cnnext[][]= new double[3][1];
cnnext[0][0] =5;
cnnext[1][0] =4;
cnnext[2][0] =3;
int Bindicenext[]= new int[3];
Bindicenext[0]=3;
Bindicenext[1]=4;
Bindicenext[2]=5;
int Nindicenext[]= new int[3];
Nindicenext[0]=0;
Nindicenext[1]=1;
Nindicenext[2]=2;
simplexe simplex= new simplexe(Anext,bnext,cnnext,Bindicenext,Nindicenext);
simplex.init();
while(simplex.iterate())
{
}
simplex.getXb();
}
public void testOtherform2() {
double [][]Anext= new double[3][7];
Anext[0][0]=3;
Anext[0][1]=2;
Anext[0][2]=1;
Anext[0][3]=2;
Anext[0][4]=1;
Anext[0][5]=0;
Anext[0][6]=0;
Anext[1][0]=1;
Anext[1][1]=1;
Anext[1][2]=1;
Anext[1][3]=1;
Anext[1][4]=0;
Anext[1][5]=1;
Anext[1][6]=0;
Anext[2][0]=4;
Anext[2][1]=3;
Anext[2][2]=3;
Anext[2][3]=4;
Anext[2][4]=0;
Anext[2][5]=0;
Anext[2][6]=1;
double bnext[][]=new double [3][1];
bnext[0][0]=255;
bnext[1][0]=117;
bnext[2][0]=420;
double cnnext[][]= new double[4][1];
cnnext[0][0] =19;
cnnext[1][0] =13;
cnnext[2][0] =12;
cnnext[3][0] =17;
int Bindicenext[]= new int[3];
Bindicenext[0]=4;
Bindicenext[1]=5;
Bindicenext[2]=6;
int Nindicenext[]= new int[4];
Nindicenext[0]=0;
Nindicenext[1]=1;
Nindicenext[2]=2;
Nindicenext[3]=3;
simplexe simplex= new simplexe(Anext,bnext,cnnext,Bindicenext,Nindicenext);
simplex.init();
while(simplex.iterate())
{
}
simplex.getXb();
}
public void testOtherformminim() {
double [][]Anext= new double[3][5];
Anext[0][0]=-3;
Anext[0][1]=2;
Anext[0][2]=1;
Anext[0][3]=0;
Anext[0][4]=0;
Anext[1][0]=-1;
Anext[1][1]=2;
Anext[1][2]=0;
Anext[1][3]=1;
Anext[1][4]=0;
Anext[2][0]=1;
Anext[2][1]=1;
Anext[2][2]=0;
Anext[2][3]=0;
Anext[2][4]=1;
double bnext[][]=new double [3][1];
bnext[0][0]=2;
bnext[1][0]=4;
bnext[2][0]=5;
double cnnext[][]= new double[2][1];
cnnext[0][0] =1;
cnnext[1][0] =2;
int Bindicenext[]= new int[3];
Bindicenext[0]=2;
Bindicenext[1]=3;
Bindicenext[2]=4;
int Nindicenext[]= new int[2];
Nindicenext[0]=0;
Nindicenext[1]=1;
simplexe simplex= new simplexe(Anext,bnext,cnnext,Bindicenext,Nindicenext);
simplex.init();
int iter=0;
while(simplex.iterate())
{
iter++;
}
simplex.getXb();
}
public void testOtherformminim2() {
double [][]Anext= new double[2][5];
Anext[0][0]=1;
Anext[0][1]=3;
Anext[0][2]=0;
Anext[0][3]=-1;
Anext[0][4]=0;
Anext[1][0]=1;
Anext[1][1]=1;
Anext[1][2]=-1;
Anext[1][3]=0;
Anext[1][4]=1;
double bnext[][]=new double [2][1];
bnext[0][0]=4;
bnext[1][0]=10;
double cnnext[][]= new double[3][1];
cnnext[0][0] =-1;
cnnext[1][0] =1;
cnnext[2][0] =-1;
int Bindicenext[]= new int[2];
Bindicenext[0]=3;
Bindicenext[1]=4;
int Nindicenext[]= new int[3];
Nindicenext[0]=0;
Nindicenext[1]=1;
Nindicenext[2]=2;
simplexe simplex= new simplexe(Anext,bnext,cnnext,Bindicenext,Nindicenext);
simplex.init();
int iter=0;
while(simplex.iterate())
{
iter++;
}
simplex.getXb();
}
public void testOtherformminim3() {
double [][]Anext= new double[3][6];
Anext[0][0]=2;
Anext[0][1]=1;
Anext[0][2]=4;
Anext[0][3]=-1;
Anext[0][4]=0;
Anext[0][5]=0;
Anext[1][0]=3;
Anext[1][1]=2;
Anext[1][2]=3;
Anext[1][3]=0;
Anext[1][4]=-1;
Anext[1][5]=0;
Anext[2][0]=2;
Anext[2][1]=1;
Anext[2][2]=1;
Anext[2][3]=0;
Anext[2][4]=0;
Anext[2][5]=1;
double bnext[][]=new double [3][1];
bnext[0][0]=8;
bnext[1][0]=5;
bnext[2][0]=100;
double cnnext[][]= new double[3][1];
cnnext[0][0] =-90;
cnnext[1][0] =-81;
cnnext[2][0] =100;
int Bindicenext[]= new int[3];
Bindicenext[0]=3;
Bindicenext[1]=4;
Bindicenext[2]=5;
int Nindicenext[]= new int[3];
Nindicenext[0]=0;
Nindicenext[1]=1;
Nindicenext[2]=2;
simplexe simplex= new simplexe(Anext,bnext,cnnext,Bindicenext,Nindicenext);
simplex.init();
int iter=0;
while(simplex.iterate())
{
iter++;
}
simplex.getXb();
}
public void testOtherformmax4() {
double [][]Anext= new double[2][4];
Anext[0][0]=2;
Anext[0][1]=2;
Anext[0][2]=1;
Anext[0][3]=0;
Anext[1][0]=3;
Anext[1][1]=1;
Anext[1][2]=0;
Anext[1][3]=1;
double bnext[][]=new double [2][1];
bnext[0][0]=300;
bnext[1][0]=400;
double cnnext[][]= new double[2][1];
cnnext[0][0] =25;
cnnext[1][0] =15;
int Bindicenext[]= new int[2];
Bindicenext[0]=2;
Bindicenext[1]=3;
int Nindicenext[]= new int[2];
Nindicenext[0]=0;
Nindicenext[1]=1;
simplexe simplex= new simplexe(Anext,bnext,cnnext,Bindicenext,Nindicenext);
simplex.init();
int iter=0;
while(simplex.iterate())
{
iter++;
}
simplex.getXb();
}
}
I start to explain some logic implementation of the simplex algorithm.
The idea is by example to :
Minimize x+3y
with the constraint
x+2y <= 10
2x+y <= 50
The simplex is an approach to transform simple inéquation to equation by adding some phantom variable (not explained on the resolution)
By example
x <= 10 -> x + phantom =10 (it is valid in term of equation)
For each constraint we add a new phantom variable.
Minimizing is in the algorithm to search the minimum inverted variables on the minimize formula.
Example
Minimize x+3y -> searching on -x-3y the minimum(in this example -3)
Maximizing is without inverting the values.
Now the variable of the next algorithm:
A the area of resolution containing the left view ont the constraint (just the formula x+2y <= 10 -> 1,2)b the constraint value of the inequation (just the formula x+2y <= 10 -> 10)
cn the minimization parameters (Minimize x+3y -> 1,3)
Bindice basis index (Minimize x+3y -> 0,1)
Nindice non basis index (Minimize x+3y + 3 constraint -> 0,1, + 2,3,4)
9 phases
phase 1 check for optimality
phase 2 Select entering variable
phase 3 compute primal
phase 4 compute primal step length
phase 5 select leaving variables
phase 6 Compute dual step direction
phase 7 compute dual step length
phase 8 update current Primal and dual solution
phase 9 update basis with non basis index
The class
package com.mycompany.simplexe;
import Jama.Matrix;
public class simplexe {
boolean phase1(double[][] zn) {
for (double []val:zn)
if (val[0]<0) return false;
return true;
}
int phase2(double[][] zn) {
double min=Double.MAX_VALUE;
int minInt=0;
for (int i=0;i<zn.length;i++)
if (zn[i][0]<min) {
min=zn[i][0];
minInt=i;
}
return minInt;
}
double[][] phase3(double[][] B, double[][] N, int i) {
double ej[][]=new double[N[0].length][1];
ej[i][0]=1;
Matrix mat=new Matrix(B);
Matrix matInv=mat.inverse();
Matrix matInter=matInv.times(new Matrix (N));
return matInter.times(new Matrix(ej)).getArray() ;
}
int phase4(double[][] deltaXb, double[][] b) {
double maximum=Double.NEGATIVE_INFINITY;
int indice=-1;
for (int i=0;i<deltaXb.length;i++)
if (deltaXb[i][0]/b[i][0]>maximum)
{
maximum=deltaXb[i][0]/b[i][0];
indice=i;
}
return indice;
}
int phase5(int[] Bindice, int index) {
return Bindice[index];
}
double[][] phase6(double[][] B, double[][] N, int index) {
double ei[][]=new double[N.length][1];
ei[index][0]=1;
Matrix mat=new Matrix(B);
Matrix matInv=mat.inverse();
Matrix matInter=matInv.times(new Matrix (N));
matInv=matInter.transpose();
matInter=matInv.times(-1.0);
return matInter.times(new Matrix(ei)).getArray();
}
double phase7(double[][] deltaZn, double[][] zn, int index) {
return zn[index][0]/deltaZn[index][0];
}
double[][] phase8(double[][] xb,double t,double[][]deltaxb) {
Matrix matxb=new Matrix(xb);
Matrix mat=new Matrix(deltaxb);
mat=mat.times(-1.0*t);
return matxb.plus(mat).getArray();
}
int[] phase9a(int[] indiceList, int numberToreplace,int number) {
for(int i=0;i<indiceList.length;i++)
if (indiceList[i]==numberToreplace) {indiceList[i]=number; return indiceList;}
return indiceList;
}
double[][] phase9b(double[][] zn, int[] Bindice, int indexConv, double value) {
for(int i=0;i<Bindice.length;i++)
if (Bindice[i]==indexConv) {zn[i][0]=value; return zn;}
return zn;
}
double[][] generateFromIndice(double[][] A, int[] Bindice) {
double [][]result= new double[A.length][Bindice.length];
for(int i=0;i<Bindice.length;i++)
for(int j=0;j<A.length;j++)
result[j][i]=A[j][Bindice[i]];
return result;
}
simplexe(double[][] pA, double[][] pb, double[][] pcn,int[] pBindice,int[] pNindice) {
A=pA;
b=pb;
cn=pcn;
Bindice=new int[pBindice.length];
for (int i=0;i<pBindice.length;i++) Bindice[i]=pBindice[i];
Nindice=new int[pNindice.length];
for (int i=0;i<pNindice.length;i++) Nindice[i]=pNindice[i];
}
simplexe()
{}
private double[][] A;
private double[][] b;
private double[][] cn;
private double zn[][];
private double xb[][];
private int[] Bindice;
private int[] Nindice;
private double s;
private double t;
private double deltaXb[][];
private double deltaZn[][];
private int indexForT;
private int i;
private int j;
void init() {
Matrix znMat= new Matrix(cn);
znMat=znMat.times(-1.0);
zn=znMat.getArray();
xb= new Matrix(b).getArray();
}
boolean iterate() {
if (phase1(zn)) return false;
double B[][]=generateFromIndice(A,Bindice);
double N[][]=generateFromIndice(A,Nindice);
j=phase2(zn);
deltaXb=phase3(B,N,j);
indexForT=phase4(deltaXb,xb);
t=xb[indexForT][0]/deltaXb[indexForT][0];
i=phase5(Bindice,indexForT);
deltaZn=phase6(B,N,indexForT);
s=phase7(deltaZn,zn,j);
xb=phase8(xb,t,deltaXb);
zn=phase8(zn,s,deltaZn);
Bindice=phase9a(Bindice,i,indexForT);
Nindice=phase9a(Nindice,indexForT,i);
xb=phase9b(xb,Bindice,indexForT,t);
zn=phase9b(zn,Nindice,i,s);
return true;
}
int[] getIndexedB() {
return Bindice;
}
int[] getIndexedN() {
return Nindice;
}
double[][] getZn() {
return zn;
}
double[][] getXb() {
return xb;
}
double getS() {
return s;
}
double getT() {
return t;
}
double[][] getDeltaXb() {
return deltaXb;
}
double[][] getDeltaZn() {
return deltaZn;
}
int getI() {
return i;
}
int getJ() {
return j;
}
}
Adding the test class
package com.mycompany.simplexe;
import junit.framework.Assert;
import junit.framework.TestCase;
public class simplexeTest extends TestCase {
public simplexeTest(String testName) {
super(testName);
}
@Override
protected void setUp() throws Exception {
super.setUp();
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
}
double [][]A= new double[3][5];
int Bindice[]= new int[3];
int Nindice[]= new int[2];
double [][]B= new double[3][3];
double [][]N= new double[3][2];
double b[][]=new double [3][1];
double xb[][]=null;
double cn[][]= new double[2][1];
double zn[][]= new double[2][1];
public void init(){
A[0][0]=1;
A[0][1]=-1;
A[0][2]=1;
A[0][3]=0;
A[0][4]=0;
A[1][0]=2;
A[1][1]=-1;
A[1][2]=0;
A[1][3]=1;
A[1][4]=0;
A[2][0]=0;
A[2][1]=1;
A[2][2]=0;
A[2][3]=0;
A[2][4]=1;
Bindice[0]=2;
Bindice[1]=3;
Bindice[2]=4;
Nindice[0]=0;
Nindice[1]=1;
b[0][0]=1;
b[1][0]=3;
b[2][0]=5;
cn[0][0]=4;
cn[1][0]=3;
zn[0][0]=-4;
zn[1][0]=-3;
xb=b;
B=new simplexe().generateFromIndice(A,Bindice);
N=new simplexe().generateFromIndice(A,Nindice);
}
public void testPhase1() {
init();
boolean isFinished=new simplexe().phase1(zn);
Assert.assertEquals(false, isFinished);
}
public void testPhase2() {
init();
int number=new simplexe().phase2(zn);
Assert.assertEquals(0, number);
}
public void testPhase3() {
init();
double resultat[][]=new simplexe().phase3(B,N,0);
Assert.assertEquals(3, resultat.length);
Assert.assertEquals(1, resultat[0].length);
Assert.assertEquals(1.0, resultat[0][0]);
Assert.assertEquals(2.0, resultat[1][0]);
Assert.assertEquals(0.0, resultat[2][0]);
}
public void testPhase4() {
init();
double deltaXb[][]=new simplexe().phase3(B,N,0);
int index=new simplexe().phase4(deltaXb,b);
Assert.assertEquals(0, index);
deltaXb=new double[3][1];
deltaXb[0][0]=-1.0;
deltaXb[1][0]=1.0;
deltaXb[2][0]=1.0;
double xb[][]=new double[3][1];
xb[0][0]=1.0;
xb[1][0]=1.0;
xb[2][0]=5.0;
int index2=new simplexe().phase4(deltaXb,xb);
Assert.assertEquals("index ",1, index2);
}
public void testPhase5() {
init();
double deltaXb[][]=new simplexe().phase3(B,N,0);
int index=new simplexe().phase4(deltaXb,b);
int indexConv=new simplexe().phase5(Bindice,index);
Assert.assertEquals(2, indexConv);
}
public void testPhase6() {
init();
double deltaXb[][]=new simplexe().phase3(B,N,0);
int index=new simplexe().phase4(deltaXb,b);
double deltaZn[][]=new simplexe().phase6(B,N,index);
Assert.assertEquals(2, deltaZn.length);
Assert.assertEquals(1, deltaZn[0].length);
Assert.assertEquals(-1.0, deltaZn[0][0]);
Assert.assertEquals(1.0, deltaZn[1][0]);
}
public void testPhase7() {
init();
double deltaXb[][]=new simplexe().phase3(B,N,0);
int index=new simplexe().phase4(deltaXb,b);
double deltaZn[][]=new simplexe().phase6(B,N,index);
double s=new simplexe().phase7(deltaZn,zn,index);
Assert.assertEquals(4.0, s);
}
public void testPhase8() {
init();
int number=new simplexe().phase2(zn);
double deltaXb[][]=new simplexe().phase3(B,N,0);
int index=new simplexe().phase4(deltaXb,b);
double deltaZn[][]=new simplexe().phase6(B,N,index);
double s=new simplexe().phase7(deltaZn,zn,index);
xb=new simplexe().phase8(xb,b[index][0]/deltaXb[index][0],deltaXb);
zn=new simplexe().phase8(zn,s,deltaZn);
Assert.assertEquals(3, xb.length);
Assert.assertEquals(1, xb[0].length);
Assert.assertEquals(0.0, xb[0][0]);
Assert.assertEquals(1.0, xb[1][0]);
Assert.assertEquals(5.0, xb[2][0]);
Assert.assertEquals(2, zn.length);
Assert.assertEquals(1, zn[0].length);
Assert.assertEquals(0.0, zn[0][0]);
Assert.assertEquals(-7.0, zn[1][0]);
}
public void testPhase9() {
init();
int number=new simplexe().phase2(zn);
double deltaXb[][]=new simplexe().phase3(B,N,0);
int index=new simplexe().phase4(deltaXb,b);
int indexConv=new simplexe().phase5(Bindice,index);
Assert.assertEquals(2, indexConv);
Assert.assertEquals(0, number);
Bindice=new simplexe().phase9a(Bindice,indexConv,number);
Nindice=new simplexe().phase9a(Nindice,number,indexConv);
zn=new simplexe().phase9b(zn,Bindice,number,4);
xb=new simplexe().phase9b(xb,Nindice,indexConv,1);
Assert.assertEquals(0, Bindice[0]);
Assert.assertEquals(3, Bindice[1]);
Assert.assertEquals(4, Bindice[2]);
Assert.assertEquals(2, Nindice[0]);
Assert.assertEquals(1, Nindice[1]);
Assert.assertEquals(4.0, zn[0][0]);
Assert.assertEquals(-3.0, zn[1][0]);
Assert.assertEquals(1.0, xb[0][0]);
Assert.assertEquals(3.0, xb[1][0]);
Assert.assertEquals(5.0, xb[2][0]);
}
public void testGenerateBN() {
init();
double B[][]=new simplexe().generateFromIndice(A,Bindice);
double N[][]=new simplexe().generateFromIndice(A,Nindice);
Assert.assertEquals(1.0, B[0][0]);
Assert.assertEquals(0.0, B[0][1]);
Assert.assertEquals(0.0, B[0][2]);
Assert.assertEquals(0.0, B[1][0]);
Assert.assertEquals(1.0, B[1][1]);
Assert.assertEquals(0.0, B[1][2]);
Assert.assertEquals(0.0, B[2][0]);
Assert.assertEquals(0.0, B[2][1]);
Assert.assertEquals(1.0, B[2][2]);
Assert.assertEquals(1.0, N[0][0]);
Assert.assertEquals(-1.0, N[0][1]);
Assert.assertEquals(2.0, N[1][0]);
Assert.assertEquals(-1.0, N[1][1]);
Assert.assertEquals(0.0, N[2][0]);
Assert.assertEquals(1.0, N[2][1]);
}
public void testOneIterationBN() {
init();
simplexe simplex= new simplexe(A,b,cn,Bindice,Nindice);
simplex.init();
double zn1[][]=simplex.getZn();
Assert.assertEquals(-4.0, zn[0][0]);
Assert.assertEquals(-3.0, zn[1][0]);
simplex.iterate();
double deltaxb[][]=simplex.getDeltaXb();
Assert.assertEquals("d0",1.0, deltaxb[0][0]);
Assert.assertEquals("d1",2.0, deltaxb[1][0]);
Assert.assertEquals("d2",0.0, deltaxb[2][0]);
double deltazn[][]=simplex.getDeltaZn();
Assert.assertEquals("dz0",-1.0, deltazn[0][0]);
Assert.assertEquals("dz1",1.0, deltazn[1][0]);
double s1=simplex.getS();
double newzn[][]=simplex.phase8(zn1,s1,deltazn);
Assert.assertEquals("newzn",0.0, newzn[0][0]);
Assert.assertEquals("newzn2",-7.0, newzn[1][0]);
int Bindice[]=simplex.getIndexedB();
int Nindice[]=simplex.getIndexedN();
Assert.assertEquals(0, Bindice[0]);
Assert.assertEquals(3, Bindice[1]);
Assert.assertEquals(4, Bindice[2]);
Assert.assertEquals(2, Nindice[0]);
Assert.assertEquals(1, Nindice[1]);
double zn[][]=simplex.getZn();
double xb[][]=simplex.getXb();
Assert.assertEquals(1.0, xb[0][0]);
Assert.assertEquals(1.0, xb[1][0]);
Assert.assertEquals(5.0, xb[2][0]);
Assert.assertEquals(4.0, zn[0][0]);
Assert.assertEquals(-7.0, zn[1][0]);
simplex.iterate();
Bindice=simplex.getIndexedB();
Nindice=simplex.getIndexedN();
Assert.assertEquals(0, Bindice[0]);
Assert.assertEquals(1, Bindice[1]);
Assert.assertEquals(4, Bindice[2]);
Assert.assertEquals(2, Nindice[0]);
Assert.assertEquals(3, Nindice[1]);
deltaxb=simplex.getDeltaXb();
Assert.assertEquals(-1.0, deltaxb[0][0]);
Assert.assertEquals("1",1.0, deltaxb[1][0]);
Assert.assertEquals("2",1.0, deltaxb[2][0]);
double s=simplex.getS();
double t=simplex.getT();
int i=simplex.getI();
int j=simplex.getJ();
Assert.assertEquals("t",1.0, t);
Assert.assertEquals("s",7.0,s);
zn=simplex.getZn();
xb=simplex.getXb();
Assert.assertEquals("e",2.0, xb[0][0]);
Assert.assertEquals(1.0, xb[1][0]);
Assert.assertEquals(4.0, xb[2][0]);
Assert.assertEquals(-10.0, zn[0][0]);
Assert.assertEquals("last",7.0, zn[1][0]);
simplex.iterate();
zn=simplex.getZn();
xb=simplex.getXb();
Assert.assertEquals(4.0, xb[0][0]);
Assert.assertEquals(5.0, xb[1][0]);
Assert.assertEquals("de",2.0, xb[2][0]);
Assert.assertEquals(5.0, zn[0][0]);
Assert.assertEquals("last",2.0, zn[1][0]);
Assert.assertFalse(simplex.iterate());
}
public void testOtherform() {
double [][]Anext= new double[3][6];
Anext[0][0]=2;
Anext[0][1]=3;
Anext[0][2]=1;
Anext[0][3]=1;
Anext[0][4]=0;
Anext[0][5]=0;
Anext[1][0]=4;
Anext[1][1]=1;
Anext[1][2]=2;
Anext[1][3]=0;
Anext[1][4]=1;
Anext[1][5]=0;
Anext[2][0]=3;
Anext[2][1]=4;
Anext[2][2]=2;
Anext[2][3]=0;
Anext[2][4]=0;
Anext[2][5]=1;
double bnext[][]=new double [3][1];
bnext[0][0]=5;
bnext[1][0]=11;
bnext[2][0]=8;
double cnnext[][]= new double[3][1];
cnnext[0][0] =5;
cnnext[1][0] =4;
cnnext[2][0] =3;
int Bindicenext[]= new int[3];
Bindicenext[0]=3;
Bindicenext[1]=4;
Bindicenext[2]=5;
int Nindicenext[]= new int[3];
Nindicenext[0]=0;
Nindicenext[1]=1;
Nindicenext[2]=2;
simplexe simplex= new simplexe(Anext,bnext,cnnext,Bindicenext,Nindicenext);
simplex.init();
while(simplex.iterate())
{
}
simplex.getXb();
}
public void testOtherform2() {
double [][]Anext= new double[3][7];
Anext[0][0]=3;
Anext[0][1]=2;
Anext[0][2]=1;
Anext[0][3]=2;
Anext[0][4]=1;
Anext[0][5]=0;
Anext[0][6]=0;
Anext[1][0]=1;
Anext[1][1]=1;
Anext[1][2]=1;
Anext[1][3]=1;
Anext[1][4]=0;
Anext[1][5]=1;
Anext[1][6]=0;
Anext[2][0]=4;
Anext[2][1]=3;
Anext[2][2]=3;
Anext[2][3]=4;
Anext[2][4]=0;
Anext[2][5]=0;
Anext[2][6]=1;
double bnext[][]=new double [3][1];
bnext[0][0]=255;
bnext[1][0]=117;
bnext[2][0]=420;
double cnnext[][]= new double[4][1];
cnnext[0][0] =19;
cnnext[1][0] =13;
cnnext[2][0] =12;
cnnext[3][0] =17;
int Bindicenext[]= new int[3];
Bindicenext[0]=4;
Bindicenext[1]=5;
Bindicenext[2]=6;
int Nindicenext[]= new int[4];
Nindicenext[0]=0;
Nindicenext[1]=1;
Nindicenext[2]=2;
Nindicenext[3]=3;
simplexe simplex= new simplexe(Anext,bnext,cnnext,Bindicenext,Nindicenext);
simplex.init();
while(simplex.iterate())
{
}
simplex.getXb();
}
public void testOtherformminim() {
double [][]Anext= new double[3][5];
Anext[0][0]=-3;
Anext[0][1]=2;
Anext[0][2]=1;
Anext[0][3]=0;
Anext[0][4]=0;
Anext[1][0]=-1;
Anext[1][1]=2;
Anext[1][2]=0;
Anext[1][3]=1;
Anext[1][4]=0;
Anext[2][0]=1;
Anext[2][1]=1;
Anext[2][2]=0;
Anext[2][3]=0;
Anext[2][4]=1;
double bnext[][]=new double [3][1];
bnext[0][0]=2;
bnext[1][0]=4;
bnext[2][0]=5;
double cnnext[][]= new double[2][1];
cnnext[0][0] =1;
cnnext[1][0] =2;
int Bindicenext[]= new int[3];
Bindicenext[0]=2;
Bindicenext[1]=3;
Bindicenext[2]=4;
int Nindicenext[]= new int[2];
Nindicenext[0]=0;
Nindicenext[1]=1;
simplexe simplex= new simplexe(Anext,bnext,cnnext,Bindicenext,Nindicenext);
simplex.init();
int iter=0;
while(simplex.iterate())
{
iter++;
}
simplex.getXb();
}
public void testOtherformminim2() {
double [][]Anext= new double[2][5];
Anext[0][0]=1;
Anext[0][1]=3;
Anext[0][2]=0;
Anext[0][3]=-1;
Anext[0][4]=0;
Anext[1][0]=1;
Anext[1][1]=1;
Anext[1][2]=-1;
Anext[1][3]=0;
Anext[1][4]=1;
double bnext[][]=new double [2][1];
bnext[0][0]=4;
bnext[1][0]=10;
double cnnext[][]= new double[3][1];
cnnext[0][0] =-1;
cnnext[1][0] =1;
cnnext[2][0] =-1;
int Bindicenext[]= new int[2];
Bindicenext[0]=3;
Bindicenext[1]=4;
int Nindicenext[]= new int[3];
Nindicenext[0]=0;
Nindicenext[1]=1;
Nindicenext[2]=2;
simplexe simplex= new simplexe(Anext,bnext,cnnext,Bindicenext,Nindicenext);
simplex.init();
int iter=0;
while(simplex.iterate())
{
iter++;
}
simplex.getXb();
}
public void testOtherformminim3() {
double [][]Anext= new double[3][6];
Anext[0][0]=2;
Anext[0][1]=1;
Anext[0][2]=4;
Anext[0][3]=-1;
Anext[0][4]=0;
Anext[0][5]=0;
Anext[1][0]=3;
Anext[1][1]=2;
Anext[1][2]=3;
Anext[1][3]=0;
Anext[1][4]=-1;
Anext[1][5]=0;
Anext[2][0]=2;
Anext[2][1]=1;
Anext[2][2]=1;
Anext[2][3]=0;
Anext[2][4]=0;
Anext[2][5]=1;
double bnext[][]=new double [3][1];
bnext[0][0]=8;
bnext[1][0]=5;
bnext[2][0]=100;
double cnnext[][]= new double[3][1];
cnnext[0][0] =-90;
cnnext[1][0] =-81;
cnnext[2][0] =100;
int Bindicenext[]= new int[3];
Bindicenext[0]=3;
Bindicenext[1]=4;
Bindicenext[2]=5;
int Nindicenext[]= new int[3];
Nindicenext[0]=0;
Nindicenext[1]=1;
Nindicenext[2]=2;
simplexe simplex= new simplexe(Anext,bnext,cnnext,Bindicenext,Nindicenext);
simplex.init();
int iter=0;
while(simplex.iterate())
{
iter++;
}
simplex.getXb();
}
public void testOtherformmax4() {
double [][]Anext= new double[2][4];
Anext[0][0]=2;
Anext[0][1]=2;
Anext[0][2]=1;
Anext[0][3]=0;
Anext[1][0]=3;
Anext[1][1]=1;
Anext[1][2]=0;
Anext[1][3]=1;
double bnext[][]=new double [2][1];
bnext[0][0]=300;
bnext[1][0]=400;
double cnnext[][]= new double[2][1];
cnnext[0][0] =25;
cnnext[1][0] =15;
int Bindicenext[]= new int[2];
Bindicenext[0]=2;
Bindicenext[1]=3;
int Nindicenext[]= new int[2];
Nindicenext[0]=0;
Nindicenext[1]=1;
simplexe simplex= new simplexe(Anext,bnext,cnnext,Bindicenext,Nindicenext);
simplex.init();
int iter=0;
while(simplex.iterate())
{
iter++;
}
simplex.getXb();
}
}
No comments:
Post a Comment