Example #1
0
	void mutate() {
		assert(cfg);
		size_t action = 0;  // default = modify
		if (cfg->mutateSize) {
			action =
			    chooseAction(std::vector<T>{{cfg->modifyProba, cfg->addProba, cfg->eraseProba}},
			                 getRandomEngine());
		}
		std::uniform_real_distribution<T> d(0, 1);  // TODO replace by generic rand
		std::uniform_int_distribution<int> dInt(0, values.size() - 1);
		switch (action) {
			case 0:
				// modify
				values[dInt(getRandomEngine())] = d(getRandomEngine());
				break;
			case 1:
				// add
				values.push_back(d(getRandomEngine()));
				break;

			case 2:
			default:
				// erase
				values.erase(values.begin() + dInt(getRandomEngine()));
				break;
		}
	}
Example #2
0
void MergeTool::readData()
{
    while (m_process->bytesAvailable()) {
        QByteArray line = m_process->canReadLine() ? m_process->readLine() : m_process->readAllStandardOutput();
        // {Normal|Deleted|Submodule|Symbolic link} merge conflict for 'foo.cpp'
        int index = line.indexOf(" merge conflict for ");
        if (index != -1) {
            m_mergeType = mergeType(line.left(index));
            int quote = line.indexOf('\'');
            m_fileName = QString::fromLocal8Bit(line.mid(quote + 1, line.lastIndexOf('\'') - quote - 1));
            m_localState = waitAndReadStatus(m_localInfo);
            m_remoteState = waitAndReadStatus(m_remoteInfo);
            chooseAction();
        } else if (line.startsWith("Continue merging")) {
            if (QMessageBox::question(Core::ICore::dialogParent(), tr("Continue Merging"),
                                      tr("Continue merging other unresolved paths?"),
                                      QMessageBox::Yes | QMessageBox::No,
                                      QMessageBox::No) == QMessageBox::Yes) {
                m_process->write("y\n");
            } else {
                m_process->write("n\n");
            }
            m_process->waitForBytesWritten();
        }
    }
}
// virtual
void GQLearner::refinePolicyAndChooseNextAction(const double* pSenses, double* pOutActions)
{
	double reward;
	if(m_teleported)
		reward = UNKNOWN_REAL_VALUE;
	else
		reward = rewardFromLastAction();
	if(reward != UNKNOWN_REAL_VALUE)
	{
		// Find the best next action
		double maxQ = 0;
		double q;
		m_pActionIterator->reset(pSenses);
		int i;
		for(i = 0; i < m_actionCap; i++)
		{
			if(!m_pActionIterator->nextAction(pOutActions))
				break;
			q = getQValue(pSenses, pOutActions);
			if(q > maxQ)
				maxQ = q;
		}

		// Update the Q-values
		q = reward + m_discountFactor * maxQ;
		setQValue(m_pSenses, m_pAction, (1.0 - m_learningRate) * getQValue(m_pSenses, m_pAction) + m_learningRate * q);
	}

	// Decide what to do next
	GVec::copy(m_pSenses, pSenses, m_senseDims);
	chooseAction(pSenses, pOutActions);
	GVec::copy(m_pAction, pOutActions, m_actionDims);
	m_teleported = false;
}
Example #4
0
void ParameterReader::readParameters(){
	QString command;

	int cnt = 0;
    do{
		qDebug() << ">> ";
		if (_input.atEnd()) {
			command = "exit";
		} else {
			command = _input.readLine();
			if (command.isEmpty()) {
				if (++cnt == 3) {
					command = "exit";
				}
			}
		}
        chooseAction(command);

	} while(!command.startsWith("exit"));
}
Example #5
0
File: qlv2.cpp Project: chagge/AI
double QL::playAnEpisode(bool toTrain) {
	double epScore = 0;
	int ftime = 1;

	while(!gameOver() && !interface->isTerminal()) {
		if(ftime == 1) {
			ftime = 0;
			epScore += initSeq();
		}
		int toAct = chooseAction(toTrain);
		double lastScore = repeatLastAction(ind2Act[toAct], numFrmStack, toTrain);
		epScore += lastScore;
		int reward = 0;
		if(lastScore != 0.0f) {
			reward = 1;
			if(lastScore < 0.0f) {
				reward = -1;
			}
		}

		if(toTrain) {
			History history = {(maxHistoryLen+lastHistInd-2)%maxHistoryLen, 
								reward, 
								toAct, 
								interface->isTerminal(), 
								(maxHistoryLen+lastHistInd-1)%maxHistoryLen};
			saveHistory(history);
			if(dExp.size() > info.memThreshold) {
				getAMiniBatch();
				learnWts();
			}
		}
	}
	interface->resetVals(1);
	return epScore;
}
Example #6
0
/**
 * @brief Main learning method that will change the model and learn the best actions to take
 *
 * @param [in] current date and time
 */
void pqos_learn_main(char* cb_time)
{
  int k = 0, stopper = 0, numOfSteps = 0;
  double reward = 0.0, old_checker = 0.0;
  stopper = 0;
  /* while not converged */
  while (stopper == 0){
    counter = 0;
    numOfSteps = 0;

    /* for each COS */
  for(k = 1; k < num_COS; k++){    
    /* reset all values for the start of the episode */
    int current_x_pos = 0, i = 0, numberOfSteps = 0, stuck_in_pos = 0;
    double this_Q = 0.0, max_Q = 0.0, new_Q = 0.0, step_change = 0.0;
    counter = 0;
    old_reward = 0;
    numOfSteps = 0;
    
    for(i = 0; i < 12; i++){
      real_world[k][i] = 0;
    }
    /* can not have a cos equal to 0, change to 1 */
    real_world[k][11] = 1;

    /* set the current pos */
    current_x_pos = (int) convert_to_mask(k);

    /* reset the mask to match the real world */
    set_new_mask(learning_sock_count, learning_sockets, (unsigned)k, current_x_pos);
    cos_change(learning_sock_count, learning_sockets);

    while(counter == 0){
      /* check to see if the COS has a core attached to it, no point in learning if it dosent */
      if(coses_used[k] == 0){
	    break;
      }
      reward = 0.0;
      int old_x_pos = current_x_pos;
      
      /* get the best action to play and save its Q value */
      int action = chooseAction( current_x_pos, epsilon, k);
      printf("Action picked is: %d\n" , action);
      this_Q = the_model[k][current_x_pos][action];
      /* now move into the new state */
      current_x_pos = change_bit_mask(action, k);
      printf("cos bits after change: ");
      print_cos_bits(k);

      /* sleep for a moment to let the changes take effect */
      usleep(20000);
      
      /* get the reward for the action taken */
      reward = calReward(k, current_x_pos, action);

      /* find the max q value available */
      max_Q = the_model[k][current_x_pos][0];
      for(i = 1; i < 2; i++){
	if(the_model[k][current_x_pos][i] > max_Q)
	  max_Q = the_model[k][current_x_pos][i];
      }//for

      /* Calculate new value for Q */
      new_Q = this_Q + (alpha * (reward + (gamma_2 * max_Q) - this_Q));
      
      /* Set the new Q value */
      the_model[k][old_x_pos][action] = new_Q;

      //stop getting stuck in an unmovable pos like 100000000000
      if(old_x_pos == current_x_pos){
	stuck_in_pos++;
      }else {
	stuck_in_pos = 0;
      }
      if(stuck_in_pos >= 10){
	counter = 1;
	target_ipc -= 0.01;
     }      
      numOfSteps++;
    }//while
    /**
     * testing ipc data for reports
     * testing environment is
     * core 0 used for OS tasks only and linked to COS 0
     * all the rest of the cores are used for test and are linked to COS 1
     * COS 0 is ignored as its only OS tasks
     */
    if(coses_used[k] != 0 && k == 1){
      numberOfNumbers++;
      print_cos_world(k);
      printf("Cos bits are: ");
      print_cos_bits(k);

      printf("Num of steps taken: %d\n", numOfSteps);
      printf("time/C-Reward/max/min/avg %s, %.2f, %.2f, %.2f, %.2f,\n",cb_time, currentIPC, maxIPC, minIPC, average);
      reset_window();

      printRangeOfStates(k);
      printGoalsOverStates(k);
      printHeatMap();
    }
    
  }//for

  /* cal step change to see if model has converged */
  double diff = 0.0;
  diff = cal_step_change(&old_checker, reward);
  printf("Step Change in main is %.2f\n", diff);
  if(diff > -2.00 &&
     diff < 2.00 ){
    stopper = 1;
  }
  
  }//while
}
Example #7
0
http::response MinecraftDynmapProto::sendRequest(const int request_type, std::string *post_data, std::string *get_data)
{
	http::response resp;

	// Prepare the request
	NETLIBHTTPREQUEST nlhr = { sizeof(NETLIBHTTPREQUEST) };

	// FIXME: get server

	// Set request URL
	std::string url = m_server + chooseAction(request_type, get_data);
	nlhr.szUrl = (char*)url.c_str();

	// Set timeout (bigger for channel request)
	nlhr.timeout = 1000 * ((request_type == MINECRAFTDYNMAP_REQUEST_EVENTS) ? 65 : 20);

	// Set request type (GET/POST) and eventually also POST data
	if (post_data != NULL) {
		nlhr.requestType = REQUEST_POST;
		nlhr.pData = (char*)(*post_data).c_str();
		nlhr.dataLength = (int)post_data->length();
	}
	else {
		nlhr.requestType = REQUEST_GET;
	}

	// Set headers - it depends on requestType so it must be after setting that
	nlhr.headers = get_request_headers(nlhr.requestType, &nlhr.headersCount);

	// Set flags
	nlhr.flags = NLHRF_HTTP11;

#ifdef _DEBUG 
	nlhr.flags |= NLHRF_DUMPASTEXT;
#else
	nlhr.flags |= NLHRF_NODUMP;
#endif	

	// Set persistent connection (or not)
	switch (request_type)
	{
	case MINECRAFTDYNMAP_REQUEST_HOME:
		nlhr.nlc = NULL;
		break;

	case MINECRAFTDYNMAP_REQUEST_EVENTS:
		nlhr.nlc = hEventsConnection;
		nlhr.flags |= NLHRF_PERSISTENT;
		break;

	default:
		WaitForSingleObject(connection_lock_, INFINITE);
		nlhr.nlc = hConnection;
		nlhr.flags |= NLHRF_PERSISTENT;
		break;
	}

	debugLogA("@@@@@ Sending request to '%s'", nlhr.szUrl);

	// Send the request	
	NETLIBHTTPREQUEST *pnlhr = (NETLIBHTTPREQUEST*)CallService(MS_NETLIB_HTTPTRANSACTION, (WPARAM)m_hNetlibUser, (LPARAM)&nlhr);

	mir_free(nlhr.headers);

	// Remember the persistent connection handle (or not)
	switch (request_type)
	{
	case MINECRAFTDYNMAP_REQUEST_HOME:
		break;

	case MINECRAFTDYNMAP_REQUEST_EVENTS:
		hEventsConnection = pnlhr ? pnlhr->nlc : NULL;
		break;

	default:
		ReleaseMutex(connection_lock_);
		hConnection = pnlhr ? pnlhr->nlc : NULL;
		break;
	}

	// Check and copy response data
	if (pnlhr != NULL)
	{
		debugLogA("@@@@@ Got response with code %d", pnlhr->resultCode);
		store_headers(&resp, pnlhr->headers, pnlhr->headersCount);
		resp.code = pnlhr->resultCode;
		resp.data = pnlhr->pData ? pnlhr->pData : "";

		// debugLogA("&&&&& Got response: %s", resp.data.c_str());

		CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)pnlhr);
	} else {
		debugLogA("!!!!! No response from server (time-out)");
		resp.code = HTTP_CODE_FAKE_DISCONNECTED;
		// Better to have something set explicitely as this value is compaired in all communication requests
	}

	return resp;
}