Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access named variables and params #52

Closed
ericman314 opened this issue Jan 9, 2019 · 3 comments
Closed

Access named variables and params #52

ericman314 opened this issue Jan 9, 2019 · 3 comments
Labels
question Further information is requested

Comments

@ericman314
Copy link

ericman314 commented Jan 9, 2019

Say I have the following, where I want to reuse a model in multiple places. Other than using the gekko model's built-in _variables attribute, is there a "right" way to access the variable x?

def getModel():
  m = GEKKO()
  m.Var(value=5, name='x')
  return m

m = getModel()
# How to access variable x?

Putting x in the global scope is not what I intend.

@abe-mart
Copy link
Contributor

abe-mart commented Jan 9, 2019 via email

@abe-mart abe-mart added the question Further information is requested label Jan 9, 2019
@ericman314
Copy link
Author

ericman314 commented Jan 10, 2019

Nice and clean. Thank you.

Still, I suppose having a method that returns a dictionary of variables, parameters, etc. wouldn't hurt, would it? It's possible you could have a collision with a built-in attribute of the model.

@APMonitor
Copy link
Member

APMonitor commented Mar 29, 2019

I like your idea to create a dictionary of variables such as:

def getModel():
    m = GEKKO()
    vars = {}
    vars['x'] = m.Var()
    vars['y'] = m.Param()
    return vars
print(vars)

There is checking in the GEKKO code to avoid collisions with built-in variable names. This isn't a problem, especially if you don't give your variables or parameters a non-default name. Feel free to open the issue again if you'd like more development work on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants