Search code examples
pythonspyderpyomo

Running pyomo examples in Spyder


I know that some examples from the Pyomo book can be run from Anaconda company prompt, eg. by the command “runef -m ReferenceModel.py” for the farmer example.

I would like to run the examples within the Spyder IDE. Spyder doesn’t recognise any of the code. For example, I get the following error message ‘from pyomo.core import *’ used; unable to detect undefined names

How can I run the examples within Spyder? I am not sure if adding a line pyomo solve my_model.ph my_data.dat —-solver=‘glpk’ at the end of the script would work


Solution

  • Assuming you've set up your abstract model, you can instantiate it with data using:

    data = DataPortal()
    data.load(filename="my_data.dat", model=my_model)
    

    Then you can solve in Spyder and present results with the following:

    from pyomo.opt import SolverFactory
    
    opt = pyomo.environ.SolverFactory('glpk')
    instance = model.create_instance(data)
    
    opt.solve(instance)
    instance.display()
    

    References:

    (1) https://www.osti.gov/servlets/purl/1376827

    (2) https://pyomo.readthedocs.io/en/stable/working_models.html