* Sample of the main function [#pb9a4179]
''Table of Contents''
#contents

* Overview [#gcb2e842]
In the main function, the following should be done:
- Instantiate the TAgent class
- Load agent scripts
- Execute at least one of member function of a module

* Simple Example [#p26b9a48]
SkyAI provides utility functions which make the main function simple.

The following is a simple example:
#codeh(cpp){{
#include <skyai/skyai.h>
#include <skyai/utility.h>
...
using namespace std;
using namespace loco_rabbits;
...
int main(int argc, char**argv)
{
  TOptionParser option(argc,argv);
  TAgent  agent;
  if (!ParseCmdLineOption (agent, option))  return 0;

  MMazeTaskModule *p_maze_task = dynamic_cast<MMazeTaskModule*>(agent.SearchModule("maze_task"));
  if(p_maze_task==NULL)  {LERROR("module `maze_task' is not defined as an instance of MMazeTaskModule"); return 1;}

  agent.SaveToFile (agent.GetDataFileName("before.agent"),"before-");

  p_maze_task->Start();

  agent.SaveToFile (agent.GetDataFileName("after.agent"),"after-");
  return 0;
}
}}
The ParseCmdLineOption provides some command line options:
- -path PATH_LIST : specify the path list separated by comma
- -agent AGENT_LIST : specify the list of agent scripts separated by comma; the extension .agent can be omitted, and the directory can be omitted if it is included in the path list
- -outdir OUT_DIR : specify the output directory where data files are stored; if a non-existent directory is specified, no files are stored

In executing ParseCmdLineOption, the agent files are loaded.

#codeh(cpp){{
  MMazeTaskModule *p_maze_task = dynamic_cast<MMazeTaskModule*>(agent.SearchModule("maze_task"));
  if(p_maze_task==NULL)  {LERROR("module `maze_task' is not defined as an instance of MMazeTaskModule"); return 1;}
}}
This part searches the module named `maze_task' and gets it as a pointer of MMazeTaskModule type.  Here, MMazeTaskModule is a user-defined task module.
p_maze_task is used later.

The SaveToFile function saves the current agent status into a file as an agent script in the data directory specified by -outdir option.

#codeh(cpp){{
  p_maze_task->Start();
}}
This calls the member function Start of the MMazeTaskModule module, which starts a run.

* Advanced Example [#ma3bdfa2]
The following example is a more complicated example.
#codeh(cpp){{
int main(int argc, char**argv)
{
  TOptionParser option(argc,argv);  // parsing command line option

  TAgent  agent;
  std::ofstream debug;
  if (!ParseCmdLineOption (agent, option, debug))  return 0;  // scripts are loaded

  // searching modules made by the script
  MBasicLearningManager *p_lmanager = dynamic_cast<MBasicLearningManager*>(agent.SearchModule("lmanager"));
  MMazeEnvironment *p_environment = dynamic_cast<MMazeEnvironment*>(agent.SearchModule("environment"));
  if(p_lmanager==NULL)  {LERROR("module `lmanager' is not defined correctly"); return 1;}
  if(p_environment==NULL)  {LERROR("module `environment' is not defined correctly"); return 1;}
  MBasicLearningManager &lmanager(*p_lmanager);
  MMazeEnvironment &environment(*p_environment);


  // save agent into file
  agent.SaveToFile (agent.GetDataFileName("before.agent"),"before-");

  // -help option and detecting invalid options
  {
    stringstream optss;
    if (option("help")!="")
      {cerr<<"valid options:"<<endl; option.PrintUsed(); return 0;}
    if (option.PrintNotAccessed(optss))
      {cerr<<"invalid options:"<<endl<<optss.str(); return 1;}
  }

  /// main process:

  lmanager.Initialize();  // call directory the member-function
  lmanager.StartLearning();  // call directory the member-function

  while (lmanager.IsLearning())
  {
    environment.StepLoop();
  }

  // save agent into file
  agent.SaveToFile (agent.GetDataFileName("after.agent"),"after-");

  return 0;
}
}}
Here, we access a generic learning manager module MBasicLearningManager.
In order to access the members of this module, the following should be included:
#codeh(cpp){{
#include <skyai/modules_core/learning_manager.h>
}}


* Makefile [#hf7a3a9d]
Write the makefile as follows:
#codeh(makefile){{
BASE_REL_DIR:=../..
include $(BASE_REL_DIR)/Makefile_preconf
##-------------------------------------------------------------------------------------------
EXEC := maze.out
OBJS := maze.o
##-------------------------------------------------------------------------------------------
USING_SKYAI_ODE:=true
MAKING_SKYAI:=true
include $(BASE_REL_DIR)/Makefile_body
}}
The following variables should be changed:
- BASE_REL_DIR : relative path to the base directory of the SkyAI
- EXEC : specify the name of executable
- OBJS : specify the names of the object files separated by space
- USING_SKYAI_ODE or USING_SKYAI : set true if you want to use it
- MAKING_SKYAI : set true if you want to make the library


Front page   New List of pages Search Recent changes   Help   RSS of recent changes