Gaussian Processes constraints in Egret+pyomo+rogp

I need to construct a vector x=[f(m.x), m.x] where m.x is a pyomo variable and f() is a function of a pyomo variable and other fixed parameters; this vector is in turn input to a gaussian process, which I am integrating in a pyomo model using rogp. How can I construct this vector?

Adding some context: I am using python and IPOPT as a numerical solver. The optimization model is constructed using Egret.

I was able to get the code running by accessing the variables value using the value() function, but that is not what I mean to do. The gaussian process inputs are not fixed parameters - they are actual variables of the optimization model. I tried constructing x using ComponentMap() as:

# Construct the input
    m = pe.ConcreteModel()
    X = ComponentMap()
    X[0] = m.a 
    X[1] = m.b
    X[2] = c 
    X[3] = d

# Create ROGP object    
    xvar = rogp.pyomo_to_np(m.X).T
        
# Get GP prediction
    gp = gp_models[0]
    mu = gp.predict(xvar)[0]

But I get the following error message:

ERROR: Rule failed when generating expression for Constraint gp_stability with
index 0: PyomoException: Cannot convert non-constant Pyomo expression (0  <=
pg['1']*pg['1'] + qg['1']*qg['1'] + va['1']*va['1'] + vm['1']*vm['1'] +
1.1974802576175083 - 2.0*(0.35031139850616455*pg['1'] +
0.013423570431768894*qg['1'] - 0.0011899953242391348*va['1'] +
1.036619782447815*vm['1'])) to bool. This error is usually caused by using a
Var, unit, or mutable Param in a Boolean context such as an "if" statement, or
when checking container membership or equality. For example,
        >>> m.x = Var()
        >>> if m.x >= 1:
        ...     pass
    and
        >>> m.y = Var()
        >>> if m.y in [m.x, m.y]:
        ...     pass
    would both cause this exception.

ERROR: Constructing component 'gp_stability' from data=None failed:
        PyomoException: Cannot convert non-constant Pyomo expression (0  <=
        pg['1']*pg['1'] + qg['1']*qg['1'] + va['1']*va['1'] + vm['1']*vm['1']
        + 1.1974802576175083 - 2.0*(0.35031139850616455*pg['1'] +
        0.013423570431768894*qg['1'] - 0.0011899953242391348*va['1'] +
        1.036619782447815*vm['1'])) to bool.
    This error is usually caused by using a Var, unit, or mutable Param in a
    Boolean context such as an "if" statement, or when checking container
    membership or equality. For example,
        >>> m.x = Var()
        >>> if m.x >= 1:
        ...     pass
    and
        >>> m.y = Var()
        >>> if m.y in [m.x, m.y]:
        ...     pass
    would both cause this exception.
(GP_env) (base) -@Vincenzos-MacBook-Pro GP %  cd /Users/-/Documents/GitHub/GP ; /usr/bin/env /Users/-/anaconda3/envs/GP_env/bin/python /Users/-/.vscode/extensions/ms-python.debugpy-2024.14.0-darwin-
arm64/bundled/libs/debugpy/adapter/../../debugpy/launcher 52854 -- /Users/-/Documents/GitHub/GP/Egret-main/script.py 
/Users/-/Documents/GitHub/GP/GPY_MIMO_RIVANNA.py:396: SyntaxWarning: tuple indices must be integers or slices, not tuple; perhaps you missed a comma?
  output = model(valid_x.reshape((int(n_interval**3)//2, sup-inf, 5)[0,:]))
Loading GP model 0...
Backend macosx is interactive backend. Turning interactive mode on.
xvar shape: (1, 4)
Expected input shape for GP: (800, 4)
xvar content: [[<pyomo.core.base.var.VarData object at 0x153496200>
  <pyomo.core.base.var.VarData object at 0x153496580>
  <pyomo.core.base.var.VarData object at 0x15359c440>
  <pyomo.core.base.var.VarData object at 0x15357aa50>]]
ERROR: Rule failed when generating expression for Constraint gp_stability with
index 0: PyomoException: Cannot convert non-constant Pyomo expression (0  <=
pg['1']*pg['1'] + qg['1']*qg['1'] + va['1']*va['1'] + vm['1']*vm['1'] +
1.1974802576175083 - 2.0*(0.35031139850616455*pg['1'] +
0.013423570431768894*qg['1'] - 0.0011899953242391348*va['1'] +
1.036619782447815*vm['1'])) to bool. This error is usually caused by using a
Var, unit, or mutable Param in a Boolean context such as an "if" statement, or
when checking container membership or equality. For example,
        >>> m.x = Var()
        >>> if m.x >= 1:
        ...     pass
    and
        >>> m.y = Var()
        >>> if m.y in [m.x, m.y]:
        ...     pass
    would both cause this exception.
ERROR: Constructing component 'gp_stability' from data=None failed:
        PyomoException: Cannot convert non-constant Pyomo expression (0  <=
        pg['1']*pg['1'] + qg['1']*qg['1'] + va['1']*va['1'] + vm['1']*vm['1']
        + 1.1974802576175083 - 2.0*(0.35031139850616455*pg['1'] +
        0.013423570431768894*qg['1'] - 0.0011899953242391348*va['1'] +
        1.036619782447815*vm['1'])) to bool.
    This error is usually caused by using a Var, unit, or mutable Param in a
    Boolean context such as an "if" statement, or when checking container
    membership or equality. For example,
        >>> m.x = Var()
        >>> if m.x >= 1:
        ...     pass
    and
        >>> m.y = Var()
        >>> if m.y in [m.x, m.y]:
        ...     pass
    would both cause this exception.

I am not sure how to debug this/the workaround needed. I am also curious to understand what the expression below means and where it comes from. It should be related to the gaussian process but I am not sure how to interpret it.

        (0  <=
        pg['1']*pg['1'] + qg['1']*qg['1'] + va['1']*va['1'] + vm['1']*vm['1']
        + 1.1974802576175083 - 2.0*(0.35031139850616455*pg['1'] +
        0.013423570431768894*qg['1'] - 0.0011899953242391348*va['1'] +
        1.036619782447815*vm['1']))

Upvotes: 0

Views: 19

Answers (0)

Related Questions