我已经在 OPL 中编码并运行了我的模型,我正在尝试在 JAVA 中运行代码并再次运行它。作为我的代码(在 OPL 中)的一部分,我定义了一个元组,如下所示:
int Y = asSet(1..7);
int k = asSet(1..42);
int G = asSet(1..2);
tuple SL {
int i;
int j;
float l;
}
{SL} SLs with i,j in Y=...; /* e.g. {<1,2,502>, <2,5,309>, <5,7,401>, <2,3,350>} */
Then, I have defined other arrays of:
int u[SLs][G]=...; /* e.g. u[<1,2,502>][1] = 50; u[<1,2,502>][2] = 83; u[<2,5,309>][1] = 75;*/
现在我想用Java编写它,我已经这样做了,但我不确定我是否正确。如果您能分享您的想法,我将不胜感激。
import java.io.*;
import java.util.*;
public class Model {
public static int Y = 7;
public static int K = 42;
public static int G = 3;
public static int R = 2;
public class SL {
public int i; /* how say i is in Y*/
public int j; /* how say j is in Y*/
public int l;
List<SL> sl = new ArrayList<SL>();
Object[] SL1 = Sl.toArray();
int [][] u = new int [sL.length][G];
}
public static void Solve() {
/* How to instantiate SL1 and u[<i,j,l> in SL1][g in G] here and printout SL1:*/
}
}
另一个类来运行solve()方法:
public class SolverMethod {
public static void main(String[] args) {
Model.Solve();
}
}
如果您帮助我修复并运行代码,我将不胜感激。
问候, 博奈
最佳答案
您可以制作Map,以便使用SL的元组或对象可以生成唯一的数字,该数字可以用作u var中的索引。说,
Map<SL,Integer> m;
int key=m.get(sl);
u[key][g]
要实例化 SL1,您需要创建 SL 的对象,因为 SL1 不是静态的。
SL sl=new SL();
sl.SL1 or sl.u
首先创建SL对象并指向它的变量或方法。
下面是我实现的代码。我做了一些改变。
import java.io.*;
import java.util.*;
public class Model {
public static int Y = 7;
public static int K = 42;
public static int G = 3;
public static int R = 2;
static Map<SL,Integer> m=new HashMap<>();
static List<SL> sL = new ArrayList<SL>();
static int[][] u;
static int index=0;
static public class SL {
public int i;
/* how say i is in Y*/
public int j;
/* how say j is in Y*/
public int l;
}
public static void Solve() {
/* How to instantiate SL1 and u[<i,j,l> in SL1][g in G] here and printout SL1:*/
for(int i=0;i<5;i++){
SL sl=new SL();
sl.i=i;sl.j=i+1;sl.l=i+2;
sL.add(sl);
m.put(sl, index++);
}
u=new int[m.size()][G];
for(SL s:sL){
for(int i=0;i<G;i++){
u[m.get(s)][i]=i+10;
}
}
for(SL s:sL){
for(int i=0;i<G;i++){
System.out.println(u[m.get(s)][i]);
}
}
}
public static void main(String[] arg){
Model.Solve();
}
}
这里我将 sL、m 和 u 设为静态,因为我们只需要它的单个实例。
关于java - Java 中的元组类似于 OPL 中的元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51814390/