ctypes数组不可调用

时间:2019-05-10 07:03:11

标签: python scipy ctypes scipy-optimize-minimize

我想使用scipy.optimize.minimize函数。该函数包含来自DLL的命令,该命令需要ctypes数组。目标是改变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

1 个答案:

答案 0 :(得分: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),第1 st 自变量应为可调用(在您的情况下为函数)。
但是,您是在传递函数时自己调用函数,因此传递的是函数返回值

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

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