我觉得我的问题很简单,但我不知道该怎么去Google。访问另一个对象中创建的对象
我有一个run.java基本上做到这一点
public class run {
//stuff
public static void main(String[] args) {
//stuff
Solver solver = new Solver(body,seeds);
solver.solve();
LogTool.print("OUTPUT field of CLASS CREATED IN solver -- " + IDONTKNOWHOW,"notification");
}
}
===========
public class Solver {
//stuff
public void solve() {
//stuff
GlobalState GLowestState = new GlobalState(this.Cur_state);
}
}
所以,我如何才能从run.java中访问GLowestState?
是否有区别,GLowestState是用Singleton实现的?
我希望不是。
我无法尝试任何事情,因为我的IDE给我一个GLowestState.IDONTKNOWHOW错误。
==============
更新与更多的代码:
public class run {
public static void main(String[] args) {
Voxel [][][] body = new Voxel[Config.xDIM][Config.yDIM][Config.zDIM];
//stuff
Solver solver = new Solver(body,seeds);
LogTool.print("Initialized Solver Object!","notification");
LogTool.print("Beginning Annealing...","notification");
looper.solveSA();
GlobalState GLS = looper.getGLowestState(); <--- NPE here
LogTool.print("GLC: " + looper.getGlobal_lowest_cost()+ " CURC: " + looper.getCur_cost(),"notification");
// LogTool.print("GLS external: " + GLS,"notification");
LogTool.print("SolveSA: Global Current Best Solution : " + looper.getGlobal_Lowest_state_string(),"notification");
=========
public class Looper {
public static Voxel [][][] body; //Thobi hat das als ganz einfache Variable in seiner Loesermethode...nicht so OOP
public static Seed[] seeds = new Seed[Config.SAnumberOfSeeds];
public double[] Cur_state = new double[Config.SAnumberOfSeeds];
public double[] New_state = new double[Config.SAnumberOfSeeds]; // Do I even need this ?
public double[] Global_Lowest_state = new double[Config.SAnumberOfSeeds]; // Do I even need this ?
GlobalState GLowestState;
public Looper(Voxel [][][] body, Seed[] seeds) {
this.temperature = Config.StartTemp;
this.body = body;
this.seeds = seeds;
this.Cur_cost = Cur_cost;
this.New_cost = New_cost;
this.temperature = temperature;
this.Cur_state = Cur_state;
this.New_state = New_state;
this.Global_lowest_cost = Double.MAX_VALUE;
this.Global_Lowest_state = Global_Lowest_state;
}
public GlobalState getGLowestState() {
return GLowestState;
}
for (int ab = 0; ab < Config.NumberOfMetropolisResets; ab++) {
LogTool.print("==================== START CALC FOR OUTER ROUND " + ab + "=========================","notification");
if (Config.SAverboselvl==1) {
LogTool.print("SolveSA: Cur_State Read before Metropolis : A)" + Cur_state[0] + " B) " + Cur_state[1] + " C) " + Cur_state[2],"notification");
LogTool.print("Debug: GLS get 1: " + this.getGlobal_Lowest_state_string(),"notification");
}
if (ab==0){
this.initState();
if (Config.SAverboselvl==1) {
LogTool.print("SolveSA: Cur_state after Initstate : A)" + Cur_state[0] + " B) " + Cur_state[1] + " C) " + Cur_state[2],"notification");
}
}
setCur_cost(cost());
/* [Newstate] with random dwelltimes */
newState();
if (Config.SAverboselvl==1) {
LogTool.print("SolveSA: New State before Metropolis: A)" + New_state[0] + " B) " + New_state[1] + " C) " + New_state[2],"notification");
}
setNew_cost(cost());
if (Config.SAverboselvl==1) {
LogTool.print("SolveSA: New Cost : " + New_cost,"notification");
}
double random_double = RandGenerator.randDouble(0.01, 0.99);
/**
* MetropolisLoop
* @param Config.NumberOfMetropolisRounds
*/
for(int x=0;x<Config.NumberOfMetropolisRounds;x++) {
// break;
// LogTool.print("SolveSA Iteration " + x + " Curcost " + Cur_cost + " Newcost " + New_cost,"notification");
if ((Cur_cost - New_cost)>0) { // ? die Kosten
if (Config.SAverboselvl>1) {
LogTool.print("Fall 1","notification");
}
if (Config.SAdebug) {
LogTool.print("SolveSA: Metropolis NewCost : " + this.getNew_cost(),"notification");
LogTool.print("SolveSA: Metropolis CurCost : " + this.getCur_cost(),"notification");
LogTool.print("SolveSA Cost delta " + (Cur_cost - New_cost) + " ","notification");
}
Cur_state = New_state;
Cur_cost = New_cost;
} else if (Math.exp(-(Cur_cost - New_cost)/temperature)> random_double) {
Cur_state = New_state;
Cur_cost = New_cost;
if (Config.SAdebug) {
LogTool.print("SolveSA: NewCost : " + this.getNew_cost(),"notification");
LogTool.print("SolveSA: CurCost : " + this.getCur_cost(),"notification");
}
if (Config.SAverboselvl>1) {
LogTool.print("Fall 2: Zufallsgenerierter Zustand traegt hoehere Kosten als vorhergehender Zustand. Iteration: " + x,"notification");
}
}
temperature = temperature-1;
if (temperature==0) {
break;
}
random_double = RandGenerator.randDouble(0.01, 0.99);
newState();
setNew_cost(cost());
}
if (ab==9) {
double diff=0;
}
//This is where the trouble happens - GlobalLoewst cost is set correctly and kept throughout the loops, GLowestState is always the last value of Cur_State (the most recent completed iteration. If smoothly running, that would be iteration 9 and inner iteration 99) @stackexchange
if (Cur_cost<Global_lowest_cost) {
this.setGlobal_lowest_cost(Cur_cost);
GlobalState GLowestState = new GlobalState(this.Cur_state);
LogTool.print("GLS DEDICATED OBJECT STATE OUTPUT -- " + GLowestState.getGlobal_Lowest_state_string(),"notification");
this.setGlobal_Lowest_state(GLowestState.getDwelltimes());
LogTool.print("READ FROM OBJECT OUTPUT -- " + this.getGlobal_Lowest_state_string(),"notification");
// LogTool.print("DEBUG: CurCost direct : " + this.getCur_cost(),"notification");
// LogTool.print("Debug: Cur<global CurState get : " + this.getCur_state_string(),"notification");
// LogTool.print("Debug: Cur<global GLS get : " + this.getGlobal_Lowest_state_string(),"notification");
// this.setGlobal_Lowest_state(this.getCur_state(Cur_state));
// LogTool.print("Debug: Cur<global GLS get after set : " + this.getGlobal_Lowest_state_string(),"notification");
}
LogTool.print("SolveSA: Iteration : " + ab,"notification");
LogTool.print("SolveSA: Last Calculated New State/Possible state inner loop 99 : " + this.getNew_state_string(),"notification");
// LogTool.print("SolveSA: Best Solution : " + this.getCur_state_string(),"notification");
LogTool.print("SolveSA: GLS after: " + this.getGlobal_Lowest_state_string(),"notification");
LogTool.print("SolveSA: NewCost : " + this.getNew_cost(),"notification");
LogTool.print("SolveSA: CurCost : " + this.getCur_cost(),"notification");
}
}
=================
public final class GlobalState implements Comparable<Object>{
private double[] dwelltimes;
private static GlobalState instance = null;
protected GlobalState(){
// Exists only to defeat instantiation
}
public static GlobalState getInstance(){
if (instance==null){
instance = new GlobalState();
}
return instance;
}
public GlobalState(double[] dwelltimes) {
this.dwelltimes = dwelltimes;
}
@Override
public int compareTo(Object o) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public double[] getDwelltimes() {
return dwelltimes;
}
public String getGlobal_Lowest_state_string() {
String Global_Lowest_state_string = new String();
for (int cc = 0; cc < Config.SAnumberOfSeeds; cc++) {
Global_Lowest_state_string = Global_Lowest_state_string.concat(" " + cc + ") " + dwelltimes[cc]);
}
return Global_Lowest_state_string;
}
public void setDwelltimes(double[] dwelltimes_x) {
this.dwelltimes = dwelltimes_x;
}
}
的'范围得到它GLowestState'仅限于目前的解决()方法。 – 2015-01-20 20:06:44
我的答案如何在丹尼尔之下?你需要使用'public'属性。 – 2015-01-20 20:34:47
我想提供更多的上下文。我原本有一个GlobalLowestState字段(简洁的GLS)。如果满足某个条件,我写信给该字段 - 这是在solve()方法中检查的。 问题是GLS取了最后计算的Cur_State的值而不是全局lpowest状态的值(这是检查的目的)。我发现代码没有错,所以我将全局最低值写入对象GLS而不是字段。奇迹般地,GLS对象包含了正确的值。但是,即使使用get(),它也不能在solver()类之外访问。 – 2015-01-21 16:43:43