-4
$\begingroup$

I'm working on an optimization model in python with the pyomo library. However I'm getting an error message in python that I cannot seem to understand. The code and error message is below. My code is

from pyomo.environ import *
from pyomo.opt import SolverFactory
import json
model = ConcreteModel()
with open('C:/Users/cindy/python/Pyomo/ADMM_Distributed_Pyomo/input_data.json') as f:
    par = json.load(f)
    
# Declaro os parametros
model.P_PV         = Param(initialize = par['P_PV']) 
model.P_D          = Param(initialize = par['P_D'])
model.P_SE         = Param(initialize = par['P_SE'])
model.P_ESS_max    = Param(initialize = par['P_ESS_max'])
model.E_ESS_max    = Param(initialize = par['E_ESS_max'])
model.P_load_max   = Param(initialize = par['P_load_max'])
model.E0           = Param(initialize = par['E0'])
model.delta        = Param(initialize = par['delta'])
model.custo_venda  = Param(initialize = par['custo_venda'])
model.custo_compra = Param(initialize = par['custo_compra'])
model.custo_load   = Param(initialize = par['custo_load'])

model.rho              = Param(initialize = par['rho'])
model.epsilon          = Param(initialize = par['epsilon'])
model.iter             = Param(initialize = par['iter'])
model.tol_lambda       = Param(initialize = par['tol_lambda'])
model.tol_var          = Param(initialize = par['tol_var'])

model.P_SE_in_1_param  = Param(initialize = par['P_SE_in_1_param'])
model.P_SE_out_1_param = Param(initialize = par['P_SE_out_1_param'])
model.P_ESS_1_param    = Param(initialize = par['P_ESS_1_param'])    
model.E_ESS_1_param    = Param(initialize = par['E_ESS_1_param'])
model.P_load_1_param   = Param(initialize = par['P_load_1_param'])
model.P_SE_in_2_param  = Param(initialize = par['P_SE_in_2_param'])
model.P_SE_out_2_param = Param(initialize = par['P_SE_out_2_param'])
model.P_ESS_2_param    = Param(initialize = par['P_ESS_2_param'])
model.E_ESS_2_param    = Param(initialize = par['E_ESS_2_param'])
model.P_load_2_param   = Param(initialize = par['P_load_2_param'])
model.P_SE_in_3_param  = Param(initialize = par['P_SE_in_3_param'])
model.P_SE_out_3_param = Param(initialize = par['P_SE_out_3_param'])
model.P_ESS_3_param    = Param(initialize = par['P_ESS_3_param'])
model.E_ESS_3_param    = Param(initialize = par['E_ESS_3_param'])
model.P_load_3_param   = Param(initialize = par['P_load_3_param'])

model.lambda_P_SE_in_a      = Param(initialize = par['lambda_P_SE_in_a'])   
model.lambda_P_SE_out_a     = Param(initialize = par['lambda_P_SE_out_a'])  
model.lambda_P_ESS_a        = Param(initialize = par['lambda_P_ESS_a']) 
model.lambda_E_ESS_a        = Param(initialize = par['lambda_E_ESS_a'])     
model.lambda_P_load_a       = Param(initialize = par['lambda_P_load_a'])    
model.lambda_P_SE_in_b      = Param(initialize = par['lambda_P_SE_in_b'])
model.lambda_P_SE_out_b     = Param(initialize = par['lambda_P_SE_out_b'])
model.lambda_P_ESS_b        = Param(initialize = par['lambda_P_ESS_b']) 
model.lambda_E_ESS_b        = Param(initialize = par['lambda_E_ESS_b']) 
model.lambda_P_load_b       = Param(initialize = par['lambda_P_load_b']) 
model.lambda_P_SE_in_a_ant  = Param(initialize = par['lambda_P_SE_in_a_ant'])   
model.lambda_P_SE_out_a_ant = Param(initialize = par['lambda_P_SE_out_a_ant'])  
model.lambda_P_ESS_a_ant    = Param(initialize = par['lambda_P_ESS_a_ant']) 
model.lambda_E_ESS_a_ant    = Param(initialize = par['lambda_E_ESS_a_ant']) 
model.lambda_P_load_a_ant   = Param(initialize = par['lambda_P_load_a_ant'])    
model.lambda_P_SE_in_b_ant  = Param(initialize = par['lambda_P_SE_in_b_ant'])   
model.lambda_P_SE_out_b_ant = Param(initialize = par['lambda_P_SE_out_b_ant'])  
model.lambda_P_ESS_b_ant    = Param(initialize = par['lambda_P_ESS_b_ant']) 
model.lambda_E_ESS_b_ant    = Param(initialize = par['lambda_E_ESS_b_ant']) 
model.lambda_P_load_b_ant   = Param(initialize = par['lambda_P_load_b_ant'])    


#Declaracao das variaveis 
model.P_SE_in_1    = Var(bounds = (0,None),within=NonNegativeReals) #Potência importada 1
model.P_SE_out_1   = Var(bounds = (0,None),within=NonNegativeReals) #Potência vendida 1
model.P_ESS_1      = Var(bounds = (None,None)) #Potência do BESS 1
model.E_ESS_1      = Var(bounds = (0,None),within=NonNegativeReals) #Energia do BESS 1
model.P_load_1     = Var(bounds = (0,None),within=NonNegativeReals) #Potência da carga controlada 1
model.P_SE_in_2    = Var(bounds = (0,None),within=NonNegativeReals) #Potência importada 2
model.P_SE_out_2   = Var(bounds = (0,None),within=NonNegativeReals)#Potência vendida 2
model.P_ESS_2      = Var(bounds = (None,None))#Potência do BESS 2
model.E_ESS_2      = Var(bounds = (0,None),within=NonNegativeReals)#Energia do BESS 2
model.P_load_2     = Var(bounds = (0,None),within=NonNegativeReals)#Potência da carga controlada 2
model.P_SE_in_3    = Var(bounds = (0,None),within=NonNegativeReals)#Potência importada 3
model.P_SE_out_3   = Var(bounds = (0,None),within=NonNegativeReals)#Potência vendida 3
model.P_ESS_3      = Var(bounds = (None,None))#Potência do BESS 3
model.E_ESS_3      = Var(bounds = (0,None),within=NonNegativeReals)#Energia do BESS 3
model.P_load_3     = Var(bounds = (0,None),within=NonNegativeReals)#Potência da carga controlada 3

#Declaro as funçoes objetivo

#Declaro a FO1
model.obj1 = Objective(expr = model.delta * model.custo_compra * model.P_SE_in_1 
- model.delta * model.custo_venda * model.P_SE_out_1 + model.delta * model.custo_load*(model.P_load_max - model.P_load_3_param) 
+ model.lambda_P_SE_in_a*(model.P_SE_in_1 - model.P_SE_in_2_param) + model.rho/2*(model.P_SE_in_1 - model.P_SE_in_2_param)**2
+ model.lambda_P_SE_out_a*(model.P_SE_out_1 - model.P_SE_out_2_param) + model.rho/2*(model.P_SE_out_1 - model.P_SE_out_2_param)**2 
+ model.lambda_P_ESS_a*(model.P_ESS_1 - model.P_ESS_2_param) + model.rho/2*(model.P_ESS_1 - model.P_ESS_2_param)**2 
+ model.lambda_E_ESS_a*(model.E_ESS_1 - model.E_ESS_2_param) + model.rho/2*(model.E_ESS_1 - model.E_ESS_2_param)**2 
+ model.lambda_P_load_a*(model.P_load_1 - model.P_load_2_param) + model.rho/2*(model.P_load_1 - model.P_load_2_param)**2 
+ model.lambda_P_SE_in_b*(model.P_SE_in_1 - model.P_SE_in_3_param) + model.rho/2*(model.P_SE_in_1 - model.P_SE_in_3_param)**2 
+ model.lambda_P_SE_out_b*(model.P_SE_out_1 - model.P_SE_out_3_param) + model.rho/2*(model.P_SE_out_1 - model.P_SE_out_3_param)**2 
+ model.lambda_P_ESS_b*(model.P_ESS_1 - model.P_ESS_3_param) + model.rho/2*(model.P_ESS_1 - model.P_ESS_3_param)**2 
+ model.lambda_E_ESS_b*(model.E_ESS_1 - model.E_ESS_3_param) + model.rho/2*(model.E_ESS_1 - model.E_ESS_3_param)**2 
+ model.lambda_P_load_b*(model.P_load_1 - model.P_load_3_param) + model.rho/2*(model.P_load_1 - model.P_load_3_param)**2)

#Declaro as restricões da FO1
model.r1 = Constraint(expr = model.P_SE_in_1 + model.P_PV == model.P_ESS_1 + model.P_D + model.P_SE_out_1 + model.P_load_1)
model.r2 = Constraint(expr = model.P_SE_in_1 <= model.P_SE)
model.r3 = Constraint(expr = model.P_SE_out_1 <= model.P_SE)


#Declaro a FO2
model.obj2 = Objective(expr = model.lambda_P_SE_in_a*(model.P_SE_in_1_param - model.P_SE_in_2) 
+ model.rho/2*(model.P_SE_in_1_param - model.P_SE_in_2)**2 
+ model.lambda_P_SE_out_a*(model.P_SE_out_1_param - model.P_SE_out_2) + model.rho/2*(model.P_SE_out_1_param - model.P_SE_out_2)**2 
+ model.lambda_P_ESS_a*(model.P_ESS_1_param - model.P_ESS_2) + model.rho/2*(model.P_ESS_1_param - model.P_ESS_2)**2 
+ model.lambda_E_ESS_a*(model.E_ESS_1_param - model.E_ESS_2) + model.rho/2*(model.E_ESS_1_param - model.E_ESS_2)**2 
+ model.lambda_P_load_a*(model.P_load_1_param - model.P_load_2) + model.rho/2*(model.P_load_1_param - model.P_load_2)**2)

#Declaro as restricões da FO2
model.r5 = Constraint(expr = model.E_ESS_2 == model.E0 + model.delta * model.P_ESS_2)
model.r6 = Constraint(expr = -1*model.P_ESS_max <= model.P_ESS_2)
model.r7 = Constraint(expr = model.P_ESS_2 <= model.P_ESS_max)
model.r8 = Constraint(expr = model.E_ESS_2 <= model.E_ESS_max)


#Declaro a FO3
model.obj3 = Objective(expr = model.delta * model.custo_load *(model.P_load_max - model.P_load_3)
+ model.lambda_P_SE_in_b*(model.P_SE_in_1_param - model.P_SE_in_3) + model.rho/2*(model.P_SE_in_1_param - model.P_SE_in_3)**2 
+ model.lambda_P_SE_out_b*(model.P_SE_out_1_param - model.P_SE_out_3) +model.rho/2*(model.P_SE_out_1_param - model.P_SE_out_3)**2 
+ model.lambda_P_ESS_b*(model.P_ESS_1_param - model.P_ESS_3) + model.rho/2*(model.P_ESS_1_param - model.P_ESS_3)**2 
+ model.lambda_E_ESS_b*(model.E_ESS_1_param - model.E_ESS_3) + model.rho/2*(model.E_ESS_1_param - model.E_ESS_3)**2 
+ model.lambda_P_load_b*(model.P_load_1_param - model.P_load_3) + model.rho/2*(model.P_load_1_param - model.P_load_3)**2)

#Declaro as restricões da FO3
model.r4 = Constraint(expr = model.P_load_3 <= model.P_load_max)


#Problem solution 
while model.tol_lambda > 10e5 and model.tol_var > 10e5 :
    model.lambda_P_SE_in_a_ant          =   model.lambda_P_SE_in_a      
    model.lambda_P_SE_out_a_ant         =   model.lambda_P_SE_out_a     
    model.lambda_P_ESS_a_ant            =   model.lambda_P_ESS_a        
    model.lambda_E_ESS_a_ant            =   model.lambda_E_ESS_a        
    model.lambda_P_load_a_ant           =   model.lambda_P_load_a       
    model.lambda_P_SE_in_b_ant          =   model.lambda_P_SE_in_b      
    model.lambda_P_SE_out_b_ant         =   model.lambda_P_SE_out_b     
    model.lambda_P_ESS_b_ant            =   model.lambda_P_ESS_b    
    model.lambda_E_ESS_b_ant            =   model.lambda_E_ESS_b        
    model.lambda_P_load_b_ant           =   model.lambda_P_load_b
    
    #Solucao da FO1
    opt = SolverFactory('glpk')
    model.obj1.activate()
    model.obj2.deactivate()
    model.obj3.deactivate()
    results = opt.solve(model)
    model.P_SE_in_1_param     == model.P_SE_in_1
    model.P_SE_out_1_param    == model.P_SE_out_1
    model.P_ESS_1_param       == model.P_ESS_1
    model.E_ESS_1_param       == model.E_ESS_1  
    model.P_load_1_param      == model.P_load_1

    #Solucao da FO2
    '''
    opt = SolverFactory('glpk')
    model.obj1.deactivate()
    model.obj3.deactivate()
    results = opt.solve(model)
    '''
    model.P_SE_in_2_param     == model.P_SE_in_2
    model.P_SE_out_2_param    == model.P_SE_out_2
    model.P_ESS_2_param       == model.P_ESS_2
    model.E_ESS_2_param       == model.E_ESS_2
    model.P_load_2_param      == model.P_load_2

    #Solucao da FO3
    '''
    opt = SolverFactory('glpk')
    model.obj1.deactivate()
    model.obj2.deactivate()
    results = opt.solve(model)
    '''
    model.P_SE_in_3_param     == model.P_SE_in_3 
    model.P_SE_out_3_param    == model.P_SE_out_3
    model.P_ESS_3_param       == model.P_ESS_3
    model.E_ESS_3_param       == model.E_ESS_3
    model.P_load_3_param      == model.P_load_3
    

    #atualizacao variavel dual
    model.lambda_P_SE_in_a      = model.lambda_P_SE_in_a_ant    + model.rho * (model.P_SE_in_1_param - model.P_SE_in_2_param)
    model.lambda_P_SE_out_a     = model.lambda_P_SE_out_a_ant   + model.rho * (model.P_SE_out_1_param - model.P_SE_out_2_param)
    model.lambda_P_ESS_a        = model.lambda_P_ESS_a_ant      + model.rho * (model.P_ESS_1_param - model.P_ESS_2_param)   
    model.lambda_E_ESS_a        = model.lambda_E_ESS_a_ant      + model.rho * (model.E_ESS_1_param - model.E_ESS_2_param)   
    model.lambda_P_load_a       = model.lambda_P_load_a_ant     + model.rho * (model.P_load_1_param - model.P_load_2_param) 
    model.lambda_P_SE_in_b      = model.lambda_P_SE_in_b_ant    + model.rho * (model.P_SE_in_1_param - model.P_SE_in_3_param)   
    model.lambda_P_SE_out_b     = model.lambda_P_SE_out_b_ant   + model.rho * (model.P_SE_out_1_param - model.P_SE_out_3_param) 
    model.lambda_P_ESS_b        = model.lambda_P_ESS_b_ant      + model.rho * (model.P_ESS_1_param - model.P_ESS_3_param)   
    model.lambda_E_ESS_b        = model.lambda_E_ESS_b_ant      + model.rho * (model.E_ESS_1_param - model.E_ESS_3_param)   
    model.lambda_P_load_b       = model.lambda_P_load_b_ant     + model.rho * (model.P_load_1_param - model.P_load_3_param) 

    # Calcula os critérios de parada
    model.tol_var = abs(model.P_SE_in_1_param - model.P_SE_in_2_param) 
    + abs(model.P_SE_out_1_param - model.P_SE_out_2_param)
    + abs(model.P_ESS_1_param - model.P_ESS_2_param) 
    + abs(model.E_ESS_1_param - model.E_ESS_2_param) 
    + abs(model.P_load_1_param - model.P_load_2_param)
    + abs(model.P_SE_in_1_param - model.P_SE_in_3_param) 
    + abs(model.P_SE_out_1_param - model.P_SE_out_3_param)
    + abs(model.P_ESS_1_param - model.P_ESS_3_param) 
    + abs(model.E_ESS_1_param - model.E_ESS_3_param) 
    + abs(model.P_load_1_param - model.P_load_3_param)
    
    model.tol_lambda = abs(model.lambda_P_SE_in_a - model.lambda_P_SE_in_a_ant) 
    + abs(model.lambda_P_SE_out_a - model.lambda_P_SE_out_a_ant) 
    + abs(model.lambda_P_ESS_a - model.lambda_P_ESS_a_ant) 
    + abs(model.lambda_E_ESS_a - model.lambda_E_ESS_a_ant) 
    + abs(model.lambda_P_load_a - model.lambda_P_load_a_ant)
    + abs(model.lambda_P_SE_in_b - model.lambda_P_SE_in_b_ant) 
    + abs(model.lambda_P_SE_out_b   - model.lambda_P_SE_out_b_ant) 
    + abs(model.lambda_P_ESS_b - model.lambda_P_ESS_b_ant) 
    + abs(model.lambda_E_ESS_b - model.lambda_E_ESS_b_ant) 
    + abs(model.lambda_P_load_b - model.lambda_P_load_b_ant)

    iter = iter + 1,

The error is

ERROR: evaluating object as numeric value: P_SE_in_1
        (object: <class 'pyomo.core.base.var.ScalarVar'>)
    No value for uninitialized NumericValue object P_SE_in_1
Traceback (most recent call last):
  File "C:\Users\cindy\python\Pyomo\ADMM_Distributed_Pyomo\ADMM_Distributed_Pyomo.py", line 218, in <module>
    FO1 = model.obj1.expr()
  File "pyomo\core\expr\numeric_expr.pyx", line 218, in pyomo.core.expr.numeric_expr.ExpressionBase.__call__
  File "C:\Users\cindy\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pyomo\core\expr\visitor.py", line 1045, in evaluate_expression
    return visitor.dfs_postorder_stack(exp)
  File "C:\Users\cindy\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pyomo\core\expr\visitor.py", line 572, in dfs_postorder_stack
    flag, value = self.visiting_potential_leaf(_sub)
  File "C:\Users\cindy\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pyomo\core\expr\visitor.py", line 953, in visiting_potential_leaf
    return True, value(node, exception=self.exception)
  File "pyomo\core\expr\numvalue.pyx", line 156, in pyomo.core.expr.numvalue.value
  File "pyomo\core\expr\numvalue.pyx", line 143, in pyomo.core.expr.numvalue.value
ValueError: No value for uninitialized NumericValue object P_SE_in_1
PS C:\Users\cindy\python\Pyomo\ADMM_Distributed_Pyomo> 
$\endgroup$
1
  • 1
    $\begingroup$ Welcome to OR.SE! As mentioned in the answer by Oguz, please edit your question and add the error you're facing. Also, it helps if you describe your problem too rather than just sharing the code. $\endgroup$
    – EhsanK
    Nov 25 at 2:11
2
$\begingroup$

I can't see the error in your question. First of all, please include the error message as well. Second, if you try to solve a nonlinear (from the tags that you mention) glpk is not a good idea. You may use ipopt if all variables are continuous. Third (again from the tags), you don't need to define bound on your variables if use whitin and you don't have an upper bound.

$\endgroup$
1
  • $\begingroup$ Hi, thank you for the answer. I edited my question added the error. I will see the rest of your comments on my code. $\endgroup$ Nov 25 at 11:51
1
$\begingroup$

First googling your Error brought me to this stackoverflow question: Pyomo: Error No value for uninitialized NumericValue object Pyomo

Suspected answer there was that the problem couldn't be solved.That is something to look at. If you suspect that the model should be solvable with the data you have, you should investigate if all your constraints are correctly implemented. And if @Oguz Toragay is right and you should use another solver, it might very well be that the used solver cannot solve this problem.

I also looked over your code. It'd be a bit quicker if your comments in your code would be English. An interested reader might grasp more quickly what you intend to do in each step. Nonetheless the error message points us to the model variable P_SE_in_1. If you expect your model to solve and expect P_SE_in_1 to have a value after solving it, you'll probably run into errors if the model actually didn't solve. A good rule of thumb is to always check if your solver got a solution and only then access variable values.

Moreover there are some lines of code that don't practically do anything:

# Solucao da FO1
...
results = opt.solve(model)
model.P_SE_in_1_param == model.P_SE_in_1
model.P_SE_out_1_param == model.P_SE_out_1
model.P_ESS_1_param == model.P_ESS_1
model.E_ESS_1_param == model.E_ESS_1
model.P_load_1_param == model.P_load_1

The last 4 lines in this block are just boolean statements, which are not utilized. They default to True, False or result in an error if Python cannot compare these objects. But only the Error will do something in your code (it'll kill execution). I don't know the intention here, but you could remove these lines of code.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.