gpt4 book ai didi

java - GOTO/继续 Java 动态编程

转载 作者:行者123 更新时间:2023-12-02 00:20:56 24 4
gpt4 key购买 nike

我有以下 Java 代码实现动态编程递归关系:

public double routeCost() throws Exception {
double cost = Double.MAX_VALUE;
for (int l=i; l<=j; l++) {
if (! (customers.get(l) instanceof VehicleCustomer) )
continue;
double value = F(l,j) + (customers.get(l).distanceFrom(depot));
if (value < cost)
cost = value;
}

return cost;
}

private double F(int l, int m) {

//=========================== FIRST CASE ===========================
if (l==i && m==i) {
//System.out.println(i+","+j+","+l+","+m);
return firstCase();
}

//=========================== SECOND CASE ===========================
if (l==i && (i<m && m<=j) ) {
//System.out.println(i+","+j+","+l+","+m);
//analyses the possibility of performing all the soubtours based at heicle customert_i
return secondCase(i,m);

}
//=========================== GENERAL CASE ===========================
else {
System.out.println(i+","+j+","+l+","+m);

assert (customers.get(l) instanceof VehicleCustomer);

assert ( (i<l && l<=j) && (l<=m && m<=j) );
return Math.min(thirdCaseFirstTerm(l,m), thirdCaseSecondTerm(l,m));
}

}

private double firstCase() {
mainRoute.add(depot);
mainRoute.add(customers.get(i));
return depot.distanceFrom(customers.get(i));
}

private double secondCase(int i,int m) {
double caseValue = Double.MAX_VALUE;
int k = i;
while (k<m) {
double totalDemand=0;
for (int u=k+1; ( (u<=m) && (totalDemand<=truckCapacity) ); u++)
totalDemand += customers.get(u).getDemand();

double cost = F(i,k) + thita(i,k+1,m);
if (cost <= caseValue)
caseValue = cost;

k++;
}
return caseValue;
}

private double thirdCaseFirstTerm(int l, int m) {
double caseValue = Double.MAX_VALUE;

int k = i;
while (k<m) {

double totalDemand=0;
for (int u=k+1; ( (u<=m) && (totalDemand<=truckCapacity) ); u++)
totalDemand += customers.get(u).getDemand();

double cost = F(l,k) + thita(l,k+1,m);
if (cost <= caseValue)
caseValue = cost;
k++;
}

return caseValue;
}

private double thirdCaseSecondTerm(int l,int m) {
double caseValue = Double.MAX_VALUE;

int k = i;

for (Customer cust : customers) {
int h = customers.indexOf(cust);
if ( (!(cust instanceof VehicleCustomer)) || (h >=l)) {
continue;
}

double totalDemand=0;
for (int u=k+2; ( (u<=m) && (totalDemand<=truckCapacity) ); u++)
totalDemand += customers.get(u).getDemand();

double cost = F(h,k) + customers.get(h).distanceFrom(customers.get(l)) + thita(l,k+2,m);
if (cost < caseValue)
caseValue = cost;
}

return caseValue;
}

方法F(int,int)是从方法routeCost()的for循环中调用的。我想找到一种方法来强制执行断言 assert (customers.get(l) instanceof VehicleCustomer);` 不是 true,我不想继续执行 return 语句,而是想从 RouteCost() 进入 for 循环以继续下一次迭代。但 F() 必须返回一个值!

我知道我想做的事情几乎违反了面向对象的所有规则,但我真的需要它。

最佳答案

您可以在 F() 中引发 Exception 并在 routeCost() 中捕获它。

这种方法比使用断言要好得多。它们在实践中很少使用,这是有充分理由的:异常更加灵活,更适合检测错误、无效输入等。

PS:当我说“很少使用”时,我的说法是基于这样一个事实:我在过去几年中看到了数十万行 Java 代码,并且很少遇到使用断言的代码.

关于java - GOTO/继续 Java 动态编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10980829/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com