Solved

Get module variable by String

  • 3 June 2022
  • 3 replies
  • 65 views

Badge

Hi,

 

I have a String Parameter with index domain “i” called “ModPrefix” containing prefixes of modules within my project. Each of this modules has a variable with the name “Q”. Now, I would like to access these variables within the definition of a Node called “FacilityNode” like

NetOutflow = ModPrefix(i)::Q

However, this leads to the error

The identifier "Q" has not been declared, but it is declared in the modules [...].

where I replaced the names of the modules with “[...]”.

 

My idea of a workaround was an if-then-else-statement like

NetOutflow = 
if ( ModPrefix(i) = "prefix1" ) then
prefix1:Q
elseif ( ModPrefix(i) = "prefix2" ) then
prefix2:Q
else
0
endif;

in the case of two modules with prefixes “prefix1” and “prefix2”. However, I would still like to know if there is a way to circumvent it-then-else-statements, since they become ugly in the case of many modules.

 

 

icon

Best answer by Chris Kuip 3 June 2022, 19:24

View original

3 replies

Userlevel 4
Badge +7

Hi,

Hope this helps:

NetOutFlow =
ScalarValue(
StringToElement(
AllIdentifiers,
formatString("%s::Q", ModPrefix(i) ) ) )

 

Badge

Thank you @Chris Kuip !

 

I forgot to mention that this variable Q is time-dependent, i.e. it is associated with an index “t”. If I use

netoutflow =
ScalarValue(
StringToElement(
AllIdentifiers,
formatString( "%s::Q(t)", ModPrefix(i) ) ) )

then I do not get an error but it does not seem to work either, since the variables Q do not appear in the Math Program Inspector. Apparently, they are not added to the model at all. Do you know why?

 

Userlevel 4
Badge +7

Hi Sebastian,

 

You will need to create runtime identifiers, and the identifiers should be of type constraint.
Please check out:

  1. Language to create runtime identifiers - https://documentation.aimms.com/language-reference/advanced-language-components/model-structure-and-modules/runtime-libraries-and-the-model-edit-functions.html
  2. Procedure and functions that you can use - https://documentation.aimms.com/functionreference/model-handling/model-edit-functions/index.html
  3. An example that creates runtime identifiers of type constraint - https://how-to.aimms.com/Articles/131/131-Formulas-as-Data.html 

With kind regards,

Chris

Reply


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

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