为什么我不能在python中用ctypes输入c函数。关于小溪!=空值

2024-05-29 07:50:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在python中使用c编写的函数。当我按照ctypes创建dll并将其输入python程序时,我发现有一些调试断言失败。比如“表情:流!=空PTR”。怎么会这样?我希望有人能解决这个问题!你知道吗

因为我用了15个黑盒函数来调用cec15问题,所以我只需要得到这些函数的结果。在c源文件中,我创建了一个fitness函数来返回cec15函数的值,因为cec15函数的返回值在参数中。你知道吗

关于c文件中的cec15函数: void cec15_test_func(double*x,double*f,int nx,int mx,int func_num)

x将包含需要求值的值,f将保存返回值,nx是问题的维度,这里可以是10和30;mx是要求值的值的数目,因此x的长度将是mx*nx,并且f必须大于mx*sizeof(double),func num定义要求值的函数,可以是1,2,3,。。。,15你知道吗

#test.py
import ctypes
import os
from ctypes import *


import random
import numpy as np
import math
import matplotlib.pyplot as plt

mydll=ctypes.windll.LoadLibrary("C:\\Users\\TEMP.IPS.005\\Desktop\\cec15\\Debug\\cec15.dll")

def main():
    p = [1.1,2.2]
    arr = (ctypes.c_double * len(p))(*p)
    D = ctypes.c_int(10)
    func_num = ctypes.c_int(1)
    mydll.fitness(arr, 10, 1)
    print(f)

if __name__ == '__main__':
    main()



//cec15__test_func.c
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#include <stdio.h>
#include <math.h>
#include <malloc.h>

#define NO_RECORDING
#include "cec15_test_func.h"

double __stdcall fitness(double a[], int D, int func_num)
{
    //  return Sphere(a);
    //double tem[] = { 0.0 };
    double *f;
    double temp[15];
    f = temp;
    cec15_test_func(a, f, D, 1, func_num);
    return *f;
};



void __stdcall cec15_test_func(double *x, double *f, int nx, int mx, int func_num)
{
    int cf_num,i,j;

    cf_num = cf_numbers[func_num];

    if (ini_flag==1)
    {
        if ((n_flag!=nx)||(func_flag!=func_num))
        {
            ini_flag=0;
        }
    }

    if (ini_flag==0)
    {
        FILE *fpt;
        char FileName[256];
        free(M);
        free(OShift);
        free(y);
        free(z);
        //free(x_bound);
        y=(double *)malloc(sizeof(double)  *  nx);
        z=(double *)malloc(sizeof(double)  *  nx);
        //x_bound=(double *)malloc(sizeof(double)  *  nx);
        //for (i=0; i<nx; i++)
            //x_bound[i]=100.0;

        if (!(nx==10||nx==30))
        {
            printf("\nError: Test functions are only defined for D=10,30.\n");
        }
        /* Load Matrix M*/
        sprintf(FileName, "input_data/M_%d_D%d.txt", func_num,nx);

        fpt = fopen(FileName,"r");
        if (fpt==NULL)
        {
            printf("\n Error: Cannot open input file for reading \n");
        }
        if (func_num<13)
        {
            M=(double*)malloc(nx*nx*sizeof(double));
            if (M==NULL)
                printf("\nError: there is insufficient memory available!\n");
            for (i=0; i<nx*nx; i++)
            {
                fscanf(fpt,"%lf",&M[i]);
            }
        }
        else
        {
            M=(double*)malloc(cf_num*nx*nx*sizeof(double));
            if (M==NULL)
                printf("\nError: there is insufficient memory available!\n");
            for (i=0; i<cf_num*nx*nx; i++)
            {
                fscanf(fpt,"%lf",&M[i]);
            }
        }
        fclose(fpt);

        /* Load shift_data */
        sprintf(FileName, "input_data/shift_data_%d_D%d.txt", func_num,nx);
        fpt = fopen(FileName,"r");
        if (fpt==NULL)
        {
            printf("\n Error: Cannot open input file for reading \n");
        }

        if (func_num<13)
        {
            OShift=(double *)malloc(nx*sizeof(double));
            if (OShift==NULL)
            printf("\nError: there is insufficient memory available!\n");
            for(i=0;i<nx;i++)
            {
                fscanf(fpt,"%lf",&OShift[i]);
            }
        }
        else
        {
            OShift=(double *)malloc(nx*cf_num*sizeof(double));
            if (OShift==NULL)
                printf("\nError: there is insufficient memory available!\n");
            for(i=0;i<cf_num-1;i++)
            {
                for (j=0;j<nx;j++)
                {
                    fscanf(fpt,"%lf",&OShift[i*nx+j]);
                }
            }
            for (j=0;j<nx;j++)
            {
                fscanf(fpt,"%lf",&OShift[(cf_num-1)*nx+j]);
            }

        }
        fclose(fpt);


        /* Load Shuffle_data */
        sprintf(FileName, "input_data/shuffle_data_%d_D%d.txt", func_num,nx);

        if (func_num>=10&&func_num<=12)
        {
            //sprintf(FileName, "../../../src/input_data/shuffle_data_%d_D%d.txt", func_num, nx);
            fpt = fopen(FileName,"r");
            if (fpt==NULL)
            {
                printf("\n Error: Cannot open input file for reading \n");
            }
            SS=(int *)malloc(nx*sizeof(int));
            if (SS==NULL)
                printf("\nError: there is insufficient memory available!\n");

            for(i=0;i<nx;i++)
            {
                fscanf(fpt,"%d",&SS[i]);
            }   
            fclose(fpt);
        }
        else if (func_num>=13)
        {
            //sprintf(FileName, "../../../src/input_data/shuffle_data_%d_D%d.txt", func_num, nx);
            fpt = fopen(FileName,"r");
            if (fpt==NULL)
            {
                printf("\n Error: Cannot open input file for reading \n");
            }
            SS=(int *)malloc(nx*cf_num*sizeof(int));
            if (SS==NULL)
                printf("\nError: there is insufficient memory available!\n");
            for(i=0;i<nx*cf_num;i++)
            {
                fscanf(fpt,"%d",&SS[i]);
            }
            fclose(fpt);
        }


        n_flag=nx;
        func_flag=func_num;
        ini_flag=1;
        //printf("Function has been initialized!\n");

    }


    for (i = 0; i < mx; i++)
    {

        switch(func_num)
        {
        //case 1:   
        //  ellips_func(&x[i*nx],&f[i],nx,OShift,M,1,1);
        //  f[i]+=100.0;
        //  break;
        case 1: 
            bent_cigar_func(&x[i*nx],&f[i],nx,OShift,M,1,1);
            //f[i]+=100*func_num;
            break;
        case 2: 
            discus_func(&x[i*nx],&f[i],nx,OShift,M,1,1);
            //f[i]+=100*func_num;
            break;
        //case 4:   
        //  rosenbrock_func(&x[i*nx],&f[i],nx,OShift,M,1,1);
        //  f[i]+=400.0;
        //  break;
        //case 5:
        //  ackley_func(&x[i*nx],&f[i],nx,OShift,M,1,1);
        //  f[i]+=500.0;
        //  break;
        case 3:
            weierstrass_func(&x[i*nx],&f[i],nx,OShift,M,1,1);
            //f[i]+=100*func_num;
            break;
        //case 7:   
        //  griewank_func(&x[i*nx],&f[i],nx,OShift,M,1,1);
        //  f[i]+=700.0;
        //  break;
        //case 8:   
        //  rastrigin_func(&x[i*nx],&f[i],nx,OShift,M,1,0);
        //  f[i]+=800.0;
        //  break;
        //case 9:   
        //  rastrigin_func(&x[i*nx],&f[i],nx,OShift,M,1,1);
        //  f[i]+=900.0;
        //  break;
        case 4: 
            schwefel_func(&x[i*nx],&f[i],nx,OShift,M,1,0);
            //f[i]+=100*func_num;
            break;
        //case 11:  
        //  schwefel_func(&x[i*nx],&f[i],nx,OShift,M,1,1);
        //  f[i]+=1100.0;
        //  break;
        case 5: 
            katsuura_func(&x[i*nx],&f[i],nx,OShift,M,1,1);
            //f[i]+=100*func_num;
            break;
        case 6: 
            happycat_func(&x[i*nx],&f[i],nx,OShift,M,1,1);
            //f[i]+=100*func_num;
            break;
        case 7: 
            hgbat_func(&x[i*nx],&f[i],nx,OShift,M,1,1);
            //f[i]+=100*func_num;
            break;
        case 8: 
            grie_rosen_func(&x[i*nx],&f[i],nx,OShift,M,1,1);
            //f[i]+=100*func_num;
            break;
        case 9: 
            escaffer6_func(&x[i*nx],&f[i],nx,OShift,M,1,1);
            //f[i]+=100*func_num;
            break;
        case 10:    
            hf01(&x[i*nx],&f[i],nx,OShift,M,SS,1,1);
            //f[i]+=100*func_num;
            break;
        //case 18:  
        //  hf02(&x[i*nx],&f[i],nx,OShift,M,SS,1,1);
        //  f[i]+=1800.0;
        //  break;
        case 11:    
            hf03(&x[i*nx],&f[i],nx,OShift,M,SS,1,1);
            //f[i]+=100*func_num;
            break;
        //case 20:  
        //  hf04(&x[i*nx],&f[i],nx,OShift,M,SS,1,1);
        //  f[i]+=2000.0;
        //  break;
        //case 21:  
        //  hf05(&x[i*nx],&f[i],nx,OShift,M,SS,1,1);
        //  f[i]+=2100.0;
        //  break;
        case 12:    
            hf06(&x[i*nx],&f[i],nx,OShift,M,SS,1,1);
            //f[i]+=100*func_num;
            break;
        case 13:    
            cf01(&x[i*nx],&f[i],nx,OShift,M,1);
            //f[i]+=100*func_num;
            break;
        //case 24:  
        //  cf02(&x[i*nx],&f[i],nx,OShift,M,1);
        //  f[i]+=2400.0;
        //  break;
        case 14:    
            cf03(&x[i*nx],&f[i],nx,OShift,M,1);
            //f[i]+=100*func_num;
            break;
        //case 26:
        //  cf04(&x[i*nx],&f[i],nx,OShift,M,1);
        //  f[i]+=2600.0;
        //  break;
        case 15:
            cf05(&x[i*nx],&f[i],nx,OShift,M,1);
            break;
        //case 28:
        //  cf06(&x[i*nx],&f[i],nx,OShift,M,1);
        //  f[i]+=2800.0;
        //  break;
        //case 29:
        //  cf07(&x[i*nx],&f[i],nx,OShift,M,SS,1);
        //  f[i]+=2900.0;
        //  break;
        //case 30:
        //  cf08(&x[i*nx],&f[i],nx,OShift,M,SS,1);
        //  f[i]+=3000.0;
        //  break;
        default:
            printf("\nError: There are only 30 test functions in this test suite!\n");
            f[i] = 0.0;
            break;
        }
        f[i]+=100*func_num;

        #ifndef NO_RECORDING
        record(get_number_of_run(), func_num, nx, &x[i*nx], f[i]);
        #endif //NO_RECORDING


    }



}


//cec15_test_func.h
#ifndef CEC15_TEST_FUNCTIONS_H
#define CEC15_TEST_FUNCTIONS_H


#define INF 1.0e99
#define EPS 1.0e-14
#define E  2.7182818284590452353602874713526625
#define PI 3.1415926535897932384626433832795029


#define MAX_OF_RUNS 20
#define RECORDING_POINTS_NUM 19
#define MAX_FUNCTION_NUMBER  15
#define TIMES_OF_EVAL  50
#define DIMS 2

/*
 * evaluate specific function
 */

__declspec(dllexport) void __stdcall cec15_test_func(double *x, double *f, int nx, int mx, int func_num);
__declspec(dllexport) double __stdcall fitness(double a[], int D, int func_num);

#ifndef NO_RECORDING
/*
 *  Number of run
 */
void set_number_of_run(int run);
/* 
 * output to file
 */
// dir can only like test/resultdir
void write_result_statistics_to_file(char* dir, char * file_prefix);

#endif //NO_RECORDING

#endif //CEC15_TEST_FUNCTIONS_H

//cec15.def
LIBRARY cec15

EXPORTS 
    cec15_test_func @1
    fitness @2

Tags: testforifssnumintfuncdouble

热门问题