There is some functionality to do this with pyomo very easily. However, you will need to install an additional library that is needed. If you are working with conda you could do as follows:
conda install pyomo
conda install scipy numpy
conda install -c conda-forge pynumero_libraries
with that in place
import pyomo.environ as aml
from pyomo.contrib.pynumero.interfaces import PyomoNLP
model = aml.ConcreteModel()
model.x - ...
solver = aml.SolverFactory('ipopt')
solver.solve(model, tee=True)
nlp = PyomoNLP(model)
body = nlp.x_init() # returns numpy array with values of all variables
xl = nlp.xl() # returns numpy array with lower bounds
xu = nlp.xu() # return numpy array with upper bounds
var_order = nlp.variable_order() # list names of variables in the order they were parsed to the numpy arrays
Alternatively if you don't want to install pynumero you could loop through all your variables using component maps
model_vars = model.component_map(aml.Var)
for var in model_vars.values():
for k, v in var.items():
print(k, aml.value(v))