Skip to main content

Hi,


Here is the set that I have and is a subset of AllVariables with the index i_DistWeightVar. Note that the variables in this set have exactly the same indices and in the same order, (i.e., i_period, i_Terminal, i_Source, i_Product, i_Fleet, i_Zone, i_Customer).

Here is the parameter that I create that only differs with his first index than the variables in the set above.

I am hoping to be able to make assignments by using a function like StringToVariable given below:

Anything you can give me to use on the RHS of this assignment?

Best,

Gorkem

I am not sure if this is possible with just a simple assignment operation.

For sure it is possible in AIMMS, I have done it multiple times, but then I use the model edit functions in aimms: I write a procedure that creates a dynamic procedure (that has all of the explicit assignment statements) and then executes this dynamic procedure.


Hello ​@gorkem 

 

I made an example to show what could be your approach. You can make it a lot more generic depending on your needs, but I think it illustrate the idea from ​@gdiepen (which I agree is the way to do this).

The setup is as follows:

 

Section Section_1 {
Parameter param {
IndexDomain: (iset1,iset2);
Definition: uniform(0,1);
}
}


DeclarationSection __unnamed {
Set set1 {
Index: iset1;
Definition: {
ElementRange(0,100,fill:0);
}
}
Set set2 {
Index: iset2;
Definition: {
ElementRange(0,100,fill:0);
}
}
Set subset1 {
SubsetOf: AllParameters;
Index: isubset1;
Definition: AllParameters * Section_1;
}
Parameter param2 {
IndexDomain: (isubset1,iset1,iset2);
}
}

Hence, we want to copy the contents of param to param2, that has an index (isubset1) that is related to identifiers.

I build the following procedure to make this copy:

! we need to check if the runtime already exists and delete it if so
_ep_library := StringToElement(AllIdentifiers,"CopyVariable",0);
if _ep_library then me::Delete(_ep_library); endif;

_ep_library := me::CreateLibrary("CopyVariable", "cv");
_ep_procedure := me::Create("prCopyVariable",
newType : 'procedure',
parentId : _ep_library);

for isubset1 do
_sp_procedureBody
:= _sp_procedureBody + FormatString("param2('%e', iset1, iset2) := %e(iset1, iset2);\n",
isubset1,isubset1);
endfor;

me::SetAttribute(
runtimeId : _ep_procedure,
attr : 'Body',
txt : _sp_procedureBody);
me::Compile(_ep_library);
apply(_ep_procedure);

Supporting this are local identifiers:

DeclarationSection __unnamed {
ElementParameter _ep_library {
Range: AllIdentifiers;
}
ElementParameter _ep_procedure {
Range: AllSymbols;
Default: 'dummy';
}
StringParameter _sp_procedureBody;
}

And I had to add a dummy procedure that does nothing in order to use apply:

Procedure dummy;

This is what my explorer looks like:

 

My procedure uses the AIMMS model edit functions to create a procedure dynamically that will have a body based on the assignment of all elements from subset1 to param2.

You can even see the generated code after you run it:

Which is what I imagine you want.

If I added more element to isubset1, it would also become new lines in the procedure, like this:

And after running and applying, I do get the param2 with all the different values from all the parameters.

I attached the ams file (as a zip).

Hope it helps!


Reply


Didn't find what you were looking for? Try searching on our documentation pages:

AIMMS Developer & PRO | AIMMS How-To | AIMMS SC Navigator