0

我想使用 scipy.optimize.minimize 函数。该函数包含来自需要 ctypes 数组的 DLL 的命令。目标是改变 ctypes 数组中的输入以优化也是 ctypes 数组的特定输出(参见下面的代码)。

import os
import ctypes
import tkinter as tk
from PIL import ImageTk
from tkinter import filedialog
import numpy as np
from scipy.optimize import minimize

dll = ctypes.cdll.LoadLibrary(library)

LoadModelDef = dll.addModelDef(model)

nrExperiments = 1
nrin = dll.getNumInputs(LoadModelDef)

PDBL2ARR = ctypes.c_double * nrin * nrExperiments
inputs = PDBL2ARR()
inputs_init = PDBL2ARR()


def evaluaterel(library,Model,InputArray):
    nrExp = len(InputArray)
    DBL2ARR = ctypes.c_double * nrExp
    outputs = DBL2ARR()
    for i in range(2,13):
        Name= outputName(Model,i)
        library.evalVBA(Model,InputArray,nrExp,i,outputs)
        for i in range(nrExp):
            Value = str(outputs[i])
#        text = label.cget("text") + '\n' + str(Name)+ ' ' + str(Value)
#        label.configure(text=text)
    return outputs

data = np.array([line.split()[-1] for line in open("DATA.txt")], dtype=np.float64)
for i in range(nrExperiments):
    for j in range(nrin):
        inputs_init[i][j]= 0

for i in range(nrExperiments):
    for j in range(0,nrin):
        inputs[i][j]=data[j]

solution=minimize(evaluaterel(dll,LoadModelDef,inputs),inputs_init,method='SLSQP')
print(solution)

  File "c:\app\python27\lib\site-packages\scipy\optimize\optimize.py", line 292, in function_wrapper
    return function(*(wrapper_args + args))

TypeError: 'c_double_Array_1' object is not callable
4

1 回答 1

0

根据[SciPy.Docs]:scipy.optimize.minimize ( fun, x0, args=(), method=None, jac=None, hess=None, hessp=None, bounds=None, constraints=(), tol= None, callback=None, options=None ) 第一个参数应该是可调用的(在你的情况下是函数)。
但是,您在传递函数时自己调用了函数,因此您传递了函数 return value

将您的代码(错误行)修改为:

solution = minimize(evaluaterel, inputs_init, args=(dll, LoadModelDef, inputs), method="SLSQP")
于 2019-05-10T08:42:45.387 回答