简体   繁体   English

在cplex中使用for循环策略创建一个数组

[英]create an array with for loop strategy in cplex

I know I ask a lot of questions about reading CSV file and coding it in CPLEX. 我知道我问了很多有关读取CSV文件并在CPLEX中编码的问题。 But now my question is about a strategy. 但是现在我的问题是关于策略。 As I explained before I have a CSV file and I want to read it in a CPLEX. 如前所述,我有一个CSV文件,我想在CPLEX中读取它。 My question this time is that: I want to use a for loop to create an array in CPLEX from CSV file and I don't know how? 这次我的问题是:我想使用一个for循环从CSV文件在CPLEX中创建一个数组,但我不知道如何? This time I need to use for loop, not any other methods and I want to know is it possible or not: My code so far is: 这次,我需要使用for循环,而不是任何其他方法,并且我想知道是否可能:到目前为止,我的代码是:

range demand = 1..10;
int index[demand];
 int weight[demand];
execute {

  var f = new IloOplInputFile("weight.csv");
  var data = f.readline();
  while (!f.eof) {
   var data = f.readline().split(",");
   if (data.length == 2)
   for (var i=1; i<=demand.length; i++){
     index[i].add(Opl.intValue(data[0]));
     weight[i].add(Opl.intValue(data[1]));

  }   
    writeln(index);
    writeln(weight);
}
}

The problem is that I get [00..0] values for both index and weight. 问题是索引和权重都得到[00..0]值。 The weight file is attached as a picture: weight file 重量文件如图所示: 重量文件

I need I ask so many questions about this problem but I need this time that I use for loop and not define any tuple. 我需要问很多有关此问题的问题,但是这次我需要使用for循环而不定义任何元组。 I want to define each array separately. 我想分别定义每个数组。

There is a bug in your code. 您的代码中有一个错误。 If the code does not do what you want, it may help to add writeln(...) statements to trace what the code is actually doing. 如果代码没有执行您想要的操作,则可能会添加writeln(...)语句以跟踪代码实际在做什么。 Then you can probably figure out these things yourself. 然后,您可能可以自己弄清楚这些事情。

In your case there are a number of issues: 在您的情况下,存在许多问题:

  1. You are trying to use property demand.length but that property is undefined. 您正在尝试使用属性demand.length但是该属性未定义。 You can see this by adding writeln(demand.length) to your code. 您可以通过在代码中添加writeln(demand.length)来看到这一点。 So the condition i<=demand.length is never true and that loop is never executed. 因此条件i<=demand.length永远不会为真,并且永远不会执行该循环。
  2. You execute the loop over i for each line in the CSV file although each line in the CSV gives you only one entry in the array. 尽管CSV中的每一行只给您数组中的一个条目,但是您仍对CSV文件中的每一行执行i循环。
  3. Even if your loop would have worked, you are trying to call function add() on a plain integer (each entry in the array is an integer). 即使您的循环可行,您仍尝试在一个普通整数上调用函数add()(数组中的每个条目都是一个整数)。 Integer numbers don't have such a property. 整数没有这种属性。 So your loop would have raised an error. 因此,您的循环将引发错误。

The correct code to read your arrays from CSV would be this: 从CSV读取数组的正确代码是这样的:

range demand = 1..10;
int index[demand];
int weight[demand];

execute {

  var f = new IloOplInputFile("weight.csv");
  var data = f.readline();
  var i = 1;
  while (!f.eof) {
    var data = f.readline().split(",");
    if (data.length == 2) {
      index[i] = Opl.intValue(data[0]);
      weight[i] = Opl.intValue(data[1]);
      i = i + 1;
    }
  }
  writeln(index);
  writeln(weight);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM