/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package schachteln;
/**
*
* @author Andreas
*/
public class finder {
public static double[][][] bisection(int levelmax, int level, double[][] control, double[][] controlLimits, int controlIndex, double[] dtPhase, double[][] startconditions, double[][] dataSC, int[] stepsPhase, double stepsize){
//double[][][] result = new double[2][][]; //{target[1],control[x][y]}
double[][][] result = new double[2][][]; //{target[1],control[x][y]}
result[0] = new double[1][1];
result[1] = control;
double[][][] resultl = new double[2][][];
double[][][] resultr = new double[2][][];
double[][][] resultm = new double[2][][];
// grenzen
double boarderXl = 0; // linke grenze
double boarderXr = 0; // rechte grenze
if(controlLimits[level][0] != controlLimits[level][1]){
//System.out.println("nicht gleich!");
if(controlLimits[level][0]<controlLimits[level][1]){
boarderXl = controlLimits[level][0];
boarderXr = controlLimits[level][1];
} else if(controlLimits[level][0]>controlLimits[level][1]){
boarderXl = controlLimits[level][1];
boarderXr = controlLimits[level][0];
}
// stellen
double Xl = boarderXl;
double Xr = boarderXr;
double Xm = 0;
if(level%2==0){
Xm = (Xr+Xl)/2; // X Mitte
} else if(level%2==1){
Xm = Math.round((Xr+Xl)/2); // INTcode X Mitte
}
// 1. von hier parallel
// werte
control[level][controlIndex] = Xl;
resultl = recursion.out(levelmax, level, control, controlLimits, controlIndex, dtPhase, startconditions, dataSC, stepsPhase, stepsize);
double Yl = resultl[0][0][0];
control[level][controlIndex] = Xr;
resultr = recursion.out(levelmax, level, control, controlLimits, controlIndex, dtPhase, startconditions, dataSC, stepsPhase, stepsize);
double Yr = resultr[0][0][0];
control[level][controlIndex] = Xm;
resultm = recursion.out(levelmax, level, control, controlLimits, controlIndex, dtPhase, startconditions, dataSC, stepsPhase, stepsize);
double Ym = resultm[0][0][0];
// 1. bis hier parallel
// temporaere umfuelldaten
double tmpXl=0;
double tmpXr=0;
// abbruchkriterien
int counterMAX = 30; // damit die schleife sich nicht unendlich wiederholt notaus bei counterMAX schleifen
int counter = 1; // weil direkt fuer die ersten werte schon die halbierung gemacht wird
int stop = 0;
//System.out.println("level="+level+" Xl= " + Xl + " Yl= " + Yl + " | Xm= " + Xm + " Ym= " + Ym + " | Xr= " + Xr + " Yr= " + Yr);
//INTcode aber auch nochmal fuer double ueberdenken!
if(level%2==1 && (Xr-Xl)+1==2){ //+1 weil 0bis1=2intstellen sind
//System.out.println("(Yr-Yl)+1==3");
stop = 1;
if(Yl < Ym && Ym <= Yr){
//System.out.println("LoesungLi: Xl= " + Xl + " Yl= " + Yl);
result[0][0][0] = Yl;
control[level][controlIndex] = Xl;
result[1] = control;
} else if(Yl >= Ym && Ym > Yr){
//System.out.println("LoesungRe: Xr= " + Xr + " Yr= " + Yr);
result[0][0][0] = Yr;
control[level][controlIndex] = Xr;
result[1] = control;
} else{
System.out.println("1. ungenau in Bisektion Level "+ level +" in INTEGER");
}
}
// INTcode ende
//for(int i=0; i<11; i++){
while(stop==0){
if(Yl < Ym && Ym <= Yr || Yl < Ym && Ym > Yr && Yl < Yr){
//System.out.println("links kleiner");
if((Xl+(Xl-Xm)) < boarderXl){
//Xl = Xl;
} else{
tmpXl = Xl+(Xl-Xm);
Xl = tmpXl;
// 2. von hier parallel
control[level][controlIndex] = Xl;
resultl = recursion.out(levelmax, level, control, controlLimits, controlIndex, dtPhase, startconditions, dataSC, stepsPhase, stepsize);
Yl = resultl[0][0][0];
}
Xr = Xm;
if(level%2==0){
Xm = (Xr+Xl)/2;
} else if(level%2==1){
Xm = Math.round((Xr+Xl)/2); // INTcode
}
resultr = resultm;
Yr = resultr[0][0][0];
control[level][controlIndex] = Xm;
resultm = recursion.out(levelmax, level, control, controlLimits, controlIndex, dtPhase, startconditions, dataSC, stepsPhase, stepsize);
Ym = resultm[0][0][0];
// 2. bis hier parallel
} else if(Yl >= Ym && Ym > Yr || Yl > Yr && Yl < Ym && Ym > Yr){
//System.out.println("rechts kleiner");
if((Xr+(Xr-Xm)) > boarderXr){
//Xr = Xr;
} else{
tmpXr = Xr+(Xr-Xm);
Xr = tmpXr;
// 3. von hier parallel
control[level][controlIndex] = Xr;
resultr = recursion.out(levelmax, level, control, controlLimits, controlIndex, dtPhase, startconditions, dataSC, stepsPhase, stepsize);
Yr = resultr[0][0][0];
}
Xl = Xm;
if(level%2==0){
Xm = (Xr+Xl)/2;
} else if(level%2==1){
Xm = Math.round((Xr+Xl)/2); // INTcode
}
resultl = resultm;
Yl = resultl[0][0][0];
control[level][controlIndex] = Xm;
resultm = recursion.out(levelmax, level, control, controlLimits, controlIndex, dtPhase, startconditions, dataSC, stepsPhase, stepsize);
Ym = resultm[0][0][0];
// 3. bis hier parallel
} else if(Yl > Ym && Ym <= Yr || Yl >= Ym && Ym < Yr){
//System.out.println("nix kleiner");
if(level%2==0){
tmpXl = (Xm+Xl)/2;
tmpXr = (Xr+Xm)/2;
} else if(level%2==1){
tmpXl = Math.round((Xm+Xl)/2); // INTcode
tmpXr = Math.round((Xr+Xm)/2); // INTcode
}
Xl = tmpXl;
Xr = tmpXr;
// 4. von hier parallel
control[level][controlIndex] = Xl;
resultl = recursion.out(levelmax, level, control, controlLimits, controlIndex, dtPhase, startconditions, dataSC, stepsPhase, stepsize);
Yl = resultl[0][0][0];
control[level][controlIndex] = Xr;
resultr = recursion.out(levelmax, level, control, controlLimits, controlIndex, dtPhase, startconditions, dataSC, stepsPhase, stepsize);
Yr = resultr[0][0][0];
// 4. bis hier parallel
} else{
System.out.println("2. ungenau in Bisektion Level "+ level +" Xl="+Xl+";Yl="+Yl+";Xm="+Xm+";Ym="+Ym+";Xr="+Xr+";Yr="+Yr+" in DOUBLE");
}
counter = counter + 1;
if(level%2==0){
if((Xr-Xm) <= stepsize || counter >= counterMAX){
//System.out.println(Xr-Xm);
//System.out.println(counter);
stop = 1;
}
} else if(level%2==1){
// INTcode
if((Xr-Xm)<=1 && (Xm-Xl)<=1 && Ym<=Yr && Ym<=Yl || (Xr-Xm)<=1 && (Xm-Xl)<=1 && Ym>=Yr && Ym>=Yl || counter>=counterMAX){
//System.out.println("abstand=1; " + (Xr-Xm) +";" + (Xm-Xl)+ " Yl="+Yl+";Ym="+Ym+";Yr="+Yr+" counter="+counter);
//System.out.println(Xr-Xm);
//System.out.println(counter);
stop = 1;
}
}
//System.out.println("level="+level+" Xl= " + Xl + " Yl= " + Yl + " | Xm= " + Xm + " Ym= " + Ym + " | Xr= " + Xr + " Yr= " + Yr);
}
// PLATZHALTER: hier werden dann die letztendlichen rueckgabewerte erzeugt
if(Yl < Ym && Ym <= Yr){
//System.out.println("Level="+level+" Loesung: Xl= " + Xl + " Yl= " + Yl);
result[0][0][0] = Yl;
control[level][controlIndex] = Xl;
result[1] = control;
//output = Xl;
} else if(Yl >= Ym && Ym > Yr){
//System.out.println("Level="+level+" Loesung: Xr= " + Xr + " Yr= " + Yr);
result[0][0][0] = Yr;
control[level][controlIndex] = Xr;
result[1] = control;
//output = Xr;
} else if(Yl > Ym && Ym <= Yr || Yl >= Ym && Ym < Yr){
//System.out.println("Level="+level+" Loesung: Xm= " + Xm + " Ym= " + Ym);
result[0][0][0] = Ym;
control[level][controlIndex] = Xm;
result[1] = control;
//output = Xm;
} else{
System.out.println("3. ungenau in Bisektion Level "+ level +" in DOUBLE Loesung");
}
} else{
System.out.println("input1 und input2 sind gleich");
// PLATZHALTER: hier werden dann die letztendlichen rueckgabewerte erzeugt
result[0][0][0] = result[0][0][0];
control[level][controlIndex] = controlLimits[level][0];
result[1] = control;
//output = controlLimits[level][0];
}
return result;
// bisection
// hier muss der bisectionsalgo control zwischen den controlLimits verandern
// und auf die trajectories zugreifen.
//System.out.println("bisection in level= " + level + " controlIndex=" +controlIndex);
//result = recursion.out(levelmax, level, control, controlLimits, controlIndex, dtPhase, startconditions, dataSC, stepsPhase, stepsize);
//return result;
}
}