Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Iterating Among Notepad Files

    Posted 11/07/11 01:17 PM

    Originally posted by: guvenc


    Hello people I dont know if it s right forum to ask this question but here is my problem. Thanks for your helps..
    I have a thread in OPL using Cplex forum about this issue but the forum is under maintenance now and I am a bit stuck with this problem for five days.
    In short I try to read data(problem instance) from a notepad file then solve a math model with the instance, give an output file and then read another input notepad file to solve again and again...
    Below I have an(uncompleted) main script in which I want to read data from different .txt files consequtevly. The notepad files are named like those examples.
    n20r1p1w1c1pr01.txt
    n20r1p1w1c1pr02.txt
    n50r1p1w1c1pr10.txt

    20
    1 80 85 5 60
    2 122 127 5 50
    ....
    19 189 198 9 40
    20 124 132 8 40
    Above is shown one of my notepad file.

    In main; I try to assign the information inside the files to r,d,w and c in each iteration. I'll use those arrays in my math model.
    I get an error when I write such lines in order to be able to itterate among notepad files.
    *// var h6=new Array();
    *// h6[0]='01';
    *// h6[1]='02';
    ....
    *// file.open("n"h1"r"h2"p"h3"w"h4"c"h5"pr"h6[0]".txt")//in each iteration I ll play with index of h
    Internal error: ASSERTION FAILED at ..\..\..\src\script\extras.cpp:849

    In this wy i expect to iterate among notepad files. h6[1]='02' h6[2]='03'
    main
    {
    // r,d,w,c will be used as parameters in mathematical model
    var r=new Array();
    var d=new Array();
    var w=new Array();
    var c=new Array();

    var index=1;
    var file= new IloOplInputFile();
    // HERE is Notepad File Names Logic
    var h1='20';//20 50 100 500
    var h2='1';
    var h3='1';
    var h4='1';//1 2 3
    var h5='1';//1 2 3 4
    var h6='01';//01 02 ... 10

    var i=1;
    while(i<=2)
    {
    file.open("n"h1"r"h2"p"h3"w"h4"c"h5"pr"h6".txt");//This works.
    //file.open("n20r1p1w1c1pr01.txt");
    var read;
    var tmp;
    if (file.isOpen)
    {
    writeln("f is open");
    while (!file.eof)
    {
    read=file.readline();//reads line by line
    writeln(read);
    tmp=read.split("\t");
    if(tmp[1]!=null)
    {
    rindex=tmp[1];
    dindex=tmp[2];
    windex=tmp[3];
    cindex=tmp[4];
    writeln(rindex,"-",dindex,"-",windex,"-",cindex);
    index=index+1;
    }
    }
    file.close();
    }
    else
    {
    writeln("f is closed");
    }
    if (file.isOpen)
    writeln("f is open");
    else
    writeln("f is closed");

    for(a=1;a<=20;a++)
    write(r[a]," ");

    h6='02';// I change here.
    i=i+1; // This is just to try if i can get second file and it works as expected
    }

    }
    Thanks a lot for your helps.
    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Iterating Among Notepad Files

    Posted 11/07/11 06:31 PM

    Originally posted by: SystemAdmin


    You could use something like the following to iterate through the various txt files:

    
    main 
    { var h1 = 
    
    new Array (
    "20",
    "40",
    "60"); var h2 = 
    "1"; var h3 = 
    "1"; var h4 = 
    
    new Array (
    "1", 
    "2", 
    "3"); var h5 = 
    
    new Array (
    "1", 
    "2", 
    "3"); var h6 = 
    
    new Array (
    "01",
    "02",
    "03"); var index=0; var file= 
    
    new IloOplInputFile(); 
    
    while(index<=2)
    { var fileName = 
    "n"+h1[index]+
    "r"+h2+
    "p"+h3+
    "w"+h4[index]+
    "c"+h5[index]+
    "pr"+h6[index]+
    ".txt"; writeln(
    "File to open: "+ fileName); file.open(fileName); 
    // file related operations to store the other arrays file.close(); index++; 
    } 
    }
    


    This assumes the presence of the following files in the same directory as that of your mod/prj file:

    n20r1p1w1c1pr01.txt
    n40r1p1w2c2pr02.txt
    n60r1p1w3c3pr03.txt

    Hope this helps.
    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Iterating Among Notepad Files

    Posted 11/08/11 05:35 AM

    Originally posted by: guvenc


    Thank you Abhishek, you are always helping me.
    Those little errors often bothers newbies untill they get experienced.
    At least; I hope this thread helps people like me :)
    #CPLEXOptimizers
    #DecisionOptimization