/*
* Stand-alone C codes for fuzzy inference systems.
* (This file is included in fismain.c)
* J.-S. Roger Jang, 1994.
* Copyright 1994-2001 The MathWorks, Inc.
*/
/*
* Copyright 1994-2005 The MathWorks, Inc.
*/
#ifndef __FIS__
# define __FIS__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
/***********************************************************************
Macros and definitions
**********************************************************************/
/* Define portable printf and double */
#if defined(MATLAB_MEX_FILE)
# define PRINTF mexPrintf
# define DOUBLE real_T
#elif defined(__SIMSTRUC__)
# define PRINTF ssPrintf
# define DOUBLE real_T
#else
# define PRINTF printf
# define DOUBLE double
#endif
#ifndef ABS
# define ABS(x) ( (x) > (0) ? (x): (-(x)) )
#endif
#ifndef MAX
# define MAX(x,y) ( (x) > (y) ? (x) : (y) )
#endif
#ifndef MIN
# define MIN(x,y) ( (x) < (y) ? (x) : (y) )
#endif
#define MF_PARA_N 4
#define STR_LEN 500
#define MF_POINT_N 101
/* debugging macros */
/*
#define PRINT(expr) printf(#expr " = %g\n", (double)expr)
#define PRINTMAT(mat,m,n) printf(#mat " = \n"); fisPrintMatrix(mat,m,n)
#define FREEMAT(mat,m) printf("Free " #mat " ...\n"); fisFreeMatrix(mat,m)
#define FREEARRAY(array) printf("Free " #array " ...\n"); free(array)
*/
#if (defined(MATLAB_MEX_FILE) && !defined(__SIMSTRUC__))
# define FREE mxFree
#else
# define FREE free
#endif
#define FREEMAT(mat,m) fisFreeMatrix(mat,m)
#define FREEARRAY(array) FREE(array)
/***********************************************************************
Data types
**********************************************************************/
typedef struct fis_node {
int handle;
int load_param;
char name[STR_LEN];
char type[STR_LEN];
char andMethod[STR_LEN];
char orMethod[STR_LEN];
char impMethod[STR_LEN];
char aggMethod[STR_LEN];
char defuzzMethod[STR_LEN];
int userDefinedAnd;
int userDefinedOr;
int userDefinedImp;
int userDefinedAgg;
int userDefinedDefuzz;
int in_n;
int out_n;
int rule_n;
int **rule_list;
DOUBLE *rule_weight;
int *and_or; /* AND-OR indicator */
DOUBLE *firing_strength;
DOUBLE *rule_output;
/* Sugeno: output for each rules */
/* Mamdani: constrained output MF values of rules */
struct io_node **input;
struct io_node **output;
DOUBLE (*andFcn)(DOUBLE, DOUBLE);
DOUBLE (*orFcn)(DOUBLE, DOUBLE);
DOUBLE (*impFcn)(DOUBLE, DOUBLE);
DOUBLE (*aggFcn)(DOUBLE, DOUBLE);
DOUBLE (*defuzzFcn)();
DOUBLE *BigOutMfMatrix; /* used for Mamdani system only */
DOUBLE *BigWeightMatrix;/* used for Mamdani system only */
DOUBLE *mfs_of_rule; /* MF values in a rule */
DOUBLE *bias; /*bias, to be tuned when no rules are fired*/
int isbias;
struct fis_node *next;
} FIS;
typedef struct io_node {
char name[STR_LEN];
int mf_n;
DOUBLE bound[2];
DOUBLE value;
struct mf_node **mf;
} IO;
typedef struct mf_node {
char label[STR_LEN]; /* MF name */
char type[STR_LEN]; /* MF type */
int nparams; /* length of params field */
DOUBLE *params; /* MF parameters */
int userDefined; /* 1 if the MF is user-defined */
DOUBLE (*mfFcn)(DOUBLE, DOUBLE *); /* pointer to a mem. fcn */
DOUBLE value; /* for Sugeno only */
DOUBLE *value_array; /* for Mamdani only, array of MF values */
} MF;
#endif /* __FIS__ */
/***********************************************************************
File, arrays, matrices operations
**********************************************************************/
/* Copyright 1994-2002 The MathWorks, Inc. */
/* $Revision: $ $Date: $ */
/* display error message and exit */
static void fisError(char *msg)
{
#ifdef MATLAB_MEX_FILE
mexErrMsgTxt(msg);
#else
PRINTF("%s\n",msg);
exit(1);
#endif
}
#ifndef NO_PRINTF /*in case for rtw and dSPACE use */
/* an friendly interface to fopen() */
static FILE *fisOpenFile(char *file, char *mode)
{
FILE *fp, *fopen();
if ((fp = fopen(file, mode)) == NULL){
PRINTF("The file %s cannot be opened.", file);
fisError("\n");
}
return(fp);
}
#endif
/* define a standard memory access function with error checking */
void *fisCalloc(int num_of_x, int size_of_x)
{
void *ptr;
#if (defined(MATLAB_MEX_FILE) && !defined(__SIMSTRUC__))
/* datstruc.c ln325 requires ptr = NULL when it supplies num_of_x = 0 */
if (num_of_x == 0)
ptr = NULL; /* mxCalloc returns a NULL pointer if num_of_x or size_of_x = 0 */
else {
ptr = mxCalloc(num_of_x, size_of_x);
/* however we still need to check that memory was allocated successfully,
exclude the case when num_of_x = 0, and if unsuccessful issue an error */
if (ptr == NULL)
fisError("Could not allocate memory in mxCalloc function call.");}
#else /* a Simulink file (defined(__SIMSTRUC__)), or standalone is being created */
if (num_of_x == 0)
ptr = NULL; /* calloc returns a NULL pointer if num_of_x or size_of_x = 0 */
else {
ptr = calloc(num_of_x, size_of_x);
/* however we still need to check that memory was allocated successfully,
exclude the case when num_of_x = 0, and if unsuccessful issue an error */
if (ptr == NULL)
fisError("Could not allocate memory in calloc function call.");}
#endif
return(ptr);
}
char **fisCreateMatrix(int row_n, int col_n, int element_size)
{
char **matrix;
int i;
if (row_n == 0 && col_n == 0)
return(NULL);
matrix = (char **)fisCalloc(row_n, sizeof(char *));
if (matrix == NULL)
fisError("Calloc error in fisCreateMatrix!");
for (i = 0; i < row_n; i++) {
matrix[i] = (char *)fisCalloc(col_n, element_size);
if (matrix[i] == NULL)
fisError("Calloc error in fisCreateMatrix!");
}
return(matrix);
}
/* won't complain if given matrix is already freed */
static void fisFreeMatrix(void **matrix, int row_n)
{
int i;
if (matrix != NULL) {
for (i = 0; i < row_n; i++) {
FREE(matrix[i]);
}
FREE(matrix);
}
}
static DOUBLE**fisCopyMatrix(DOUBLE **source, int row_n, int col_n)
{
DOUBLE **target;
int i, j;
target = (DOUBLE **)fisCreateMatrix(row_n, col_n, sizeof(DOUBLE));
for (i = 0; i < row_n; i++)
for (j = 0; j < col_n; j++)
target[i][j] = source[i][j];
return(target);
}
#ifndef NO_PRINTF /* not available for RTW and dSPACE */
static void fisPrintMatrix(DOUBLE **matrix, int row_n, int col_n)
{
int i, j;
for (i = 0; i < row_n; i++) {
for (j = 0; j < col_n; j++)
PRINTF("%.3f ", matrix[i][j]);
PRINTF("\n");
}
}
static void fisPrintArray(DOUBLE *array, int size)
{
int i;
for (i = 0; i < size; i++)
PRINTF("%.3f ", array[i]);
PRINTF("\n");
}
static void
fisPause()
{
PRINTF("Hit RETURN to continue ...\n");
getc(stdin);
}
#endif
/***********************************************************************
Parameterized membership functions
**********************************************************************/
/* Copyright 1994-2006 The MathWorks, Inc. */
/* $Revision: $ $Date: $ */
#ifndef UNUSED_PARAMETER
# if defined(__LCC__)
# define UNUSED_PARAMETER(x) /* do nothing */
# else
/*
* This is the semi-ANSI standard way of indicating that a
* unused function parameter is required.
*/
# define UNUSED_PARAMETER(x) (void) (x)
# endif
#endif
/* Triangular membership function */
static DOUBLE fisTriangleMf(DOUBLE x, DOUBLE *params)
{
DOUBLE a = params[0], b = params[1], c = params[2];
if (a>b)
fisError("Illegal parameters in fisTriangleMf() --> a > b");
if (b>c)
fisError("Illegal parameters in fisTriangleMf() --> b > c");
if (a == b && b == c)
return(x == a);
if (a == b)
return((c-x)/(c-b)*(b<=x)*(x<=c));
if (b == c)
return((x-a)/(b-a)*(a<=x)*(x<=b));
return(MAX(MIN((x-a)/(b-a), (c-x)/(c-b)), 0));
}
/* Trapezpoidal membership function */
static DOUBLE fisTrapezoidMf(DOUBLE x, DOUBLE *params)
{
DOUBLE a = params[0], b = params[1], c = params[2], d = params[3];
DOUBLE y1 = 0, y2 = 0;
if (a>b) {
PRINTF("a = %f, b = %f, c = %f,
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
ANFISchengxu.rar (7个子文件)
ANFISchengxu
newfis.c 1KB
anfisedit.c 46KB
getfis.c 18KB
anfis.c 10KB
fis.c 66KB
genfis1.c 4KB
anfismex.c 371B
共 7 条
- 1
dongxiao2014
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页