java - Java 中的元组类似于 OPL 中的元组

标签 java tuples opl

我已经在 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/

相关文章:

javascript - 在 cplex/opl 中初始化集合

cplex - 使用 MIP 接送车辆的车辆路线

c# - 如何使用 Tuple 返回 2 个或 3 个值?

java - 加载 NER 分类器时出错 - ZLIB 输入流意外结束

java - 管理集合映射的通用方法

java - 如何查找javac任务中的内存泄漏

python - 按不需要的字母顺序排列的元组

haskell - 为什么 GHC 元组的大小限制为 62?

java - 扫描程序在使用next()或nextFoo()之后跳过nextLine()吗?