Using Gurobi expression on Pyomo model

61 Views Asked by At

I would like to create a decision variable that is a minimum from 3 other decision variables. My model is written in Pyomo and I use Gurobi solver. There is a min_ function provided by Gurobi https://www.gurobi.com/documentation/9.5/refman/py_min_.html and it can solve my issue. But I cannot use such a function directly on my Pyomo model or can I somehow add Gurobi expression on Pyomo model just before solving? I was trying to dump a model to mps, then load it with gurobi and add the constraints before solving. Do you know a better way?

I just add a few variables and constraints (based on the min_ expression at the end). This is how it looks like with the current approach (saving to .mps and then loading it, and then optimizing):

       with tempfile.TemporaryDirectory() as temp_dir:
            model_file_path = os.path.join(temp_dir, "model.mps")
            self.model.write(
                model_file_path,
                io_options={"symbolic_solver_labels": True}
            )

            from gurobipy import read

            model = read(model_file_path)

            for h in self.model.H.value_list:
                st_gen_h = model.addVar(
                    lb=0, vtype=GRB.CONTINUOUS, name=f'st_gen_h({h})')
                model.addConstr(
                    quicksum(
                        model.getVarByName(f'st_gen_z({h}_{z})')
                        for z in self.model.Z.value_list
                    ) == st_gen_h
                )
                model.addConstr(
                    st_gen_h
                    ==
                    min_(
                        model.getVarByName(f'st_p'),
                        model.getVarByName(f'st_soc({h})'),
                        model.getVarByName(f'gen_load_minus({h})')
                    )
                )

            model.optimize()