Returning results of a Pyomo optimisation from a function

786 views Asked by At

I have set up a problem with Pyomo that optimises the control strategy of a CHP unit, which is laid out (very roughly) in the following way:

class Problem
    def OptiControl
        <Pyomo Concrete model formulation>
        return (model.obj.value())

The problem class is there because there are several control strategies methods that I'm investigating, and so I call these by using (for example) b = problem1.OptiControl().

The problem is that if I try to return the value of the objective my script gets stuck and when I Ctrl+C out of it I get (' Signal', 2, 'recieved, but no process queued'). Also if I write model.write() the script ends normally but nothing is displayed in IPython. Trying to print model.obj.value() also doesn't work.

I assume this has something to do with the fact that I'm calling Pyomo in a function because the model worked succesfully before, but I don't know how to get round this.

EDIT: writing the values to a file also doesn't work. If it helps, this is the excerpt of my code where I solve the model:

        opt = SolverFactory("glpk") # Choose solver
        solution = opt.solve(model) # Solve model

        model.write()

        with open('ffs.txt','w') as f:
            model.obj.expr()
            for t in model.P:
                f.write(model.f["CHP",t].value)
1

There are 1 answers

2
Sebastian On

I ended up working around this problem by re-writing my model as an AbstractModel, writing my data to a .dat file and reading that (clunky, but I need results now so it had to be done). For some reason it works now and does everything I expect it would do.

It also turned out my problem was infeasible which might have added to my problems, but at least with this method I could use results.write() and I was able to find that out which wasn't the case before.