Topic
1 reply Latest Post - ‏2013-04-17T09:12:27Z by AlexFleischer
gemiel
gemiel
2 Posts
ACCEPTED ANSWER

Pinned topic Include functions from a separate file in a main block.

‏2013-04-16T14:31:45Z |

Hi

The "vellino" example shows a way to declare modeling objects in a separate .mod file.

I would like to do the same for functions in a main() block : my program uses a few functions to update datasets, relax unfeasible bounds, print messages, test solution quality and so on. The function declaration part in my main() block is quite long and it would be much easier reading the program if I could declare all those functions in a separate file.

I tried to do as for modeling objects by declaring them in a .mod file, but it failed. Apparently functions need to be declared within a main() block ?

Thanks

 

 

  • AlexFleischer
    AlexFleischer
    982 Posts
    ACCEPTED ANSWER

    Re: Include functions from a separate file in a main block.

    ‏2013-04-17T09:12:27Z  in response to gemiel

    Hi,

    let me give you an example of what you could do:

     

    In sub.mod

    execute
    {
     function A(x)
     {
      writeln("A",x);
     }   
     A(1);

     

    and then in your main model you do

    include "sub.mod";

    dvar float+        a in 0..1;

    maximize a;

    subject to{

    }

    main
    {
      A(2);
      thisOplModel.generate();
      cplex.solve();
    }