函数循环不止一次java
function loop for more than one times java
所以基本上我尝试从其他 class java 调用 return 数组的方法,它工作得很好,除了它是数组的大小或长度的两倍,是原始数组的两倍。
这是我的 return 数组和长度的代码。
public static double [] get_days(){
//extracted days from table into array
readFile();
double[] data = new double[list.size()];
System.out.println(list.size());
Integer[] daysArray = list.stream().map(Product::getDay)
.toArray(Integer[]::new);
for(int i = 0; i < daysArray.length; i++){
data[i] = Double.valueOf(daysArray[i]) ;
}
System.out.println("Array Size (Supposed to have 230 Data only) "+ data.length);
return data;
}
这是我调用另一个方法的方法 class
public class order_Picking extends AbstractProblem{
get_Product test = new get_Product();
public order_Picking(){
super(161,1,1);
}
public double [] var = new double[numberOfVariables];
public double [] Days = test.get_days();
@Override
public void evaluate (Solution solution){
System.out.println(Days.length);
//Jumlah produk pada batch ke-i pada picking list ke-i pada lokasi yang ke-i
for(int i=0; i< var.length;i++){
var[i]= EncodingUtils.getInt(solution.getVariable(i));
}
//jumlah ketersedian produk
int k1 = 100;
int k2 = 250;
int k3 = 150;
//Picking list-1
double [] pl1 = new double[3] ;
int p1 =100;
pl1[0]= p1;
int p2 = 20;
pl1[1]= p2;
int p3 = 40;
pl1[2]= p3;
int totalpl1 = p1+p2+p3;
//picking list-2
double [] pl2 = new double[3] ;
int p4 = 10;
pl2[0]= p4;
int p5 = 20;
pl2[1]= p5;
int p6 = 15;
pl2[2]= p6;
int totalpl2 = p4+p5+p6;
// Fungsi Tujuan untuk minimasi jarak
double f1 = distance(var) ;
double c1 = 0;
double c2 = 0;
for (int i = 0 ; i < var.length;i++){
c1 = (var[i]+var[i]*var[i])-totalpl1 ;
}
for (int i = 0 ; i < var.length;i++){
c2 = (var[i]+var[i]*var[i])-totalpl2 ;
}
//constraint picking list-1
//constraint picking list-2
solution.setObjective(0, f1);
solution.setConstraint(0,c1 == 0.0 ? 0.0 : c1);
solution.setConstraint(0,c2 == 0.0 ? 0.0 : c1);
}
@Override
public Solution newSolution() {
Solution solution = new Solution(161, 1, 1);
for (int i = 0 ; i<var.length;i++){
solution.setVariable(i,EncodingUtils.newBinaryInt(0,1));
}
return solution;
}
public static void main(String[] args) {
order_Picking value = new order_Picking();
NondominatedPopulation result = new Executor()
.withAlgorithm("GA")
.withProblemClass(order_Picking.class)
.withProperty("Populationsize",100)
.withProperty("sbx.rate",0.2)
.withProperty("pm",0.5)
.withMaxEvaluations(10000)
.run();
for (Solution solution : result) {
if (solution.violatesConstraints()) {
System.out.println("Solution for index 0 : " + +solution.getObjective(0));
for (int i = 0; i < value.var.length; i++){
System.out.println("Solusi variabel ke-"+i+" adalah "+ solution.getVariable(i));
}
}
}
}
public double distance (double [] x){
double a = 0;
for (int i = 0; i < x.length ; i++){
a += x[i];
}
return a;
}
}
该方法没有任何问题,但是当我在 public static void main 之外的其他 class 上调用它时,它似乎 运行 是 return 的两倍] 数组的大小超过 230,我不明白为什么它变成 460 而它应该是 230
here is the result on console
我确实没有看到您的 list 变量,也没有看到您的 readFile() 方法(如果它非常明显,我深表歉意'拉脸)。我目前的假设是这样的:当你读取文件时,也许你没有清空目的地,它只是加载了额外的数据,导致加倍。
也许它是一个 class-level StringBuilder?请在您的问题中包含此部分:)
private StringBuilder list = null;
然后里面的文件读取方法:
public static void readFile()
{
// can empty with a new instance
list = new StringBuilder();
// or reset the length
list.setLength(0);
...
// perform reading, now with an empty StringBuilder
}
如果没有 ^ 这个 ^ 你可能会做类似于以下示例的事情:
list = new StringBuilder("1").append(", ").append("2");
readFile(); // in method -> list.append("1").append(", ").append("2");
这将为 StringBuilder 提供输出:
["1", ", ", "2", "1", ", ", "2"]
长度为 6,而不是所需的:
["1", ", ", "2"]
长度为 3。我可以看出这是造成精确的重复计数的原因。
所以基本上我尝试从其他 class java 调用 return 数组的方法,它工作得很好,除了它是数组的大小或长度的两倍,是原始数组的两倍。 这是我的 return 数组和长度的代码。
public static double [] get_days(){
//extracted days from table into array
readFile();
double[] data = new double[list.size()];
System.out.println(list.size());
Integer[] daysArray = list.stream().map(Product::getDay)
.toArray(Integer[]::new);
for(int i = 0; i < daysArray.length; i++){
data[i] = Double.valueOf(daysArray[i]) ;
}
System.out.println("Array Size (Supposed to have 230 Data only) "+ data.length);
return data;
}
这是我调用另一个方法的方法 class
public class order_Picking extends AbstractProblem{
get_Product test = new get_Product();
public order_Picking(){
super(161,1,1);
}
public double [] var = new double[numberOfVariables];
public double [] Days = test.get_days();
@Override
public void evaluate (Solution solution){
System.out.println(Days.length);
//Jumlah produk pada batch ke-i pada picking list ke-i pada lokasi yang ke-i
for(int i=0; i< var.length;i++){
var[i]= EncodingUtils.getInt(solution.getVariable(i));
}
//jumlah ketersedian produk
int k1 = 100;
int k2 = 250;
int k3 = 150;
//Picking list-1
double [] pl1 = new double[3] ;
int p1 =100;
pl1[0]= p1;
int p2 = 20;
pl1[1]= p2;
int p3 = 40;
pl1[2]= p3;
int totalpl1 = p1+p2+p3;
//picking list-2
double [] pl2 = new double[3] ;
int p4 = 10;
pl2[0]= p4;
int p5 = 20;
pl2[1]= p5;
int p6 = 15;
pl2[2]= p6;
int totalpl2 = p4+p5+p6;
// Fungsi Tujuan untuk minimasi jarak
double f1 = distance(var) ;
double c1 = 0;
double c2 = 0;
for (int i = 0 ; i < var.length;i++){
c1 = (var[i]+var[i]*var[i])-totalpl1 ;
}
for (int i = 0 ; i < var.length;i++){
c2 = (var[i]+var[i]*var[i])-totalpl2 ;
}
//constraint picking list-1
//constraint picking list-2
solution.setObjective(0, f1);
solution.setConstraint(0,c1 == 0.0 ? 0.0 : c1);
solution.setConstraint(0,c2 == 0.0 ? 0.0 : c1);
}
@Override
public Solution newSolution() {
Solution solution = new Solution(161, 1, 1);
for (int i = 0 ; i<var.length;i++){
solution.setVariable(i,EncodingUtils.newBinaryInt(0,1));
}
return solution;
}
public static void main(String[] args) {
order_Picking value = new order_Picking();
NondominatedPopulation result = new Executor()
.withAlgorithm("GA")
.withProblemClass(order_Picking.class)
.withProperty("Populationsize",100)
.withProperty("sbx.rate",0.2)
.withProperty("pm",0.5)
.withMaxEvaluations(10000)
.run();
for (Solution solution : result) {
if (solution.violatesConstraints()) {
System.out.println("Solution for index 0 : " + +solution.getObjective(0));
for (int i = 0; i < value.var.length; i++){
System.out.println("Solusi variabel ke-"+i+" adalah "+ solution.getVariable(i));
}
}
}
}
public double distance (double [] x){
double a = 0;
for (int i = 0; i < x.length ; i++){
a += x[i];
}
return a;
}
}
该方法没有任何问题,但是当我在 public static void main 之外的其他 class 上调用它时,它似乎 运行 是 return 的两倍] 数组的大小超过 230,我不明白为什么它变成 460 而它应该是 230 here is the result on console
我确实没有看到您的 list 变量,也没有看到您的 readFile() 方法(如果它非常明显,我深表歉意'拉脸)。我目前的假设是这样的:当你读取文件时,也许你没有清空目的地,它只是加载了额外的数据,导致加倍。
也许它是一个 class-level StringBuilder?请在您的问题中包含此部分:)
private StringBuilder list = null;
然后里面的文件读取方法:
public static void readFile()
{
// can empty with a new instance
list = new StringBuilder();
// or reset the length
list.setLength(0);
...
// perform reading, now with an empty StringBuilder
}
如果没有 ^ 这个 ^ 你可能会做类似于以下示例的事情:
list = new StringBuilder("1").append(", ").append("2");
readFile(); // in method -> list.append("1").append(", ").append("2");
这将为 StringBuilder 提供输出:
["1", ", ", "2", "1", ", ", "2"]
长度为 6,而不是所需的:
["1", ", ", "2"]
长度为 3。我可以看出这是造成精确的重复计数的原因。