Public Member Functions

iCub::iha::ActionSelect Class Reference
[Action Selection (IHA)]

Action Selection Process class. More...

#include <ActionSelect.h>

Public Member Functions

bool parseNeighbourList (Bottle *neighbourlist[])
 Read the data from the bottles for each horizon and populate the probability and action frequency arrays.
int selectAction ()
 Select an action given the current experience and action probabilites.
void init (Searchable &config)
 Initialize.
void getActions (ConstString filename, ConstString seqdir)
 read actions from sequence files
void updateNextBehaviourSet (int act)
 Set the next behaviour set based on the passed action.
int chooseFromDistribution (vector< double > distribution)
 stochastically select a number from a weighted distrubution (roulette wheel)
int chooseRandom ()
 Pick a random action.
int chooseRandomFromSet (vector< int > aset)
 Pick a random number (action) from a set (vector actually) of numbers.
int selectRandomAction (vector< int > next_behaviour_set)
 Pick a random number (action) from the next behaviour set.
vector< int > getNextBehaviourSet (int act)
 Get the next behaviour set for the given action.
bool parseNeighbourList (Bottle *neighbourlist[])
 Read the data from the bottles for each horizon and populate the probability and action frequency arrays.
int selectAction ()
 Select an action given the current experience and action probabilites.
void init (Searchable &config)
 Initialize.
void getActions (ConstString filename)
 read actions from sequence files
void updateNextBehaviourSet (int act)
 Set the next behaviour set based on the passed action.
int chooseFromDistribution (vector< double > distribution)
 stochastically select a number from a weighted distrubution (roulette wheel)
int chooseRandom ()
 Pick a random action.
int chooseRandomFromSet (vector< int > aset)
 Pick a random number (action) from a set (vector actually) of numbers.
int selectRandomAction (vector< int > next_behaviour_set)
 Pick a random number (action) from the next behaviour set.
vector< int > getNextBehaviourSet (int act)
 Get the next behaviour set for the given action.
std::string getActionName (int act)

Detailed Description

Action Selection Process class.

For description of the algorithm see Action Selection (IHA)

Author:
Assif Mirza

Copyright (C) 2008 RobotCub Consortium

CopyPolicy: Released under the terms of the GNU GPL v2.0.

Definition at line 66 of file ActionSelect.h.


Member Function Documentation

int iCub::iha::ActionSelect::chooseFromDistribution ( vector< double >  distribution  ) 

stochastically select a number from a weighted distrubution (roulette wheel)

e.g. if distribution is 0.1 0.5 0.3 0.1

then there should be a 10% chance of picking choices 0 or 3 a 30% chance of picking 2 and a 50% chance of picking choice number 2

Parameters:
distribution vector of doubles that is the prob distribution

Definition at line 66 of file ActionSelect.cpp.

Referenced by selectAction().

                                                                           {
        // rand() return an integer between 0 and MAXINT
        // convert to an int between 0 and wheelResolution
        int r = rand() % wheelResolution;

        int accumProb = 0;
        int chosen = -1;

        if (IhaDebug::getLevel()>=DBGL_DEBUG2) {
                IhaDebug::pmesg(DBGL_DEBUG2,"chooseFromDistribution: r=%d\n",r);
                IhaDebug::pmesg(DBGL_DEBUG2,"Dist:");
                for (int a=0 ; a<distribution.size() && chosen<0 ; a++) {
                        IhaDebug::pmesg(DBGL_DEBUG2," %d:%f=%d",a,distribution[a],(int) (distribution[a] * (double)wheelResolution));
                }
                IhaDebug::pmesg(DBGL_DEBUG2,"\n");
        }
        for (int a=0 ; a<distribution.size() && chosen<0 ; a++) {
                accumProb += (int) (distribution[a] * (double)wheelResolution);
                if (r < accumProb) {
                        chosen = a;
                }
        } 
        IhaDebug::pmesg(DBGL_DEBUG2,"Chose: %d\n",chosen);
        return chosen;
}

int iCub::iha::ActionSelect::chooseFromDistribution ( vector< double >  distribution  ) 

stochastically select a number from a weighted distrubution (roulette wheel)

e.g. if distribution is 0.1 0.5 0.3 0.1

then there should be a 10% chance of picking choices 0 or 3 a 30% chance of picking 2 and a 50% chance of picking choice number 2

Parameters:
distribution vector of doubles that is the prob distribution
int iCub::iha::ActionSelect::chooseRandomFromSet ( vector< int >  aset  )  [inline]

Pick a random number (action) from a set (vector actually) of numbers.

Parameters:
aset vector of actions

Definition at line 128 of file ActionSelect.h.

Referenced by selectRandomAction().

    {
        int r = rand() % aset.size();
        return aset[r];
    }

int iCub::iha::ActionSelect::chooseRandomFromSet ( vector< int >  aset  )  [inline]

Pick a random number (action) from a set (vector actually) of numbers.

Parameters:
aset vector of actions

Definition at line 128 of file ActionSelect.h.

    {
        int r = rand() % aset.size();
        return aset[r];
    }

void ActionSelect::getActions ( ConstString  filename,
ConstString  seqdir 
)

read actions from sequence files

Read the action list from the given file into the Actions class.

Parameters:
filename sequence file name
seqdir directory parh to file

Populates action_commands array and NUM_ACTIONS

Definition at line 48 of file ActionSelect.cpp.

References iCub::iha::Actions::getFullActionCommandList().

                                                                      {
    //------------------------------------------------------
        // create the action defs object and read the actions
        // from the config file
        Property actiondefs_props;
        actiondefs_props.fromConfigFile(filename.c_str());
        
        if (!iCubActions.open(actiondefs_props, seqdir)) {
                fprintf(stderr,"Error in action definitions\n");
                exit(-1);
        }

        action_commands = iCubActions.getFullActionCommandList();
        NUM_ACTIONS = action_commands.size();
        IhaDebug::pmesg(DBGL_DEBUG2,"NUM_ACTIONS defined : %d\n",NUM_ACTIONS);
}

void ActionSelect::getActions ( ConstString  filename  ) 

read actions from sequence files

Read the action list from the given file into the Actions class.

Parameters:
filename sequence file name
seqdir directory parh to file

Populates action_commands array and NUM_ACTIONS

Definition at line 49 of file ActionSelect.cpp.

References iCub::iha::Actions::getFullActionCommandList().

                                                  {
    //------------------------------------------------------
        // create the action defs object and read the actions
        // from the config file
        Property actiondefs_props;
        actiondefs_props.fromConfigFile(filename.c_str());
        
        //if (!iCubActions.open(actiondefs_props, seqdir)) {
        if (!iCubActions.open(actiondefs_props)) {
                fprintf(stderr,"Error in action definitions\n");
                exit(-1);
        }

        action_commands = iCubActions.getFullActionCommandList();
        NUM_ACTIONS = action_commands.size();
        IhaDebug::pmesg(DBGL_DEBUG2,"NUM_ACTIONS defined : %d\n",NUM_ACTIONS);
}

vector<int> iCub::iha::ActionSelect::getNextBehaviourSet ( int  act  )  [inline]

Get the next behaviour set for the given action.

Parameters:
act action

Definition at line 144 of file ActionSelect.h.

                                             {
        return iCubActions.getActionNextBehaviourSet(act);
    }

vector<int> iCub::iha::ActionSelect::getNextBehaviourSet ( int  act  )  [inline]

Get the next behaviour set for the given action.

Parameters:
act action

Definition at line 144 of file ActionSelect.h.

                                             {
        return iCubActions.getActionNextBehaviourSet(act);
    }

bool iCub::iha::ActionSelect::parseNeighbourList ( Bottle *  neighbourlist[]  ) 

Read the data from the bottles for each horizon and populate the probability and action frequency arrays.

Parameters:
neighbourlist[] Neighbour list bottles
bool iCub::iha::ActionSelect::parseNeighbourList ( Bottle *  neighbourlist[]  ) 

Read the data from the bottles for each horizon and populate the probability and action frequency arrays.

Parameters:
neighbourlist[] Neighbour list bottles

Definition at line 99 of file ActionSelect.cpp.

{
        elist.clear();
        distances.clear();
        actfreq_distributions.clear();
        masses.clear();
        values.clear();

        for (int hor=0; hor < numHorizons; hor++) 
        {
                if (neighbourlist[hor]==NULL) {
                        IhaDebug::pmesg(DBGL_STATUS2,"No neighbour list received for horizon %d\n",horizons[hor]);
                        continue;
                }
                IhaDebug::pmesg(DBGL_DEBUG3,"RCV:%s\n",neighbourlist[hor]->toString().c_str());

                // check this is a proper line (invalid lines have an expid of -1)
                //   item 0 is horizon
                //   item 1 is expid
                //   data starts at n-2
                if (neighbourlist[hor]->get(1).asInt() >= 0 && neighbourlist[hor]->size()>2) 
                {
                        int expid = neighbourlist[hor]->get(1).asInt();

                        IhaDebug::pmesg(DBGL_DEBUG2,"Distances from exp %d h=%d :\n",expid,horizons[hor]);
                        // loop through neighbours
                        for (int n=2; n < neighbourlist[hor]->size(); n++) {
                                // get the distance and experience id to which this distance relates
                                
                                Bottle* itemn = neighbourlist[hor]->get(n).asList();
                                int    exp  = itemn->get(0).asInt();    // item 0 expid
                                double dist = itemn->get(1).asDouble(); // item 1 distance

                                // save the distance in the map if it is within the radius we
                                // are interested in
                                if (dist <= neighbour_radius) {
                                        pair<int,int> he = pair<int,int>(horizons[hor],exp);
                                        elist.insert(pair<double,pair<int,int> >(dist, he));
                                        distances.insert(pair<pair<int,int>,double >(he, dist));
                                

                                        double val  = itemn->get(2).asDouble();   // item 3 value
                                
                                        // store the value for this hor/exp
                                        values[pair<int,int>(horizons[hor],exp)] = val;

                                        IhaDebug::pmesg(DBGL_DEBUG2,"exp: %d dist: %f val: %f actfreq: ",exp,dist,val);

                                        Bottle* actfreql = itemn->get(3).asList();// item 4 action frequency distribution
                                        //
                                        // zero the distribution
                                        for (int a=0;a<NUM_ACTIONS;a++) {
                                                actfreq_distributions[he].push_back(0);
                                        }
                                        // store the action frequency distribution
                                        int totalaf=0;
                                        for (int afl=0;afl<actfreql->size();afl++) {
                                                int ac = actfreql->get(afl).asList()->get(0).asInt();
                                                int af = actfreql->get(afl).asList()->get(1).asInt();
                                                IhaDebug::pmesg(DBGL_DEBUG2,"%d:%d ",ac,af);
                                                actfreq_distributions[he][ac]=(double)af;
                                                totalaf+=af;;
                                        }
                                        IhaDebug::pmesg(DBGL_DEBUG2,"\n");
                                        // turn into probabilities by dividing by number of actions
                                        // in the frequecy distribution
                                        for (int i=0;i< actfreq_distributions[he].size();i++) {
                                                actfreq_distributions[he][i] /= (double)totalaf;
                                        }
                                        // the total of the action frequencies = mass of the experience
                                        masses[he] = (double)totalaf;

                                }

                        } // loop thru neighbours
                } // valid line

        } // for

        return true;
}

int iCub::iha::ActionSelect::selectRandomAction ( vector< int >  next_behaviour_set  ) 

Pick a random number (action) from the next behaviour set.

Parameters:
next_behaviour_set vector of actions
int ActionSelect::selectRandomAction ( vector< int >  next_behaviour_set  ) 

Pick a random number (action) from the next behaviour set.

Parameters:
next_behaviour_set vector of actions

Definition at line 469 of file ActionSelect.cpp.

References chooseRandom(), and chooseRandomFromSet().

Referenced by selectAction().

{
        int chosenAction;
        if (use_behaviour_sets) 
        {
                chosenAction = chooseRandomFromSet(next_behaviour_set);
                IhaDebug::pmesg(DBGL_STATUS1,"Choosing random action %d from set \n",(int)chosenAction);
                IhaDebug::pmesg(DBGL_DEBUG1,"Behaviour Set : ");
                for (int i=0;i<next_behaviour_set.size();i++) {
                        IhaDebug::pmesg(DBGL_DEBUG1,"%d ",next_behaviour_set[i]);
                }
                IhaDebug::pmesg(DBGL_DEBUG1,"\n");
        }
        else
        {
                // choose a random action from all available
                chosenAction  = chooseRandom();
                IhaDebug::pmesg(DBGL_STATUS1,"Choosing random action %d\n",(int)chosenAction);
        }
         return chosenAction;

}

void ActionSelect::updateNextBehaviourSet ( int  act  ) 

Set the next behaviour set based on the passed action.

Parameters:
act action

Definition at line 463 of file ActionSelect.cpp.

References iCub::iha::Actions::getActionNextBehaviourSet(), and iCub::iha::Actions::getActionNextBehaviourSetNo().

Referenced by iCub::contrib::ActionSelectionModule::sendAction().

                                                 {
        if (iCubActions.getActionNextBehaviourSetNo(act) >= 0) {
                next_behaviour_set=iCubActions.getActionNextBehaviourSet(act);
        }
}

void iCub::iha::ActionSelect::updateNextBehaviourSet ( int  act  ) 

Set the next behaviour set based on the passed action.

Parameters:
act action

The documentation for this class was generated from the following files:
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Friends Defines