我正在NSGA-II算法中实现非支配排序子程序。以下是代码的初始部分:
public ArrayList<ArrayList<AttStrategy>> nondominatedSorting
(ArrayList<AttStrategy> StrategyPopulation, double max_rho_mal, double p_max) {
int i = 0, p, q;
int [] domination_count = new int[StrategyPopulation.size()];
ArrayList<AttStrategy> dominated_strategies = new ArrayList<>();
ArrayList<AttStrategy> dominating_strategies = new ArrayList<>();
ArrayList<ArrayList<AttStrategy>> dominated_strategies_list = new ArrayList<>();
ArrayList<ArrayList<AttStrategy>> pareto_strategies = new ArrayList<>();
while(i < StrategyPopulation.size()) {
int j = 0;
domination_count[StrategyPopulation.get(i).getId()] = 0;
dominated_strategies.clear();
for (j = i+1; j < StrategyPopulation.size(); j++){
if (constraintDominated(StrategyPopulation.get(i), StrategyPopulation.get(j), max_rho_mal, p_max))
dominated_strategies.add(StrategyPopulation.get(j));
else if (constraintDominated(StrategyPopulation.get(j), StrategyPopulation.get(i), max_rho_mal, p_max))
domination_count[StrategyPopulation.get(i).getId()] += 1;
}
if (domination_count[StrategyPopulation.get(i).getId()] == 0) {
dominating_strategies.add(StrategyPopulation.get(i));
}
dominated_strategies_list.add(StrategyPopulation.get(i).getId(), dominated_strategies);
//values getting stored in the arraylist
System.out.println("Size of " + StrategyPopulation.get(i).getId() + ": " + dominated_strategies_list.get(i).size());
i = i + 1;
} //while-loop ends
//values getting reset to 0.
System.out.println("********Outside the loop*********");
for(i = 0; i < dominated_strategies_list.size(); i++){
System.out.println("Size of " + i + ": " + dominated_strategies_list.get(i).size());
}
//.............................................
}
我遇到的问题是2-D arraylist dominated_strategies_list 。这个arraylist正在while循环中填充,但是一旦在循环外部使用它,所有值都将重置为0.但是,其他arraylists和在循环内更新的数组会将它们的值保留在循环之外。因此,我不能在子例程的后面部分使用这个arraylist( dominated_strategies_list )。我在哪里弄错了?建议一些可能的解决方案。
答案 0 :(得分:0)
您的问题是,您只有一个dominated_strategies
列表,您可以重复填写并清除。
该行
dominated_strategies_list.add(StrategyPopulation.get(i).getId(), dominated_strategies);
只会将此单个列表的引用添加到dominated_strategies_list
,它不会复制dominated_strategies
列表,因此在下一轮循环中,dominated_strategies.clear();
会丢弃您之前的结果
解决方案是替换
dominated_strategies.clear();
与
dominated_strategies = new ArrayList<>();
答案 1 :(得分:0)
在每次迭代中,您都要清除引用的ArrayList,而不是尝试创建新的ArrayList。
以下是展示错误和解决方案的代码的简化示例。
public static void main(String[] args){
//Initialize stuff
ArrayList<Integer> dom_strat = new ArrayList<>();
ArrayList<ArrayList<Integer>> dom_list = new ArrayList<>();
dom_strat.add(5);
dom_strat.add(3);
//Add arrayList
dom_list.add(dom_strat);
//Print
System.out.println("Before clear");
for(ArrayList<Integer> aList:dom_list){
for(Integer i: aList){
System.out.println(i);
}
}
//Clear referenced arrayList dom_list
dom_strat.clear();
//Print
System.out.println("After clear");
for(ArrayList<Integer> aList:dom_list){
for(Integer i: aList){
System.out.println(i);
}
}
//Initialize again and apply the fix
dom_strat = new ArrayList<>();
dom_list = new ArrayList<>();
dom_strat.add(5);
dom_strat.add(3);
//Add arrayList
dom_list.add(dom_strat);
//Instead of clear, create a new arrayList
dom_strat = new ArrayList<>();
//Print
System.out.println("After applying the fix");
for(ArrayList<Integer> aList:dom_list){
for(Integer i: aList){
System.out.println(i);
}
}
}