ctypes 数组不可调用

ctypes array is not callable

我想使用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

根据 [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),第 1st 参数应该是 callable(函数,在您的例子中)。
但是,您在传递函数时自己调用函数,因此您传递的是 函数 return 值.

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

solution = minimize(evaluaterel, inputs_init, args=(dll, LoadModelDef, inputs), method="SLSQP")