3

when I run the GEKKO optimization, I got a 'FileNotFoundError', please let me know how to handle it. Is there any problem in my code? Y is Binary Integer Decision Variable.

#initialize gekko
model = GEKKO(remote=False)
#APOPT is an Mixed Integer Nonlinear Problem solver
model.options.SOLVER = 1
model.time
#optional solver settings with APOPT
model.solver_options = ['minlp_maximum_iterations 500', \
                    # minlp iterations with integer solution
                    'minlp_max_iter_with_int_sol 10', \
                    # treat minlp as nlp
                    'minlp_as_nlp 0', \
                    # nlp sub-problem max iterations
                    'nlp_maximum_iterations 50', \
                    # 1 = depth first, 2 = breadth first
                    'minlp_branch_method 1', \
                    # maximum deviation from whole number
                    'minlp_integer_tol 0.05', \
                    # covergence tolerance
                    'minlp_gap_tol 0.01']

#parameter
X = total_PV_set
k = len(X)
eq = model.Param(value=len(X))
eq1 = model.Param(value=1)

#Decision Variable
# N = model.Var(value=1, lb=1, ub=k, integer=True)
N = 3
Y = model.Array(model.Var, (N, k), lb=0, ub=1, integer=True)
V = model.Array(model.Var, (N, 1))
W = model.Array(model.Var, (N, 1))
vary = model.Array(model.Var, (N, 1))
covary = model.Array(model.Var, (N, 1))

#Constraints
for i in range(N):
    vary_buff = 0
    for j in range(k):
        vary_buff += model.Intermediate(variance(X[j]) * Y[i][j])
    model.Equation(vary[i] == vary_buff)
for i in range(N):
    covary_buff = 0
    for j in range(k):
        for e in range(k-1):
            if j < (e+1):
                covary_buff += model.Intermediate(2*covariance(X[j], X[e+1])*Y[i][j]*Y[i][e+1])
    model.Equation(covary[i] == covary_buff)
for i in range(N):
    model.Equation(V[i] == model.Intermediate(vary[i]+covary[i]))
for i in range(N):
    model.Equation(W[i] == model.Intermediate(model.sum(Y[i][:])))
model.Equation(model.sum(Y) == eq)
for i in range(k):
    model.Equation(model.sum(Y[:, i]) == eq1)


cc = model.Intermediate(model.sum([(W[i]*V[i]) for i in range(N)]))
model.Obj(cc/model.sum(W))

#minimize objective
# model.options.IMODE = 3
# model.options.MEAS_CHK = 0
model.solve()

#Print the results
print ('--- Results of the Optimization Problem ---')
print('Y: '+str(Y))
print('N: '+str(N))
print('V: '+str(V))
print('W: '+str(W))
print('Objective: '+str(model.options.objfcnval))

Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32 runfile('C:/Users/chldj/EOJIN/VPP_test.py', wdir='C:/Users/chldj/EOJIN') Backend TkAgg is interactive backend. Turning interactive mode on. C:/Users/chldj/EOJIN/VPP_test.py:91: DeprecationWarning: elementwise comparison failed; this will raise an error in the future. model.Equation(vary[i] == vary_buff) C:/Users/chldj/EOJIN/VPP_test.py:98: DeprecationWarning: elementwise comparison failed; this will raise an error in the future. model.Equation(covary[i] == covary_buff) C:/Users/chldj/EOJIN/VPP_test.py:100: DeprecationWarning: elementwise comparison failed; this will raise an error in the future. model.Equation(V[i] == model.Intermediate(vary[i]+covary[i])) C:/Users/chldj/EOJIN/VPP_test.py:102: DeprecationWarning: elementwise comparison failed; this will raise an error in the future. model.Equation(W[i] == model.Intermediate(model.sum(Y[i][:]))) ---------------------------------------------------------------- APMonitor, Version 0.9.2 APMonitor Optimization Suite ----------------------------------------------------------------

Error: Exception: Access Violation
At line 2391 of file custom_parse.f90
Traceback: not available, compile with -ftrace=frame or -ftrace=full
Error: 'results.json' not found. Check above for additional error details
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\pycharm\PyCharm Community Edition 2019.2.2\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "C:\pycharm\PyCharm Community Edition 2019.2.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/chldj/EOJIN/VPP_test.py", line 114, in <module>
    model.solve()
  File "C:\python\lib\site-packages\gekko\gekko.py", line 2145, in solve
    self.load_JSON()
  File "C:\python\lib\site-packages\gekko\gk_post_solve.py", line 13, in load_JSON
    f = open(os.path.join(self._path,'options.json'))
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\chldj\\AppData\\Local\\Temp\\tmpdgnw5ovqgk_model0\\options.json'

Is it means that the solution is infinity? I think 1st iteration's sum(W) will be a 0. so it can make objective function 'infinity'. how can I fix it?

choi
  • 61
  • 2
  • The error on the last line says it's `FileNotFoundError ... No such file or directory:` for `options.json` file. Seems like you miss some setup for your package / model / environment – Alexey S. Larionov Mar 12 '20 at 17:29
  • I wasn't able to replicate your error because `total_PV_set` is not defined. I replaced it with [1,2,3] and got the error that `NameError: name 'variance' is not defined`. You'll probably get an error for `covariance` as well. Those are not functions in Gekko. Gekko produces exact first and second derivatives for the solver so there are only certain functions that are available. You'll need to use Gekko functions as shown in the documentation. https://gekko.readthedocs.io/en/latest/model_methods.html – John Hedengren Mar 13 '20 at 04:56
  • Please include a minimal and reproducible example that allows us to help with the problem. For example, I would also recommend that you include statements such as `from gekko import GEKKO` at the top. – John Hedengren Mar 13 '20 at 04:58
  • so you mean that you need a certain function for variance and covariance? that is a customized function, if you need, I will edit on my post. – choi Mar 13 '20 at 05:44
  • I register a new question, with full code(including any functions and value). please check and help. – choi Mar 13 '20 at 06:05

1 Answers1

0

The functions variance and covariance are not in the Gekko library. You will need to remove these functions and instead use any of the Gekko library functions. Some Numpy functions are also permitted for matrix operations such as numpy.dot. You can use a combination of functions such as from Gekko and numpy.

from gekko import GEKKO
import numpy as np
m = GEKKO()
A = m.Array(m.Var,(4,3))
b = m.Array(m.Param,3,value=1)
x = np.dot(A,b)
[m.Minimize(x[i]**2) for i in range(4)]
m.solve(disp=False)
print(A)

Here is a list of Gekko functions:

  • abs(x) absolute value |x|
  • abs2(x) absolute value with MPCC
  • abs3(x) absolute value with binary variable for switch
  • acos(x) inverse cosine, cos^-1(x)
  • acosh(x) inverse hyperbolic cosine, cosh^-1(x)
  • Array(type,size) array of GEKKO objects
  • arx auto-regressive exogenous input (time series) model
  • asin(x) inverse sine, sin^-1(x)
  • asinh(x) inverse hyperbolic sine, sinh^-1(x)
  • atan(x) inverse tangent, tan^-1(x)
  • atanh(x) inverse hyperbolic tangent, tanh^-1(x)
  • bspline bspline for 2D data
  • cos(x) cosine
  • cspline cubic spline for 1D data
  • erf(x) error function
  • erfc(x) complementary error function
  • exp(x) e^x
  • if3(cond,x1,x2) switch between x1 (cond<0) and x2 (cond>=0)
  • log(x) log_e (x), natural log
  • log10(x) log_10 (x), log base 10
  • max2(x1,x2) maximum value with MPCC
  • max3(x1,x2) maximum value with binary variable for switch
  • min2(x1,x2) minimum value with MPCC
  • min3(x1,x2) minimum value with binary variable for switch
  • periodic periodic (initial=final) for dynamic problems
  • pwl piece-wise linear function
  • sign2(x) signum operator with MPCC
  • sign3(x) signum operator with binary variable for switch
  • sin(x) sine
  • sinh(x) hyperbolic sine
  • sqrt(x) square root
  • state_space continuous/discrete and dense/sparse state space
  • sum summation of elements in a list or numpy array
  • tan(x) tangent
  • tanh(x) hyperbolic tangent
  • vsum(x) vertical sum of a single variable in the data direction

Other functions like variance and covariance aren't in the function library.

John Hedengren
  • 12,068
  • 1
  • 21
  • 25