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

📄 actionselection.c

📁 XCS is a new algorithm for artificial intelligent
💻 C
字号:
/*
/       (XCS)
/	------------------------------------
/	Learning Classifier System based on accuracy
/
/     by Martin Butz
/     University of Wuerzburg / University of Illinois at Urbana/Champaign
/     butz@illigal.ge.uiuc.edu
/     Last modified: 10-17-99
/
/     everything that is related to the action selection procedure
*/

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "classifierList.h"
#include "actionSelection.h"
#include "xcsMacros.h"
#include "env.h"


/* Determines the prediction array out of the classifier set ms */
void getPredictionArray(struct xClassifierSet *ms, double *pa)
{
  double nr[NUMBER_OF_ACTIONS], fit;
  int i, act;  

  /* ms should never be NULL (because of covering) */
  assert(ms!=NULL);
  
  /* set the values to 0 (nr memorizes the sum of the fitnesses) */  
  for(i=0;i<NUMBER_OF_ACTIONS;i++){
    pa[i]=0;
    nr[i]=0;
  }

  /* Get the sum of the fitnesses and the predictions */
  for( ; ms!=NULL ; ms=ms->next){
    act=getActInt(ms->cl->act);
    fit=ms->cl->fit;
    if(ms->cl->exp < REDUCTION_THRESHOLD)
      fit*=FITNESS_REDUCTION;
    pa[act]+=(ms->cl->pre*fit);
    nr[act]+=fit;
  }

  /* Divide the sum of the predictions by the sum of the fitnesses */
  for(i=0 ; i<NUMBER_OF_ACTIONS ;i++){
    if(nr[i] != 0)
      pa[i]/=nr[i];
    else
      pa[i]=0;
  }
}

/* choose the apropriate action */
void actionWinner(char *action, double *predictionArray)
{
  randomActionWinner(action, predictionArray);  
}

/* choose randomly one of the possible actions */
void randomActionWinner(char *action, double *predictionArray)
{
  int ret;

  /* Make sure it does not choose an action without a representative classifier */
  do{
    ret=(int)(frand()*(double)NUMBER_OF_ACTIONS);
  }while(predictionArray[ret]==0.);

  getActString(action, ret);
}

/* choose the best action in the prediction array */
void deterministicActionWinner(char *action, double *predictionArray)
{
  getActString(action, detActWinInt(predictionArray));
}

/* return the corresponding int value of the best action in the prediction array*/
int detActWinInt(double *predictionArray)
{
  int i,ret=0;
  double max=0.;

  for(i=0;i<NUMBER_OF_ACTIONS;i++){
    if(predictionArray[i]>max){
      max=predictionArray[i];
      ret=i;
    }
  }
  return ret;
}

/* choose the action by using the roulette wheel */
void rouletteWheelActionWinner(char *action, double *predictionArray)
{
  double sum=0.,sel;
  int i;
  
  for(i=0;i<NUMBER_OF_ACTIONS;i++)
    sum+=predictionArray[i];

  sel=frand()*sum;
  sum=predictionArray[0];
  i=0;
  while(sel>sum){
    i++;
    sum+=predictionArray[i];
  }
  getActString(action, i);
}

/*################################# converting action representation #############################################################*/

/* returns the corresponding int value of the action string act */
int getActInt(char *act)
{
  int actnr=0,i;

  for(i=0;i<ACTION_LENGTH;i++)
    actnr+=(((int)(act[i]-'0'))*pow(2,ACTION_LENGTH-i-1));
  return actnr;
}

/* returns the corresponding string of the action act */
void getActString(char *actString,int act)
{
  int actnr=0,i;

  for(i=0;i<ACTION_LENGTH;i++){
    if(act>=pow(2,ACTION_LENGTH-i-1)){
      actnr=1;
      act-=pow(2,ACTION_LENGTH-i-1);
    }else{
      actnr=0;
    }
    actString[i]=((char)actnr+'0');
  }
}

⌨️ 快捷键说明

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