1

I solve a model and get an answer that I called it "sol" i want to have set 'i' such that i={1,...,sol}.

Instead of changing the . I wrote set as below, but it didn't work.

In other words I need my set changes depending on the value of sol.

My try:

 scalar sol;
 *after solving my model sol=objFun.val
 set i /1*sol/;

Is there any way to use scalar in set defenition?

Martin Wickman
  • 19,662
  • 12
  • 82
  • 106
Richard
  • 177
  • 1
  • 9
  • i tried it https://support.gams.com/gams:a_scalar_drives_the_length_of_a_set but i got Error 198!! why? – Richard Dec 06 '18 at 12:13
  • How does your code look like producing error 198? I just tried the code snippets in the linked you mentioned, and none gave that error to me. – Lutz Dec 06 '18 at 19:44
  • Hi @lutz , in the line myset(univ)$(ord(univ) <= dim) = yes; I had error in the (order(univ))<=dim). But when I changed it into (univ.val<=dim) , I hadn't any error. Do you know where my wrong is? – Richard Dec 07 '18 at 05:39
  • 1
    Ok, it seems as if your set "univ" is not ordered (that is, what error 198 is about). Here is some info about this and how you could potentially avoid that problem: https://www.gams.com/latest/docs/UG_OrderedSets.html#UG_OrderedSets_OrderedAndUnorderedSets – Lutz Dec 07 '18 at 08:29
  • @lutz thanks a lot for your link – Richard Dec 07 '18 at 09:40

1 Answers1

3

Two options I can propose as I do not know what you intend to use the set i for:

  1. I do not think it is absolutely necessary to have:

    set i /1*sol/;
    

    Rather, have:

    set i /1*UB/;
    

    where UB is a value >> sol, then define a dynamic set of i, say j, such that:

    j(i) = no;
    j(i)$(ord(i) le sol) = yes;
    
  2. if you are using the set i in a different model/solve, write out the new model/solve in a different .gms file and using put command to write out your new set depending on sol in a text file:

    put new_set /'new_set.txt'/;
    put new_set;
    put 'set j /1*' sol.l:14:4 '/' /;
    

    @Richard This creates a new file called 'new_set.txt' and then writes the exact words:

       "set j /1*  12345423.2345/;"
    

    to the file if sol.l = 12345423.234486754, which is actually the definition of a new set.
    'sol' is the objective function variable. If sol is a scalar you assign the objective function value to as you defined, remove the '.l'.
    '14' refers to how many digits you preassign to 'sol' when using the put utility it must be defined before hand.
    '4' is the number of decimal places for 'sol'.

    For further reference: https://www.gams.com/latest/docs/UG_Put.html

    In your new .gms file, include 'new_set.txt', and in the original file use the 'execute' command to call gams.exe to solve the new .gms file.

These are just my thoughts. Worth trying if you have been pushed to a wall without results. Hope they work out for you.

EJay
  • 159
  • 4
  • Hi@EJay , Thanks for your answer, I have not used the put command yet. can You explain a little , What does ' set/ 1 * 'sol.l: 14: 4' / '/;' mean? (I did not understand where the numbers 14 and 4 came from) Or introduce a link to read more about it? thanks again – Richard Dec 12 '18 at 06:36