Example #1
0
    /*! Returns the action that maximizes the Q-value for the given state
     *
     */
    size_t argmax_q(const mat & Q, const size_t &state, const uvec & possible_a){

      // There might be several actions that give the max Q-value
      vector<size_t> maxq_actions;

      // Calculate the max Q-value for this state and actions possible from here
      double maxq = Q(state, possible_a(0));
      for(auto i : range(1,possible_a.size())){
        if(Q(state,possible_a(i)) > maxq){
          maxq = Q(state,possible_a(i));
        }
      }
      // Check if there are several actions with same q-value as maxq
      for(auto i : range(possible_a.size())){
        if (Q(state,possible_a(i)) == maxq){
          maxq_actions.push_back(possible_a(i));
        }
      }

      // Return the single action that maximizes the Q-value or
      //  randomly one of the actions that maximize the Q-value.
      if(maxq_actions.size() > 1){
        return maxq_actions[randint(maxq_actions.size())];
      }else{
        return maxq_actions[0];
      }
    }
Example #2
0
    /*! Combinations of integer ranges
     *
     *  Calculates all combinations of integer ranges of which the length is given in the input vector.
     *
     *  @param dim   vector of dimensions for each variable
     *
     *  @retval      Matrix of size (prod(dim) x #variables)
     */
    Mat<size_t> combinations(const uvec & dim){
      size_t nvariables = dim.size();
      size_t ncombinations = prod(dim);
      Mat<size_t> result(ncombinations, nvariables);

      for(auto var_i : range(nvariables)){
        size_t var_val = 0;
        size_t var_len;

        if (var_i == nvariables-1){
          var_len = 1; // Last variable
        }else{
          var_len = prod(dim(span(var_i+1,nvariables-1)));
        }

        for(auto j : range(ncombinations)){
          if ((j % (var_len * dim(var_i)) == 0) && j > 0){
            var_val = 0;
          }else if ((j % var_len == 0) && j > 0){
            var_val += 1;
          }
          result(j, var_i) = var_val;
        }
      }
      return result;
    };
Example #3
0
// Set difference
// - Return vector Ea with elements of Ec removed
// ======================================================================
uvec setdifference(uvec Ea, uvec Ec){
  int na = Ea.size();
   
  for(int j=na; j --> 0;){
    bool matchind = any(Ec == Ea(j));
    if(matchind == TRUE){
      Ea.shed_row(j);
    }
  }

  return(Ea);
}
Example #4
0
//[[Rcpp::export]]
mat update_mstates_arma(const uvec& extantLines, const mat& Q,  mat mstates)
{
	int u; 
	vec p_u ; 
	double sms; 
	int m = Q.n_rows;
	for (int i = 0; i < extantLines.size(); i++){
		u = extantLines(i); 
		p_u = mstates.col(u-1);
		mstates.col(u-1) = Q * p_u ; 
		//~ for (int k = 0; k < m; k++){
			//~ mstates(u-1, k) = dot( p_u, Q.col(k)); 
		//~ }
		sms = sum(mstates.col(u-1)); 
		mstates.col(u-1) /= sms; 
	}
	return mstates;
}
Example #5
0
void calc_overlap(vec &ov,vec &ov_n1,Dres_det dres1,int f1,Dres_det dres2,uvec f2)
{
	int n = f2.size();

	double cx1 = dres1.x(f1);
	double cx2 = dres1.x(f1)+dres1.w(f1)-1;
	double cy1 = dres1.y(f1);
	double cy2 = dres1.y(f1)+dres1.h(f1)-1;

	vec gx1 = dres2.x(f2);
	vec gx2 = dres2.x(f2)+dres2.w(f2)-1;
	vec gy1 = dres2.y(f2);
	vec gy2 = dres2.y(f2)+dres2.h(f2)-1;

	double ca = dres1.h(f1)*dres1.w(f1); 
	vec ga = dres2.h(f2)*dres2.w(f2);

	//find the overlapping area
	vec xx1 = zeros<vec>(n);
	vec yy1 = zeros<vec>(n);
	vec xx2 = zeros<vec>(n);
	vec yy2 = zeros<vec>(n);

	for(int i = 1;i<=n;i++)
	{
		xx1(i) = max(cx1,gx1(i));
		yy1(i) = max(cy1,gy1(i));
		xx2(i) = min(cx2,gx2(i));
		yy2(i) = min(cy2,gy2(i));
	}
	vec w = xx2-xx1+1;
	vec h = yy2-yy1+1;

	uvec inds = find((w>0)*(h>0));
	ov = zeros<vec>(n);
	ov_n1 = zeros<vec>(n);
	if(inds.is_empty() == 0)
	{
		vec inter = w(inds)*h(inds);
		vec u = ca+ga(inds)-w(inds)*h(inds);
		ov(inds) = inter/u;
		ov_n1(inds) = inter/ca;
	}
}
Example #6
0
//[[Rcpp::export]]
mat finite_size_correction(const vec& p_a, const vec& A, const uvec& extantLines, mat mstates)
{
	// NOTE mstates m X n 
	int u; 
	vec rho; 
	vec rterm; 
	//~ vec lterm; 
	double lterm; 
	vec p_u; 
	for (int iu = 0; iu < extantLines.size(); iu++){
		u = extantLines(iu); 
		p_u = mstates.col(u-1); 
		rterm = p_a / ( A - p_u); 
		rho = A / (A - p_u ); 
		lterm = dot( rho, p_a); //
		p_u = p_u % (lterm - rterm) ;
		p_u = p_u / sum(p_u ) ; 
		mstates.col(u - 1) = p_u; 
	}
	return mstates; 
}
Example #7
0
inline uvec vclip(uvec v, double low = vmin, double high = vmax)/*{{{*/
{
	for (int i = 0;i < v.size();i ++)
		v[i] = min(max(v[i], vmin), vmax);
	return v;
}/*}}}*/