Pyomo: Access Solution From Python Code

后端 未结 3 1486
[愿得一人]
[愿得一人] 2020-12-29 14:58

I have a linear integer programme I want to solve. I installed solver glpk (thanks to this answer) and pyomo. I wrote code like this:

from pyomo.environ impo         


        
3条回答
  •  余生分开走
    2020-12-29 15:47

    I'm not sure if this is what you are looking for, but this is a way that I have some variables being printed in one of my scripts.

    from pyomo.environ import *
    from pyomo.opt import SolverFactory
    from pyomo.core import Var
    
    M = AbstractModel()
    opt = SolverFactory('glpk')
    
    # Vars, Params, Objective, Constraints....
    
    instance = M.create_instance('input.dat') # reading in a datafile
    results = opt.solve(instance, tee=True)
    results.write()
    instance.solutions.load_from(results)
    
    for v in instance.component_objects(Var, active=True):
        print ("Variable",v)
        varobject = getattr(instance, str(v))
        for index in varobject:
            print ("   ",index, varobject[index].value)
    

提交回复
热议问题