예제 #1
0
파일: Adapter.cpp 프로젝트: DDMAL/aruspix
int main(int argc, char **argv)
{
	int n_gaussians;
	int n_states;
	bool is_symbol;
	char* file_states;

	char* model_name1=NULL;
	char* model_name2=NULL;
	char* model_name3=NULL;
	char* spacing_model;
	
	char* lex_name1=NULL;
	char* lex_name2=NULL;
	char* lex_name3=NULL;
	char *sent_start_symbol=NULL;
	char *sent_end_symbol=NULL;
	char *spacing_symbol=NULL;
	
	char *model_file_in;
	char *model_file_out;
	char *dir_name;

	real accuracy;
	real threshold;
	int max_iter;
	real prior;

	int max_load;

	bool add_space_to_targets;
	bool viterbi;
	bool train_separate;
	bool disk;
	
	bool no_learn_means;
	bool learn_var;
	bool learn_weights;
	real map_factor;
	bool adapt_separate;
	bool adapt_separate_set_data;

	Allocator *allocator = new Allocator;

    FileListCmdOption input_file_list("file name", "the list of inputs files or one data file");
	input_file_list.isArgument(true);
  
	FileListCmdOption target_file_list("file name", "the list of target files or one target file");
	target_file_list.isArgument(true);

	//=============================================================== 
	//=================== The command-line ==========================
	//=============================================================== 

	// Construct the command line
	CmdLine cmd;
	cmd.setBOption("write log", false );

	// Put the help line at the beginning
	cmd.info(help);

	// Adapt mode
	cmd.addText("\nArguments:");
	cmd.addSCmdArg("model_name1", &model_name1, "the list of the model models file");
	cmd.addSCmdArg("lex_name1", &lex_name1, "the model lexicon file");
	cmd.addSCmdArg("model_name2", &model_name2, "the list of the data models file");
	cmd.addSCmdArg("lex_name2", &lex_name2, "the data lexicon file");
	cmd.addSCmdArg("model_name3", &model_name3, "the list of the output models file");
	cmd.addSCmdArg("lex_name3", &lex_name3, "the output lexicon file");
	cmd.addCmdOption(&input_file_list);
	cmd.addCmdOption(&target_file_list);
	cmd.addSCmdArg("input model file", &model_file_in, "the input model file to be adapted");
	cmd.addSCmdArg("output model file", &model_file_out, "the adapted output model file");
	
    // Data Initialization
    cmd.addText("\nInitialization Options:") ;	
	cmd.addICmdOption("-n_gaussians", &n_gaussians, 10, "number of Gaussians");
	cmd.addICmdOption("-n_states", &n_states, 5, "number of states");
	cmd.addSCmdOption("-file_states", &file_states, "", "file containing n_states per model");
	cmd.addBCmdOption("-train_separate", &train_separate, false, "first train separate models");

    // Data and Model Parameters
    cmd.addText("\nData and Model Options:") ;
	cmd.addBCmdOption("-symbol", &is_symbol, false, "targets are in symbol format");
	cmd.addBCmdOption("-disk", &disk, false, "keep data on disk");
	cmd.addICmdOption("-load", &max_load, -1, "max number of examples to load for train");
	cmd.addSCmdOption("-spacing_model", &spacing_model,"", "name of silence phone");
	cmd.addBCmdOption("-add_sil_to_targets", &add_space_to_targets, false, "add silence at begining of targets");	
    cmd.addSCmdOption("-sent_start_symbol" , &sent_start_symbol , SP_START , "symbol that will start every sentence" ) ;
    cmd.addSCmdOption("-sent_end_symbol" , &sent_end_symbol , SP_END , "symbol that will end every sentence" ) ;
    cmd.addSCmdOption("-spacing_symbol" , &spacing_symbol , SP_WORD ,"the silence dictionary symbol" ) ;

	// Learning Options
	cmd.addText("\nLearning Options:");
	cmd.addBCmdOption("-viterbi", &viterbi, false, "viterbi training (instead of EM)");
	cmd.addRCmdOption("-threshold", &threshold, 0.001, "variance threshold");
	cmd.addRCmdOption("-prior", &prior, 0.001, "prior on the weights");
	cmd.addICmdOption("-iter", &max_iter, 25, "max number of iterations of HMM");
	cmd.addRCmdOption("-e", &accuracy, 0.00001, "end accuracy");
	cmd.addBCmdOption("-adapt_separate", &adapt_separate, false, "adapt models separately");
	cmd.addBCmdOption("-adapt_separate_set_data", &adapt_separate_set_data, true, "set data to non represented models when adapting  models separately");
	
	// MAP Options
	cmd.addText("\nMAP Options:");
	cmd.addRCmdOption("-map", &map_factor, 0.5, "adaptation factor [0-1]");
	cmd.addBCmdOption("-no_means", &no_learn_means, false, "no enroll means");
	cmd.addBCmdOption("-learn_var", &learn_var, false, "enroll var");
	cmd.addBCmdOption("-learn_weights", &learn_weights, false, "enroll weights");

	// Misc Options
	cmd.addText("\nMisc Options:");
	cmd.addSCmdOption("-dir", &dir_name, ".", "directory to save measures");

	// Read the command line
	cmd.read(argc, argv);
	cmd.setWorkingDirectory(dir_name);

	DiskXFile::setBigEndianMode();

	//==================================================================== 
	//=================== Data preparation ===============================
	//====================================================================
	
	Random::seed();
	
    clock_t start_time , end_time ;
    real total_time = 0.0 ;
    start_time = clock() ;
	
	MlHMM hmm;
	
	//==================================================================== 
	//=================== Set Options ====================================
	//====================================================================
	
	// initialization
	hmm.setIOption("number of gaussians", n_gaussians );
	hmm.setIOption("number of states", n_states );
	hmm.setBOption("train separate", train_separate );
	
	// data
	hmm.setBOption("is symbol", is_symbol );
	hmm.setBOption("add spacing", add_space_to_targets );
	hmm.setIOption("max load", max_load );
	hmm.setBOption("disk", disk );

	// training
	hmm.setROption("accuracy", accuracy );
	hmm.setROption("threshold", threshold );
	hmm.setIOption("iterations", max_iter );
	hmm.setROption("prior", prior );
	hmm.setBOption("viterbi", viterbi );
	
	// map
	hmm.setROption("map factor", map_factor );
	hmm.setBOption("no means", no_learn_means );
	hmm.setBOption("learn var", learn_var );
	hmm.setBOption("learn weights", learn_weights );
	hmm.setBOption("adapt separate", adapt_separate );
	hmm.setBOption("adapt separate set data", adapt_separate_set_data );
	
	hmm.setLexicon( model_name1, spacing_model, lex_name1, add_space_to_targets ? sent_start_symbol : NULL );

	hmm.mapSetDataLexicon( model_name2, lex_name2, model_name3, lex_name3 );

	//==================================================================== 
	//=================== Create the DataSet ... =========================
	//==================================================================== 
	
	hmm.setData( input_file_list.file_names, input_file_list.n_files, target_file_list.file_names, target_file_list.n_files );
	
	hmm.mapInit( file_states );
	
	char* n_nll = strConcat(2,"hmm_train_val_",viterbi ? "viterbi" : "em");
	allocator->retain(n_nll);
	hmm.mapAdapt( model_file_in, model_file_out, cmd.getXFile(n_nll) );
	
	end_time = clock() ;
    total_time = (real)(end_time-start_time) / CLOCKS_PER_SEC ;
	
    printf("\nTotal time spent adapting = %.2f secs\n", total_time) ;

	delete allocator;
	
	printf( SUCCESS );
	
	return(0);
}
예제 #2
0
파일: osc_bt.cpp 프로젝트: malloch/qualia
int main(int argc, char** argv) {
  //signal(SIGINT, stop);
  //signal(SIGTERM, stop);
  std::set_terminate(handler);

  int dimObservations;
  int dimActions;
  char* stringNActions;

  int oscPort;
  int oscRemotePort;
  char* oscIP;
  int agentId;

  bool exportData;
  int seed;


  //=================== The command-line ==========================

  // Construct the command line
  CmdLine cmd;

  // Put the help line at the beginning
  cmd.info("Simple B-Tree agent");

  cmd.addText("\nArguments:");
  cmd.addICmdArg("agent_id", &agentId, "the id of the agent", true);
  cmd.addICmdArg("dim_observations", &dimObservations, "observation dimension (without the reward)", true);
  cmd.addICmdArg("dim_actions", &dimActions, "action dimension", true);
  cmd.addSCmdArg("n_actions", &stringNActions, "number of actions as comma-separated values", true);

  cmd.addText("\nOSC Options:");
  cmd.addSCmdOption("-ip", &oscIP, "127.0.0.1", "the osc IP address", false);
  cmd.addICmdOption("-port", &oscPort, 11000, "the osc local STARTING port (actual port will be port + agent_id)", false);
  cmd.addICmdOption("-rport", &oscRemotePort, 12000, "the osc remote local port", false);

  cmd.addText("\nMisc Options:");
  cmd.addBCmdOption("-export-data", &exportData, false, "export the data to files", false );
  cmd.addICmdOption("-seed", &seed, -1, "the random seed (-1 = based on time)");

  // Read the command line
  //int mode = cmd.read(argc, argv);
  cmd.read(argc, argv);

  // Parse n actions.
  printf("Parsing actions: ");
  unsigned int nActions[100];
  char tmp[1000];
  strcpy(tmp, stringNActions);
  int k=0;
  for (int i=0; i<dimActions-1; i++, k++) {
    Q_ASSERT_ERROR_MESSAGE( sscanf(tmp, "%d,%s", &nActions[k], tmp) > 0, "Malformed argument <n_actions>: %s", stringNActions);
    printf("%d ", nActions[k]);
  }
  Q_ASSERT_ERROR_MESSAGE( sscanf(tmp, "%d", &nActions[k]), "Malformed argument <n_actions>: %s", stringNActions);
  printf("%d \n", nActions[k]);

  oscPort += agentId;
  char oscPortStr[100];
  char oscRemotePortStr[100];
  sprintf(oscPortStr, "%d", oscPort);
  sprintf(oscRemotePortStr, "%d", oscRemotePort);
  printf("Opening OSC connection (ip=%s, port=%s, remote_port=%s)\n", oscIP, oscPortStr, oscRemotePortStr);
  OscManager::initOsc(oscIP, oscPortStr, oscRemotePortStr);

  // Set random seed.
  if (seed == -1)
  {
    randomSeed(time(NULL) + agentId);
  }
  else
  {
    randomSeed(seed);
  }

  ActionProperties actionProperties(ACTION_DIM, N_ACTIONS);

#if ATTRACTION_MODE
  int nRepeat = (agentId + 1) * 10;

  BehaviorTreeNode* root =

      BT.priority()->setChildren(
          BT.sequential()->setChildren(
              Q_NEW(BoolCondition<TestBTreeAgent>)(&TestBTreeAgent::hasClosest, false),
              BT.repeat(-1)->setChild(
                  BT.sequential()->setChildren(
                      Q_NEW(FloatCondition<TestBTreeAgent>)(&TestBTreeAgent::getClosestDistance, GREATER_OR_CLOSE, 0.05),
                      Q_NEW(ChooseAction)(&actionProperties, ATTRACT),
                      BT_END)
                  ),
              BT_END),
          BT.repeat(-1)->setChild(
              BT.probability()->setWeightedChildren(
                  BT.weighted(0.99, Q_NEW(ChooseAction)(&actionProperties, ATTRACT)),
                  BT.weighted(0.01, BT.failure()),
                  BT_END)
              ),
          BT.sequential()->setChildren(
            Q_NEW(BoolCondition<TestBTreeAgent>)(&TestBTreeAgent::hasClosest, true),
            BT.repeat(500)->setChild(
                Q_NEW(ChooseAction)(&actionProperties, REPEL)
            ),
            BT_END),

          BT_END);

//  BehaviorTreeInternalNode* root = new PriorityNode();
//  ProbabilityNode* probNode = new ProbabilityNode();
//  probNode->addChild(new ChooseAction(&actionProperties, ATTRACT), 0.99f);
//  probNode->addChild(new AlwaysFailure(), 0.01f);
//
//  root->addChild((new SequentialNode())
//        ->addChild(new BoolCondition<TestBTreeAgent>(&TestBTreeAgent::hasClosest, false))
//        ->addChild((new PriorityNode())
//          ->addChild((new RepeatNode(-1))
//            ->addChild((new SequentialNode)
//              ->addChild(new FloatCondition<TestBTreeAgent>(&TestBTreeAgent::getClosestDistance, GREATER_OR_CLOSE, 0.05))
//              ->addChild(new ChooseAction(&actionProperties, ATTRACT))))
//          ->addChild((new RepeatNode(-1))
//            ->addChild(probNode))))
//
//      ->addChild((new SequentialNode())
//        ->addChild(new BoolCondition<TestBTreeAgent>(&TestBTreeAgent::hasClosest, true))
//        ->addChild((new RepeatNode(500))
//            ->addChild(new ChooseAction(&actionProperties, REPEL))));

#else
  int nRepeat = (agentId + 1) * 10;
  BehaviorTreeNode* root =
      sequential()->CHILDREN(
          repeat(nRepeat)->CHILDREN(
              BT_NEW(ChooseAction)(&actionProperties, UP)
          ),
          repeat(nRepeat)->CHILDREN(
              BT_NEW(ChooseAction)(&actionProperties, RIGHT)
          ),
          repeat(nRepeat)->CHILDREN(
              BT_NEW(ChooseAction)(&actionProperties, DOWN)
          ),
          repeat(nRepeat)->CHILDREN(
              BT_NEW(ChooseAction)(&actionProperties, LEFT)
          )
      );
//  BehaviorTreeInternalNode* root = new SequentialNode();
//  root->addChild((new RepeatNode(nRepeat))
//        ->addChild((new ChooseAction(&actionProperties, UP))))
//      ->addChild((new RepeatNode(nRepeat))
//        ->addChild((new ChooseAction(&actionProperties, RIGHT))))
//      ->addChild((new RepeatNode(nRepeat))
//        ->addChild((new ChooseAction(&actionProperties, DOWN))))
//      ->addChild((new RepeatNode(nRepeat))
//        ->addChild((new ChooseAction(&actionProperties, LEFT))));
#endif

  Agent* agent = new TestBTreeAgent(&actionProperties, root);

  printf("--- Creating environment ---\n");
  Environment* env;
  Environment* oscEnv = new OscRLEnvironment(agentId, dimObservations, actionProperties.dim());
  if (exportData) {
    char fileName[1000];
    sprintf(fileName, "export-%d.raw", agentId);
    DiskXFile* f = new(Alloc::instance()) DiskXFile(fileName, "w+");
    env = new FileExportEnvironment(oscEnv, f, dimObservations, actionProperties.dim());
  } else
    env = oscEnv;

  Qualia* qualia = new RLQualia(agent, env);

  printf("--- Initialising ---\n");
  qualia->init();

  printf("--- Starting ---\n");
  qualia->start();

  printf("--- Looping ---\n");

  stopLoop = false;
  while (!stopLoop) {
    qualia->step();
  }

  delete qualia;
  delete agent;
  delete env;
  Q_DELETE(root);
  //delete root;

  return 0;
}