Topic
IC4NOTICE: developerWorks Community will be offline May 29-30, 2015 while we upgrade to the latest version of IBM Connections. For more information, read our upgrade FAQ.
7 replies Latest Post - ‏2013-01-20T20:10:38Z by Robop
SystemAdmin
SystemAdmin
7929 Posts
ACCEPTED ANSWER

Pinned topic Read data from a text file

‏2012-10-16T17:19:37Z |
Hello everyone.

I would like to read binary data from a txt file. I am using Cplex optimization studio 12.2.

I suppose I would need some script?

Could someone help me with this?

The file is formated like this:

1000000010010010 0000100010010010 0000100010010010 0000100010010010 0000100010010010 0010000010010010 0010010000010010 0010010000010010

0010010000010010 0100000100100010 0100000100000010 0100000100000010 0010000000001000 0010000000000001 0010000000000001 0010000000000001

0000100010010010 0100000010010010 0000100010010010 0000100010010010 0000100010010010 0010010000010010 0010001000010010 0010010000010010

0010010000010010 0100000100000010 0100000100010010 0100000100000010 0010000000000001 0010000000000100 0010000000000001 0010000000000001

0000100010010010 0000100010010010 0000100010010010 0001000010010010 0000100010010010 0010010000010010 0010010000010010 0010010000010010

0010010000010010 0100000100000010 0100000100000010 0100000100000010 0010000000000001 0010000000000001 0010000000000001 0010000000000001
... and so on

Thank you!

Bruno
Updated on 2013-01-20T20:10:38Z at 2013-01-20T20:10:38Z by Robop
  • SystemAdmin
    SystemAdmin
    7929 Posts
    ACCEPTED ANSWER

    Re: Read data from a text file

    ‏2012-10-23T09:58:05Z  in response to SystemAdmin
    Which API to CPLEX are you using? Some programming API like C, C++, or Java? Or are you working with OPL inside the CPLEX Studio and want to use the text file as a data source?
    Tobias
    • Robop
      Robop
      8 Posts
      ACCEPTED ANSWER

      Re: Read data from a text file

      ‏2013-01-16T20:13:28Z  in response to SystemAdmin
      Hi, I have the same question.
      I am using the OPL inside the CPLEX Studio. I actually want to read each line of single txt file as a data input and resolve my model for it.
      I am trying to figure out how to do that and any help will be appreciated.
      • SystemAdmin
        SystemAdmin
        7929 Posts
        ACCEPTED ANSWER

        Re: Read data from a text file

        ‏2013-01-16T21:43:56Z  in response to Robop
        This is quite OPL specific. You may want to try to ask this question in OPL forum.
      • SystemAdmin
        SystemAdmin
        7929 Posts
        ACCEPTED ANSWER

        Re: Read data from a text file

        ‏2013-01-16T22:17:15Z  in response to Robop
        Actually, I found that it is not too difficult. Reading this chapter in the user manual and the reference documentation for IloOplModelDefinition I could come up with the following:
        1. Create a file Model.mod that contains the model definition. In my case it contains a scalar variable bound that will be changed in each iteration:
        
        
        
        float bound = ...; dvar float+ x;   minimize x; subject to 
        { x >= bound; 
        }
        

        2. Create a script file Loop.mod that repeatedly loads the model, sets the data for bound and solves the model:
        
        main 
        { var i; 
        
        for (i = 0; i < 5; ++i) 
        { var source = 
        
        new IloOplModelSource(
        "Model.mod"); var cplex = 
        
        new IloCplex(); var def = 
        
        new IloOplModelDefinition(source); var opl = 
        
        new IloOplModel(def, cplex); var data = 
        
        new IloOplDataElements(); data.bound=i; opl.addDataSource(data); opl.generate(); cplex.solve(); writeln(
        "Iteration " + i + 
        ": objective: " + cplex.getObjValue()); opl.end(); cplex.end(); data.end(); def.end(); source.end(); 
        } 
        }
        

        3. Run the Loop.mod file. This gives
        
        Iterator 0: objective: 0 Iterator 1: objective: 1 Iterator 2: objective: 2 Iterator 3: objective: 3 Iterator 4: objective: 4
        

        From here it should be easy to generalize that. In order to read data from a text file you can use IloOplInputFile.
        • Robop
          Robop
          8 Posts
          ACCEPTED ANSWER

          Re: Read data from a text file

          ‏2013-01-16T22:47:02Z  in response to SystemAdmin
          Thanks a lot for the answer!!!
          However with the IloOplInputFile I can only read the string, right?
          So if I have: int Dhttp://1..2http://1..3
          and want to load it from data.txt, which is:
          10 10 10
          11 11 11

          I have to read each line with IloOplInputFile, split it by " " and then call "parseInt"? So there is no easier way?
          • SystemAdmin
            SystemAdmin
            7929 Posts
            ACCEPTED ANSWER

            Re: Read data from a text file

            ‏2013-01-17T06:44:37Z  in response to Robop
            Sorry, I am not an OPL expert. Maybe try the OPL forum.
            What comes to mind is to create a temporary data file and use that as data source. Assume that in the model you have
            float+ D[1..2][1..3] = ...;
            

            and in the text file you have
            10 10 10
            11 11 11
            

            Then you could do something like this:
            // Open data file
            var input = new IloOplInputFile("...");
            // Create a temporary data file and write values for the D[] array
            var dataFile = new IloOplOutputFile("temp.dat");
            dataFile.writeln("D = [");
            dataFile.writeln("[" + input.readLine() + "]"); // Line "10 10 10"
            dataFile.writeln("[" + input.readLine() + "]"); // Line "11 11 11"
            dataFile.writeln("];");
            dataFile.close();
            // Register the temporary data file with the model.
            var data = new IloOplDataSource("temp.dat");
            opl.addDataSource(data);
            
            Updated on 2014-03-24T22:41:42Z at 2014-03-24T22:41:42Z by iron-man
            • Robop
              Robop
              8 Posts
              ACCEPTED ANSWER

              Re: Read data from a text file

              ‏2013-01-20T20:10:38Z  in response to SystemAdmin
              DanielJunglas,

              Thank you a lot for the answer and an advice!
              I finally decided to use an excel file as input data file, because I have several 3-dimensional arrays. This link was helpful http://www-01.ibm.com/support/docview.wss?rs=0&context=SSCMS55&uid=swg21401340&loc=en_US&cs=utf-8&cc=us&lang=all

              But the thing with writing *.txt file to *.dat file works as well! So thank you again.