⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 evalfismex.c

📁 模糊逻辑工具箱 模糊逻辑工具箱 模糊逻辑工具箱 模糊逻辑工具箱 模糊逻辑工具箱
💻 C
字号:
/* Copyright (c) 1994-98 by The MathWorks, Inc. */
/* $Revision: 1.3 $ */

#include "mex.h"
#include "fis.h"

/* change calloc to mxCalloc, free to mxFree */
#define calloc mxCalloc
#define free mxFree

#include "lib.c"
#include "mf.c"
#include "t_norm.c"
#include "defuzz.c"
#include "callml.c"
#include "list.c"
#include "list2.c"
#include "evaluate.c"
#include "mt2ceval.c"

#define	INPUT		prhs[0]		/* input of FIS */
#define	FISMATRIX	prhs[1]		/* FIS matrix */
#define NUMOFPOINT      prhs[2]         /* number of points */
#define	OUTPUT		plhs[0]		/* output */
#define	IRR		plhs[1]		/* input mf matrix */
#define	ORR		plhs[2]		/* output mf matrix */
#define	ARR		plhs[3]		/* aggregated output mf */

/* void */
void
mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
	double *output, *irr, *orr, *arr;
	double *input, *tmp;

        mxArray *fispointer, *inputpointer, *mfpointer;
        char *buf;
	unsigned int m ,n;
	int i, j,k, param_size, data_n, in_n;
	FIS *fis;

        int status, buflen=100;	
        int io_vector_size;
        double *real_data_ptr;
	int *in_mf_n, *out_mf_n;
	IO *io_list;
        double *numofpointPt;
        int numofpoints;

        if (nrhs != 3)
		mexErrMsgTxt("Needs 3 input arguments.");
        if (!mxIsDouble(INPUT))
		mexErrMsgTxt("The first input must be a defined double matrix");

        if (!mxIsStruct(FISMATRIX))
		mexErrMsgTxt("The Second input must be a structure.");


	/* Check the dimensions of input vector */
	/* This is permissive - granted as long as enough inputs
	   are there. So the user can pass the original training data
	   directly */
        m=mxGetM(NUMOFPOINT);
        numofpointPt=mxGetPr(NUMOFPOINT);
        if (m > 0 & numofpointPt[0] > 101)          
          numofpoints=numofpointPt[0];
        else
          numofpoints=101;

	m = mxGetM(INPUT);
	n = mxGetN(INPUT);

/*=====================*/
        fis=matlab2cStr(prhs[1], numofpoints);
/*=====================*/
	if (!((n >= fis->in_n) || ((n == 1) && (m >= fis->in_n)))) {
		mexPrintf("The input vector is of size %dx%d,", m, n);
		mexPrintf("while expected input vector size is %d.\n", in_n);
		mexErrMsgTxt("Exiting ...");
	}
	if ((n == 1) && (m == fis->in_n))
		data_n = 1;
	else
		data_n = m;

	/* signal error if multiple input vectors and mutiple output arguments */
	/*
	if ((data_n > 1) && (nlhs > 1))
		mexErrMsgTxt("Multiple output arguments can only be used with a single input vector.");
	*/


	/* Create matrices for returned arguments */
	OUTPUT = mxCreateDoubleMatrix(data_n, fis->out_n, mxREAL);

	for (i = 0; i < data_n; i++) {
	    /* Assign pointers to the various parameters */
		output = mxGetPr(OUTPUT);
		input = mxGetPr(INPUT);

		/* Dispatch the inputs */
		for (j = 0; j < fis->in_n; j++)
			fis->input[j]->value = input[j*data_n+i];
		/* Compute the output */
		fisEvaluate(fis, numofpoints);
		/* Collect the output */
		for (j = 0; j < fis->out_n; j++)
			output[j*data_n+i] = fis->output[j]->value;
	}

	/* take care of additonal output arguments */
	if (nlhs >= 2) {
		IRR = mxCreateDoubleMatrix(fis->rule_n, fis->in_n, mxREAL);
		irr = mxGetPr(IRR);
		for (i = 0; i < fis->rule_n; i++)
			for (j = 0; j < fis->in_n; j++) {
				int which_mf = fis->rule_list[i][j];
				double mf_value;
				if (which_mf > 0)
					mf_value = fis->input[j]->mf[which_mf-1]->value;
				else if (which_mf == 0) {
				/* Don't care; mf_value depends on AND or OR */
					if (fis->and_or[i] == 1)	/* AND */
						mf_value = 1;
					else
						mf_value = 0;
					}
				else            /* Linguistic hedge NOT */
					mf_value = 1 - fis->input[j]->mf[-which_mf-1]->value;
				irr[j*fis->rule_n+i] = mf_value;
			}
	}

	if (nlhs >= 3) {
		if (strcmp(fis->type, "mamdani") == 0) {
			ORR = mxCreateDoubleMatrix(numofpoints, (fis->rule_n)*(fis->out_n), mxREAL);
			orr = mxGetPr(ORR);
			for (i = 0; i < numofpoints; i++)
				for (j = 0; j < (fis->rule_n)*(fis->out_n); j++) {
					int which_rule = j%(fis->rule_n);	/* zero offset */
					int which_output = j/(fis->rule_n);	/* zero offset */
					int which_mf = fis->rule_list[which_rule][fis->in_n+which_output];
					double tmp;
					if (which_mf > 0)
						tmp = fis->output[which_output]->mf[which_mf-1]->value_array[i];
					else if (which_mf == 0)
						tmp = 0;
					else
						tmp = 1-fis->output[which_output]->mf[-which_mf-1]->value_array[i];
					/*
					mexPrintf("rule = %d, output = %d, mf = %d\n", which_rule,
						which_output, which_mf);
					*/
					if (!fis->userDefinedImp)
						orr[j*numofpoints+i] = (*fis->impFcn)(tmp,
							fis->firing_strength[which_rule]);
					else {
						double tmp_vec[2];
						tmp_vec[0] = tmp;
						tmp_vec[1] = fis->firing_strength[which_rule];
						orr[j*numofpoints+i] =
							fisCallMatlabFcn(tmp_vec, 2, fis->impMethod);
        			}
				}
		}
		if (strcmp(fis->type, "sugeno") == 0) {
			ORR = mxCreateDoubleMatrix(fis->rule_n, fis->out_n, mxREAL);
			orr = mxGetPr(ORR);
			for (i = 0; i < fis->rule_n; i++)
				for (j = 0; j < fis->out_n; j++) {
					int which_mf = fis->rule_list[i][fis->in_n+j]-1;
					/*
					mexPrintf("mf = %d\n", which_mf);
					*/
					if (which_mf == -1)	/* don't_care consequent */
						orr[j*fis->rule_n+i] = 0;
					else
						orr[j*fis->rule_n+i] = fis->output[j]->mf[which_mf]->value;
				}
		}
	}

	if (nlhs >= 4) {
		if (strcmp(fis->type, "mamdani") == 0) {
			ARR = mxCreateDoubleMatrix(numofpoints, fis->out_n, mxREAL);
			arr = mxGetPr(ARR);
			for (i = 0; i < numofpoints; i++)
				for (j = 0; j < fis->out_n; j++)
					arr[j*numofpoints+i] = fisFinalOutputMf(fis, j, i);
		}
		if (strcmp(fis->type, "sugeno") == 0) {
			ARR = mxCreateDoubleMatrix(fis->rule_n, fis->out_n, mxREAL);
			arr = mxGetPr(ARR);
			for (i = 0; i < fis->rule_n; i++)
				for (j = 0; j < fis->out_n; j++)
					arr[j*fis->rule_n+i] = fis->firing_strength[i];
		}
	}

	/* destroy FIS data structure */
	fisFreeFisNode(fis);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -