📄 analysis.java
字号:
// System.out.println("extendRows =" + extendRows + " extendCols =" + extendCols);
//}
}
else { //there aren't rows that satisfy P+ or P- are empty
// System.out.println("Section 1.1.b has been entered");
int pplus[] = new int[extendCols];
int pminus[] = new int[extendCols];
initialiseArray(pplus, -1);
initialiseArray(pminus, -1);
if (modulus1_1b(pplus, pminus)){
int k = -1;//there is a row where mod(P+) = 1 or mod(P-) = 1
if (pplus[0] != -1 && pminus[0] != -1){
//i.e there are rows where p+ and p- equal 1
//Then use the P+
int rowP = pplus[0];
k = pPlusCombine(rowP);
// System.out.println("Program reached position 1");
}
else if (pplus[0] != -1){
int rowP = pplus[0];
k = pPlusCombine(rowP);
//print2DArray(extend, extendRows, extendCols);
//System.out.println("Program reached position 2");
}
else if (pminus[0] != -1){
int rowP = pminus[0];
//System.out.println("pminus[0]= " + pminus[0]);
k = pMinusCombine(rowP);
//System.out.println("Program reached position 3 k=" + k );
}
//now delete the row k
if (k!= -1){
deleteExtendedColumn(k);
//System.out.println("Program reached position 4");
//print2DArray(extend, extendRows, extendCols);
}
}
else {
//find a nonZeroRow of C
//k is a column such that Chk != 0
int h = firstNonZeroRowC();
if (h != -1){
int k = firstNonZeroColumn(h);
//System.out.println("in 1.1.b.2 h=" + h + " k=" + k);
substituteColumns2(h,k);
deleteExtendedColumn(k); //now delete the firstNonZeroColumn
//print2DArray(extend, extendRows, extendCols);
}
}
}//End of 1st else
} //End of phase1 loop
//Now in phase2
phase2(transpose);
}//End of function
//phase 2 executes phase2 of the algorithm
private void phase2(){
phase2(false);
}
private void phase2(boolean transpose){
//now setup B so that it contains only
//System.out.println("\nEntering phase 2");
b = generateInitialiseZeroArray(b, tranNum, tranNum);
this.b = b;
//print2DArray(b,2,2);
//initialiseZero(b);
this.b = copyB();
//System.out.println("Printing Array after copyB!");
// print2DArray(b, bRows, bCols);
int h = -1;
while (rowWithNegativeElementB()){
//System.out.println("Beginning of loop");
h = rowWithNegativeElementBNumber();
// System.out.println("The value of h= " + h);
int pPlus[] = new int [bCols+2];
int pMinus[] = new int [bCols+2];
setPPlusAndPMinus(b,h, pPlus, pMinus); //get P+ and P- sets
if (pPlus[0] != -1){ //There are elements in the row with +ve values
//System.out.println("pPlus != -1");
for (int j=0; pPlus[j] != -1; j++){
for (int k =0; pMinus[k] != -1; k++){
//System.out.println("Reached position 1");
//now perform a linear combination of j and k to form another column
//first check if there is a spare column in B
if (b.length == bCols)
b = addColumnToB();
int newCol = bCols;
linearCombToNewColumn(pPlus[j], pMinus[k], h, newCol);
divideByGCD(b,newCol); //divide by the columns GCD
bCols++;
}
}
//now delete from B all columns of index k in P-
for (int i=0; pMinus[i] != -1; i++){
deleteColumn(b, pMinus[i], bCols);
bCols--;
}
// System.out.println("2D Array");
print2DArray(b,bRows, bCols);
}//endfor
}//endwhile
//now delete from B all columns having non-minimal support??
//see the other paper for details of this!!!!
//System.out.println("Should now delete all columns with nonminimal support");
deleteColsWithoutMinimalSupport();
if (!transpose)
tinvars(); //
else {
}
//print2DArray(b, bRows, bCols);
}//End of function
//deletes a column from an array where colNum is the column to be deleted and
//totalColNum is the total number of columns used in the array
private void deleteColumn(int array [][], int colNum , int totalColNum){
for (int i = colNum; i < totalColNum-1; i++){
for (int j =0; j < array.length; j++){
array[j][i] = array[j][i+1];
}
}
}
private void linearCombToNewColumn(int j, int k, int h, int newCol){
int alpha, beta;
//I'm not sure what alpha and beta should be!
//Need to see Alaiwan and Memmi
//In the other implementation alpha is +abs and beta is -abs
alpha = Math.abs(b[h][j]);
beta = Math.abs(b[h][k]);
// System.out.println("In linearCombTo j= " + j + "k= " + k);
for (int x =0; x < bRows; x++){
if (x==h)
b[x][newCol] = 0;
else
b[x][newCol] = beta * b[x][j] + alpha* b[x][k];
}
//is this correct??? The other implementation has
//something strange!!!
}
//Divides a column in array[][] by its greatest common denominator
private void divideByGCD(int array[][], int colNum){
int gcd = getGCD(array, colNum);
if (gcd == 0){
//System.out.println("GCD==0");
return;
}
for (int i=0; i < bRows; i++){
array[i][colNum]/= gcd;
//System.out.println("array[" + i + "][" + colNum + "] =" + array[i][colNum]);
}
}
//End of function///////////
//returns the greatest common denominator of the column
private int getGCD(int array[][], int colNum){
int lowestGCD =0;
for (int i=0; i < bRows; i++){
for (int j =i; j < bRows; j++){
int tempGCD = gcd(Math.abs(array[i][colNum]), Math.abs(array[j][colNum]));
if (lowestGCD ==0)
lowestGCD = tempGCD;
else if (tempGCD < lowestGCD && tempGCD != 0)
lowestGCD = tempGCD;
// System.out.println("gcd= " + lowestGCD);
// for (int p=0; p < bRows; p++){
// System.out.print(" " + array[p][colNum]);
// }
}
}
return lowestGCD;
}
//End of function///////////
//returns the greates common denominator of x and y
private int gcd(int x, int y){
//System.out.println("x= " +x + " y=" + y);
if (x == 0)
return y;
if (y ==0)
return x;
if (x == y)
return x;
if (x < y)
return gcd(x, (y-x));
else
return gcd(y, (x-y));
}
//End of Function////////////
//adds a column to Array B
private int [][] addColumnToB(){
int tempA [][] = new int[b.length+1][];
for (int i=0; i < b.length; i++)
tempA[i] = new int[bRows];
//now initialise equal to B
for (int i=0; i < b.length; i++){
for (int j=0; j < b[i].length; j++){
tempA[i][j] = b[i][j];
}
}
b = tempA; //I hope this works!!!!!
//bCols++; //the actual number of Columns that have actual data in them has
//not changed
return b;
}
//places the indices of columns in row h of -ve nums in pMinus and of pos
//values in pPlus
private void setPPlusAndPMinus(int array[][], int h, int pPlus[], int pMinus[]){
int pPlusPos = 0;
int pMinusPos = 0;
for (int j=0; j < bCols; j++){
//System.out.println("value of j=" + j + "value of h" + h);
if (array[h][j] < 0){
pMinus[pMinusPos] = j;
pMinusPos++;
}
else if(array[h][j] > 0){
pPlus[pPlusPos] = j;
pPlusPos++;
}
}
pPlus[pPlusPos] = -1;
pMinus[pMinusPos] = -1;
//System.out.print("\nPrinting pPlus ");
// for (int z=0; pPlus[z] != -1; z++){
//System.out.print(" " + pPlus[z]);
// }
//System.out.print("\nPrinting pMinus ");
// for (int z=0; pMinus[z] != -1; z++){
//System.out.print(" " + pMinus[z]);
// }
}
//End of function//////////////
//Identifies if there is an a negative element in B[][]
private boolean rowWithNegativeElementB(){
//System.out.println("bRows= " + bRows + " bCols =" + bCols);
for (int i=0; i < bRows; i++){
for (int j=0; j< bCols; j++){
if (b[i][j] < 0){
//row = i;
return true;
}
}
}
return false;
}
private int rowWithNegativeElementBNumber(){
//System.out.println("bRows= " + bRows + " bCols =" + bCols);
for (int i=0; i < bRows; i++){
for (int j=0; j< bCols; j++){
if (b[i][j] < 0){
//row = i;
return i;
}
}
}
return -1;
}
//Copies the extended section corresponding to B to B[][]
private int[][] copyB(){
bRows = 0;
bCols = extendCols;
// System.out.println ("CRows-" + currCRowNum + " extendRows=" + extendRows + " extendCols=" + extendCols);
for (int i = currCRowNum, x =0; i < extendRows; i++, x++){
bRows++;
for (int j = 0; j < extendCols; j++){
b[x][j] = extend[i][j];
// System.out.println("b[x][j]=" + b[x][j] + " extend[x][j]=" + extend[x][j]);
//System.out.println(b[x][j]);
}
}
//System.out.println("bRows=" + bRows + " extenRows=" + extendRows);
return b;
}
//End of function////////
private int sign(int x){
if (x > 0)
return 1;
else if (x < 0)
return -1;
else
return 0;
}
//finds the element in rowP that is 1 and then calls linearCombination
//h is the row that satisfies |P+|=1 or |P-|=1
private int pPlusCombine(int h){
int k =-1;
for (int i=0; i < extend[h].length; i++){
if (extend[h][i] == 1){
k = i;
break;
}
}
if (k != -1){
substituteColumns1(h,k);
}
return k; //returns the index k
}
//finds
//////////////////////////////
private int pMinusCombine(int h){
int k =-1;
//System.out.println("pMinusCombine called for row h=" + h + "length=" + extend[h].length);
for (int i=0; i < extend[h].length; i++){
if (extend[h][i] == -1){
k = i;
//System.out.println("!!!!!k=" + k);
break;
}
}
if (k !=-1){
substituteColumns1(h,k);
}
return k; //returns the index k
}
//End of function/////////////////
//
private void substituteColumns1(int h, int k){
int j, alpha, beta;
int position = extend[h][k];
// if (position == 0){
// System.out.println("In SubstituteColumns1 position is equal to zero");
// }
beta = Math.abs(extend[h][k]); //is this correct?? Should it be [h][k] ???
if (position < 0){
for (j=0; j< extendCols; j++){
if (extend[h][j] > 0){
alpha = Math.abs(extend[h][j]); //should it be [h][j] ?????
//int beta = abs(extend[h][k]); //why is beta calculated??
linearCombination(j,k,alpha, beta);
}
}
}else {
for (j=0; j < extendCols; j++){
if (extend[h][j] < 0){
alpha = Math.abs(extend[h][j]); //should this be the other way round????
linearCombination(j,k,alpha,beta);
}
}
}
}
//End of function
private void substituteColumns2(int h, int k){
int j, alpha, beta;
int position = extend[h][k];
//if (position == 0){
// System.out.println("Position equals zero in substituteColumns2()");
//}
beta = Math.abs(extend[h][k]);
for (j=0; j<extendCols;j++){
if (j!=k && extend[h][j] != 0){
if(sign(extend[h][j]) != sign(extend[h][k]))
alpha = Math.abs(extend[h][j]);
else
alpha = - Math.abs(extend[h][j]);
linearCombination(j,k,alpha,beta);
}
}
}
public void addTransitionName(String transitionName){
transitionNames.addElement(transitionName);
tranNum++;
}
public void addPlaceName(String placeName){
placeNames.addElement(placeName);
placeNum++;
}
public void addArcSourceTarget(String arcSource, String arcTarget){
arcSources.addElement(arcSource);
arcTargets.addElement(arcTarget);
}
public void addArcValue(int value){
arcValues.addElement(new Integer(value));
}
public void addSubnetArcsToC(){
int num = subnetArcSource.size();
String arcSource;
String arcTarget;
int theValue;
Integer integerValue;
for (int i=0; i < num; i++){
arcSource = (String)subnetArcSource.elementAt(i);
arcTarget = (String)subnetArcTarget.elementAt(i);
integerValue = (Integer)subnetArcWeight.elementAt(i);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -