Exemple #1
0
main()
	{
	
		struct BT *rt=nn(6);
		rt->l=nn(67);
		rt->r=nn(45);
		rt->l->r=nn(454);
		rt->r->r=nn(56);
		
		rt->l->l=nn(4545);
		rt->r->l=nn(45);
		
		print_inorder(rt);
		int level=0;
		mirror(rt,level);
		printf("\n\n");
		print_inorder(rt);
		
		printf("Catalan No:: %d \n\n",cat(3));
			
	
	}	
Exemple #2
0
void insert(struct BT **root,struct Q *q,int d)
    {
        struct BT *temp=nn(d);
        
        if(!*root)
            *root=temp;
            
         else
            {
                struct BT *front=get(q);
                
                if(!front->l)
                    front->l=temp;
               else  if(!front->r)
                    front->r=temp;  
                     
                if(hasdouble(front))
                    dq(q);
            
            }
            
           nq(q,temp);    
     
    } 
Exemple #3
0
void render::HeightMap::compute(
	unsigned int rows, unsigned int cols,
	const float *heights, 
	int stride,
	const math::matrix3x1<float> span,
	bool eliminateZeroTriangles, 
	Texture **textures,
	unsigned int levels,
	unsigned int M) {
			
	vertices.reserve((rows + 1) * (cols + 1));
	for (unsigned int r = 0; r <= rows; r++) {
		for (unsigned int c = 0; c <= cols; c++) {
			float u = (float)r / (float)rows;
			float v = (float)c / (float)cols;
			
			math::matrix3x1<float> p(u * span.x, 0, v * span.z);
			if (r < rows && c < cols) {
				p.y = heights[(r * cols + c) * stride] * span.y;
			}
			
			math::matrix3x1<float> n(0, 1, 0);
			geometry::Vertex vertex(p, n, u, v);
			vertices.push_back(vertex);
		}
	}
	
	for (unsigned int r = 1; r < rows - 1; r++) {
		for (unsigned int c = 1; c < cols - 1; c++) {
			
			geometry::Vertex &vertex = vertices.at(r * (cols + 1) + c);
			
			const float dy = 0.01f;
			math::matrix3x1<float> ne( (heights[(r * cols + c) * stride] - heights[((r + 1) * cols + c) * stride]), dy, 0);
			math::matrix3x1<float> nw( heights[((r-1) * cols + c) * stride] - heights[(r * cols + c) * stride], dy, 0);
			math::matrix3x1<float> nn( 0, dy, heights[(r * cols + c) * stride] - heights[(r * cols + c - 1) * stride]);
			math::matrix3x1<float> ns( 0, dy, heights[(r * cols + c+1) * stride] - heights[(r * cols + c) * stride]);
			
			/*
			math::matrix3x1<float> ne( (heightMap.get(r, c) - heightMap.get(r+1, c)), dy, 0);
			math::matrix3x1<float> nw( heightMap.get(r-1, c) - heightMap.get(r, c), dy, 0);
			math::matrix3x1<float> nn( 0, dy, heightMap.get(r, c) - heightMap.get(r, c-1));
			math::matrix3x1<float> ns( 0, dy, heightMap.get(r, c+1) - heightMap.get(r, c));
			*/

			math::matrix3x1<float> normal = (ne.unit() + nw.unit() + nn.unit() + ns.unit()).unit();
			vertex.nx = normal.x;
			vertex.ny = normal.y;
			vertex.nz = normal.z;
		}
	}	
	
	unsigned int N = 1;
	for (unsigned int level = 0; level < levels; ++level) {
	
		meshes.push_back(RigidMesh());
		RigidMesh &mesh = meshes.back();
		mesh.levelOfDetail = level;
		
		if (textures) {
			for (unsigned int i = 0; i < RigidMesh::Num_textures && textures[i]; ++i) {
				mesh.textures[i] = 0;
			}
		}
	
		computeIndices(rows, cols, mesh, N, eliminateZeroTriangles);
		N *= M;
	}
	
	constructVertexBuffer();
}
Exemple #4
0
void
trackrun(Lextok *n)
{
	runstmnts = nn(ZN, 0, n, runstmnts);
}
Exemple #5
0
  void pBFGS_nnetwork<T>::updater_loop()
  {
    {
      {
	std::lock_guard<std::mutex> lock(thread_is_running_mutex);
	thread_is_running++;
      }
      thread_is_running_cond.notify_all();
    }
    
    while(thread_running){
      std::chrono::seconds duration(1);
      std::this_thread::sleep_for(duration);
      
      // checks that if some BFGS thread has been converged or
      // not running anymore and recreates a new optimizer thread
      // after checking what the best solution found was updated
      
      try{
	std::lock_guard<std::mutex> lock(bfgs_mutex);
	
	for(auto& o : optimizers){
	  if(o->solutionConverged() || o->isRunning() == false){
	    
	    math::vertex<T> x;
	    T y;
	    unsigned int iters = 0;
	    
	    if(o->getSolution(x, y, iters)){
	      if(y < global_best_y){
		global_best_y = y;
		global_best_x = x;
	      }
	      
	      global_iterations += iters;
	    }
	    
	    {
	      o.reset(new BFGS_nnetwork<T>(net, data, overfit, negativefeedback));

	      nnetwork<T> nn(this->net);
	      nn.randomize();
	      normalize_weights_to_unity(nn);
	      T alpha = T(0.5f);
	      negative_feedback_between_neurons(nn, data, alpha);
	      
	      math::vertex<T> w;
	      nn.exportdata(w);
	      
	      o->minimize(w);
	    }
	  }
	}
      }
      catch(std::exception& e){
      }
      
    }
    
    
    {
      std::lock_guard<std::mutex> lock(bfgs_mutex);
      
      for(auto& o : optimizers)
	o->stopComputation();
      
      optimizers.clear();
    }
    
    
    
    {
      {
	std::lock_guard<std::mutex> lock(thread_is_running_mutex);
	thread_is_running--;
      }
      thread_is_running_cond.notify_all();
    }    
    
    
  }
  OpspacePlanarController * OpspacePlanarController::
  create(std::string const & param_root,
	 jspace::Vector const & default_kp,
	 jspace::Vector const & default_kd)
  {
    ros::NodeHandle nn("~");
    OpspacePlanarController * controller(0);
    
    try {
      std::string q1_name;
      if ( ! nn.getParam(param_root + "/q1_name", q1_name)) {
	throw std::runtime_error("missing q1_name parameter");
      }
      
      std::string tmp;
      bool q1_inverted(false);
      if (nn.getParam(param_root + "/q1_inverted", tmp)) {
	q1_inverted = str_to_bool(param_root + "/q1_inverted", tmp, false);
      }
      
      double l1_length;
      if ( ! nn.getParam(param_root + "/l1_length", l1_length)) {
	throw std::runtime_error("missing l1_length parameter");
      }

      std::string q2_name;
      if ( ! nn.getParam(param_root + "/q2_name", q2_name)) {
	throw std::runtime_error("missing q2_name parameter");
      }
      
      bool q2_inverted(false);
      if (nn.getParam(param_root + "/q2_inverted", tmp)) {
	q2_inverted = str_to_bool(param_root + "/q2_inverted", tmp, false);
      }
      
      double l2_length;
      if ( ! nn.getParam(param_root + "/l2_length", l2_length)) {
	throw std::runtime_error("missing l2_length parameter");
      }
      
      double op_kp;
      if ( ! nn.getParam(param_root + "/op_kp", op_kp)) {
	throw std::runtime_error("missing op_kp parameter");
      }
      
      double op_kd;
      if ( ! nn.getParam(param_root + "/op_kd", op_kd)) {
	throw std::runtime_error("missing op_kd parameter");
      }
      
      double op_vmax;
      if ( ! nn.getParam(param_root + "/op_vmax", op_vmax)) {
	throw std::runtime_error("missing op_vmax parameter");
      }
      
      controller = new OpspacePlanarController(q1_name, q1_inverted, l1_length,
					       q2_name, q2_inverted, l2_length,
					       op_kp, op_kd, op_vmax,
					       default_kp, default_kd);
    }
    
    catch (std::exception const & ee) {
      ROS_ERROR ("OpspacePlanarController::create(`%s'): EXCEPTION: %s", param_root.c_str(), ee.what());
      delete controller;
      controller = 0;
    }
    
    return controller;
  }
Exemple #7
0
bool Smiles::load(const QString &file)
{
    clear();
    for (unsigned i = 0;; i++){
        const smile *s = defaultSmiles(i);
        if (s == NULL)
            break;
        SmileDef sd;
        sd.paste = s->paste;
        sd.icon  = NULL;
        m_smiles.push_back(sd);
    }
    QString fname = file;
    QFile f(fname);
    if (!f.open(IO_ReadOnly))
        return false;
#ifdef USE_EXPAT
    int pdot = fname.findRev(".");
    if ((pdot > 0) && (fname.mid(pdot + 1).lower() == "xep")){
        XepParser p;
        if (!p.parse(f))
            return false;
        for (list<xepRecord>::iterator it = p.m_rec.begin(); it != p.m_rec.end(); ++it){
            xepRecord &r = *it;
            QPixmap pict = p.pict(r.index);
            if (pict.isNull())
                continue;
            SmileDef sd;
            sd.title = getValue(r.title.c_str());
            sd.paste = sd.title;
            string exp   = getValue(r.smiles.c_str());
            for (const char *p = exp.c_str(); *p; p++){
                if (*p == '\\'){
                    if (*(++p) == 0)
                        break;
                    sd.exp += '\\';
                    sd.exp += *p;
                    continue;
                }
                if ((*p == '{') || (*p == '}'))
                    sd.exp += '\\';
                sd.exp += *p;
            }
            QIconSet *is = new QIconSet(pict);
            m_icons.push_back(is);
            sd.icon	 = is;
            unsigned index = (unsigned)(-1);
            for (index = 0;; index++){
                const smile *s = defaultSmiles(index);
                if (s == NULL)
                    break;
#if QT_VERSION < 300
                QString exp = s->exp;
                bool bMatch = false;
                while (!exp.isEmpty()){
                    QString e = getToken(exp, '|', false);
                    QRegExp re(e);
                    int len;
                    if ((re.match(sd.paste.c_str(), 0, &len) == 0) && (len == (int)(sd.paste.length()))){
                        bMatch = true;
                        break;
                    }
                }
                if (bMatch){
                    sd.title = s->title;
                    break;
                }
#else
                QRegExp re(s->exp);
                int len;
                if ((re.match(sd.paste.c_str(), 0, &len) == 0) && ((unsigned)len == sd.paste.length())){
                    sd.title = s->title;
                    break;
                }
#endif
            }
            if (index < 16){
                m_smiles[index] = sd;
            }else{
                m_smiles.push_back(sd);
            }
        }
        return true;
    }
#endif
#ifdef WIN32
    fname = fname.replace(QRegExp("\\"), "/");
#endif
    int pos = fname.findRev("/");
    if (pos >= 0){
        fname = fname.left(pos + 1);
    }else{
        fname = "";
    }
    string s;
    QRegExp start("^ *Smiley *= *");
    QRegExp num("^ *, *-[0-9]+ *, *");
    QRegExp nn("[0-9]+");
    QRegExp re("\\[\\]\\|\\(\\)\\{\\}\\.\\?\\*\\+");
    while (getLine(f, s)){
        QString line = QString::fromLocal8Bit(s.c_str());
        if (line[0] == ';')
            continue;
        int size;
        int pos = start.match(line, 0, &size);
        if (pos < 0)
            continue;
        line = line.mid(size);
        getToken(line, '\"');
        QString dll = getToken(line, '\"', false);
        if (dll.isEmpty())
            continue;
        dll = dll.replace(QRegExp("\\\\"), "/");
        pos = num.match(line, 0, &size);
        if (pos < 0)
            continue;
        QString num = line.left(size);
        line = line.mid(size);
        pos = nn.match(num, 0, &size);
        unsigned nIcon = num.mid(pos, size).toUInt();
        getToken(line, '\"');
        QString pattern = getToken(line, '\"', false);
        getToken(line, '\"');
        QString tip = getToken(line, '\"', false);
        QString dllName = fname + dll;
        dllName = dllName.replace(QRegExp("/\\./"), "/");
        string fn;
        fn = dllName.utf8();
        ICONS_MAP::iterator it = icons.find(fn.c_str());
        IconDLL *icon_dll = NULL;
        if (it == icons.end()){
            icon_dll = new IconDLL;
            if (!icon_dll->load(fn.c_str())){
                delete icon_dll;
                icon_dll = NULL;
            }
            icons.insert(ICONS_MAP::value_type(fn.c_str(), icon_dll));
        }else{
            icon_dll = (*it).second;
        }
        if (icon_dll == NULL)
            continue;
        const QIconSet *icon = icon_dll->get(nIcon);
        if (icon == NULL){
            log(L_DEBUG, "Icon empty %u", nIcon);
            continue;
        }
        QString p;
        QString paste;
        unsigned index = (unsigned)(-1);
        while (!pattern.isEmpty()){
            QString pat = getToken(pattern, ' ', false);
            if (index == (unsigned)(-1)){
                for (index = 0; index < 16; index++){
                    const smile *s = defaultSmiles(index);
                    if (pat == s->paste)
                        break;
                }
            }
            if (paste.isEmpty())
                paste = pat;
            QString res;
            while (!pat.isEmpty()){
                int pos = re.match(pat);
                if (pos < 0)
                    break;
                res += pat.left(pos);
                res += "\\";
                res += pat.mid(pos, 1);
                pat = pat.mid(pos + 1);
            }
            res += pat;
            if (!p.isEmpty())
                p += "|";
            p += res;
        }
        if (tip.isEmpty())
            tip = paste;
        SmileDef sd;
        sd.exp     = p.latin1();
        sd.paste   = paste.latin1();
        sd.title   = tip.latin1();
        sd.icon    = icon;
        if (index < 16){
            m_smiles[index] = sd;
        }else{
            m_smiles.push_back(sd);
        }
    }
    return true;
}
Eigen::Matrix3f build_random_rotation(DOF6::Rotationf &rot, const int N=2, const float noise=0.f) {
  Eigen::Vector3f n, nn, v,n2;
  float a = M_PI*(rand()%1000)/1000.f; //angle

  v(0)=0.01f;v(2)=v(1)=1.f;     //"some" plane

  nn(0) = (rand()%1000)/1000.f;
  nn(1) = (rand()%1000)/1000.f;
  nn(2) = (rand()%1000)/1000.f;
  nn.normalize();

  Eigen::AngleAxisf aa(a,nn);

  std::cout<<"rot angle\n"<<a<<"\n";
  std::cout<<"rot axis\n"<<nn<<"\n";

  std::vector<Eigen::Vector3f> normal;
  std::vector<Eigen::Vector3f> normal2;
  for(int j=0; j<std::max(2,N); j++) {
    //seconds
    n(0) = (rand()%1000)/1000.f-0.5f;        //init normals
    n(1) = (rand()%1000)/1000.f-0.5f;
    n(2) = (rand()%1000)/1000.f-0.5f;
    n.normalize();

    n2 = aa.toRotationMatrix()*n;
    n2(0) += 2*noise*((rand()%1000)/1000.f-0.5f);        //add noise
    n2(1) += 2*noise*((rand()%1000)/1000.f-0.5f);
    n2(2) += 2*noise*((rand()%1000)/1000.f-0.5f);
    n2.normalize();

    normal.push_back(n);
    normal2.push_back(n2);
  }

  for(size_t i=0; i<normal.size(); i++) {
    rot.add1(normal[i],normal2[i],v);
  }

  rot.finish1();

  pcl::RotationFromCorrespondences rfc;
  pcl::TransformationFromCorrespondences tfc;
  for(size_t i=0; i<normal.size(); i++) {
    rot.add2(normal[i],normal2[i],1);
    // rfc.add(normal[i],normal2[i]);
    tfc.add(normal[i],normal2[i]);
  }

  static float dis1=0,dis2=0,dis3=0;
  float d1=0,d2=0,d3=0;
  d1=MATRIX_DISTANCE(rfc.getTransformation(),aa.toRotationMatrix());
  d2=MATRIX_DISTANCE(rot.toRotationMatrix(),aa.toRotationMatrix());
  Eigen::Matrix3f M=tfc.getTransformation().matrix().topLeftCorner<3,3>();
  d3=MATRIX_DISTANCE(M,aa.toRotationMatrix());
  dis1+=d1;
  dis2+=d2;
  dis3+=d3;
  std::cout<<"rfc: "<<d1<<"\n";
  std::cout<<"rot: "<<d2<<"\n";
  std::cout<<"tfc: "<<d3<<"\n";
  std::cout<<"dis1: "<<dis1<<"\n";
  std::cout<<"dis2: "<<dis2<<"\n";
  std::cout<<"dis3: "<<dis3<<"\n";

  return aa.toRotationMatrix();
}
void mexFunction(int nout, mxArray *pout[], int nin, const mxArray *pin[]) {
  FILE *f = fopen("log.txt", "wt");
  fclose(f);

  if (nin < 2) { mexErrMsgTxt("nnmex called with < 2 input arguments"); }

  const mxArray *A = pin[0], *B = pin[1];
  const mxArray *ANN_PREV = NULL, *ANN_WINDOW = NULL, *AWINSIZE = NULL;
  int aw = -1, ah = -1, bw = -1, bh = -1;
  BITMAP *a = NULL, *b = NULL, *ann_prev = NULL, *ann_window = NULL, *awinsize = NULL;
  VECBITMAP<unsigned char> *ab = NULL, *bb = NULL;
  VECBITMAP<float> *af = NULL, *bf = NULL;

  if (mxGetNumberOfDimensions(A) != 3 || mxGetNumberOfDimensions(B) != 3) { mexErrMsgTxt("dims != 3"); }
  if (mxGetDimensions(A)[2] != mxGetDimensions(B)[2]) { mexErrMsgTxt("3rd dimension not same size"); }

  int mode = MODE_IMAGE;
  if (mxGetDimensions(A)[2] != 3) { // a discriptor rather than rgb
    if (mxIsUint8(A) && mxIsUint8(B)) { mode = MODE_VECB; } 
    else if (is_float(A) && is_float(B)) { mode = MODE_VECF; } 
    else { mexErrMsgTxt("input not uint8, single, or double"); }
  }

  Params *p = new Params();
  RecomposeParams *rp = new RecomposeParams();
  BITMAP *borig = NULL;
  if (mode == MODE_IMAGE) {
    a = convert_bitmap(A);
    b = convert_bitmap(B);
	borig = b;
    aw = a->w; ah = a->h;
    bw = b->w; bh = b->h;
  } 
  else if (mode == MODE_VECB) {
    ab = convert_vecbitmap<unsigned char>(A);
    bb = convert_vecbitmap<unsigned char>(B);
    if (ab->n != bb->n) { mexErrMsgTxt("3rd dimension differs"); }
    aw = ab->w; ah = ab->h;
    bw = bb->w; bh = bb->h;
    p->vec_len = ab->n;
  } 
  else if (mode == MODE_VECF) {
    af = convert_vecbitmap<float>(A);
    bf = convert_vecbitmap<float>(B);
    if (af->n != bf->n) { mexErrMsgTxt("3rd dimension differs"); }
    aw = af->w; ah = af->h;
    bw = bf->w; bh = bf->h;
    p->vec_len = af->n;
  }

  double *win_size = NULL;
  BITMAP *amask = NULL, *bmask = NULL;

  double scalemin = 0.5, scalemax = 2.0;  // The product of these must be one.
  /* parse parameters */
  int i = 2;
  int sim_mode = 0;
  int knn_chosen = -1;
  p->algo = ALGO_CPU;
  int enrich_mode = 0;
  if (nin > i && !mxIsEmpty(pin[i])) {
    if (mxStringEquals(pin[i], "cpu")) { p->algo = ALGO_CPU; }
    else if (mxStringEquals(pin[i], "gpucpu")) { p->algo = ALGO_GPUCPU; }
    else if (mxStringEquals(pin[i], "cputiled")) { p->algo = ALGO_CPUTILED; }
    else if (mxStringEquals(pin[i], "rotscale")) { sim_mode = 1; }
    else if (mxStringEquals(pin[i], "enrich")) { p->algo = ALGO_CPUTILED; enrich_mode = 1; }
    else { mexErrMsgTxt("Unknown algorithm"); }
  } i++;
  if (nin > i && !mxIsEmpty(pin[i])) { p->patch_w = int(mxGetScalar(pin[i])); } i++;
  if (nin > i && !mxIsEmpty(pin[i])) { p->nn_iters = int(mxGetScalar(pin[i])); } i++;
  if (nin > i && !mxIsEmpty(pin[i])) { p->rs_max = int(mxGetScalar(pin[i])); } i++;
  if (nin > i && !mxIsEmpty(pin[i])) { p->rs_min = int(mxGetScalar(pin[i])); } i++;
  if (nin > i && !mxIsEmpty(pin[i])) { p->rs_ratio = mxGetScalar(pin[i]); } i++;
  if (nin > i && !mxIsEmpty(pin[i])) { p->rs_iters = mxGetScalar(pin[i]); } i++;
  if (nin > i && !mxIsEmpty(pin[i])) { p->cores = int(mxGetScalar(pin[i])); } i++;
  if (nin > i && !mxIsEmpty(pin[i])) { bmask = convert_bitmap(pin[i]); } i++; // XC+
  if (nin > i && !mxIsEmpty(pin[i])) { 
    if (!mxIsDouble(pin[i])) { mexErrMsgTxt("\nwin_size should be of type double."); }
    win_size = (double*)mxGetData(pin[i]);
    if (mxGetNumberOfElements(pin[i])==1) { p->window_h = p->window_w = int(win_size[0]); }
    else if (mxGetNumberOfElements(pin[i])==2) { p->window_h = int(win_size[0]); p->window_w = int(win_size[1]); }
    else { mexErrMsgTxt("\nwin_size should be a scalar for square window or [h w] for a rectangular one."); }
  } i++;
  /* continue parsing parameters */
  // [ann_prev=NULL], [ann_window=NULL], [awinsize=NULL], 
  if (nin > i && !mxIsEmpty(pin[i])) { 
    ANN_PREV = pin[i];
    int clip_count = 0;
    ann_prev = convert_field(p, ANN_PREV, bw, bh, clip_count);       // Bug fixed by Connelly
  } i++;
  if (nin > i && !mxIsEmpty(pin[i])) { 
    ANN_WINDOW = pin[i];
    int clip_count = 0;
    ann_window = convert_field(p, ANN_WINDOW, bw, bh, clip_count);      
  } i++;
  if (nin > i && !mxIsEmpty(pin[i])) { 
    AWINSIZE = pin[i];
    awinsize = convert_winsize_field(p, AWINSIZE, aw, ah);  
    if (p->window_w==INT_MAX||p->window_h==INT_MAX) { p->window_w = -1; p->window_h = -1; }
  } i++;
  if (nin > i && !mxIsEmpty(pin[i])) { 
    knn_chosen = int(mxGetScalar(pin[i]));
    if (knn_chosen == 1) { knn_chosen = -1; }
    if (knn_chosen <= 0) { mexErrMsgTxt("\nknn is less than zero"); }
  } i++;
  if (nin > i && !mxIsEmpty(pin[i])) { 
    scalemax = mxGetScalar(pin[i]);
    if (scalemax <= 0) { mexErrMsgTxt("\nscalerange is less than zero"); }
    scalemin = 1.0/scalemax;
    if (scalemax < scalemin) {
      double temp = scalemax;
      scalemax = scalemin;
      scalemin = temp;
    }
  } i++;

  if (ann_window&&!awinsize&&!win_size) {
    mexErrMsgTxt("\nUsing ann_window - either awinsize or win_size should be defined.\n");
  }

  if (enrich_mode) {
	int nn_iters = p->nn_iters;
    p->enrich_iters = nn_iters/2;
	p->nn_iters = 2;
	if (A != B) { mexErrMsgTxt("\nOur implementation of enrichment requires that image A = image B.\n"); }
	if (mode == MODE_IMAGE) {
	  b = a;
	} else {
	  mexErrMsgTxt("\nEnrichment only implemented for 3 channel uint8 inputs.\n");
	}
  }

  init_params(p);
  if (sim_mode) {
    init_xform_tables(scalemin, scalemax, 1);
  }
  
  RegionMasks *amaskm = amask ? new RegionMasks(p, amask): NULL;

  BITMAP *ann = NULL; // NN field
  BITMAP *annd_final = NULL; // NN patch distance field
  BITMAP *ann_sim_final = NULL;

  VBMP *vann_sim = NULL;
  VBMP *vann = NULL;
  VBMP *vannd = NULL;

  if (mode == MODE_IMAGE) {
    // input as RGB image
    if (!a || !b) { mexErrMsgTxt("internal error: no a or b image"); }
    if (knn_chosen > 1) {
      p->knn = knn_chosen;
      if (sim_mode) { mexErrMsgTxt("rotating+scaling patches not implemented with knn (actually it is implemented it is not exposed by the wrapper)"); }
      PRINCIPAL_ANGLE *pa = NULL;
      vann_sim = NULL;
      vann = knn_init_nn(p, a, b, vann_sim, pa);
      vannd = knn_init_dist(p, a, b, vann, vann_sim);
      knn(p, a, b, vann, vann_sim, vannd, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, pa);
//      sort_knn(p, vann, vann_sim, vannd);
    } else if (sim_mode) {
      BITMAP *ann_sim = NULL;
      ann = sim_init_nn(p, a, b, ann_sim);
      BITMAP *annd = sim_init_dist(p, a, b, ann, ann_sim);
      sim_nn(p, a, b, ann, ann_sim, annd);
      if (ann_prev) { mexErrMsgTxt("when searching over rotations+scales, previous guess is not supported"); }
      annd_final = annd;
      ann_sim_final = ann_sim;
    } else {
      ann = init_nn(p, a, b, bmask, NULL, amaskm, 1, ann_window, awinsize);
      BITMAP *annd = init_dist(p, a, b, ann, bmask, NULL, amaskm);
      nn(p, a, b, ann, annd, amaskm, bmask, 0, 0, rp, 0, 0, 0, NULL, p->cores, ann_window, awinsize); 
      if (ann_prev) minnn(p, a, b, ann, annd, ann_prev, bmask, 0, 0, rp, NULL, amaskm, p->cores);  
      annd_final = annd;
    }
  } 
/*
  else if (mode == MODE_VECB) {
//    mexPrintf("mode vecb %dx%dx%d, %dx%dx%d\n", ab->w, ab->h, ab->n, bb->w, bb->h, bb->n);
//    mexPrintf("  %d %d %d %d\n", ab->get(0,0)[0], ab->get(1,0)[0], ab->get(0,1)[0], ab->get(0,0)[1]);
    if (!ab || !bb) { mexErrMsgTxt("internal error: no a or b image"); }
    ann = vec_init_nn<unsigned char>(p, ab, bb, bmask, NULL, amaskm);
    VECBITMAP<int> *annd = vec_init_dist<unsigned char, int>(p, ab, bb, ann, bmask, NULL, amaskm);
//    mexPrintf("  %d %d %d %p %p\n", annd->get(0,0)[0], annd->get(1,0)[0], annd->get(0,1)[0], amaskm, bmask);
    vec_nn<unsigned char, int>(p, ab, bb, ann, annd, amaskm, bmask, 0, 0, rp, 0, 0, 0, NULL, p->cores); 
    if (ann_prev) vec_minnn<unsigned char, int>(p, ab, bb, ann, annd, ann_prev, bmask, 0, 0, rp, NULL, amaskm, p->cores);  
    annd_final = vecbitmap_to_bitmap(annd);
    delete annd;
  } 
  else if (mode == MODE_VECF) {
//    mexPrintf("mode vecf %dx%dx%d, %dx%dx%d\n", af->w, af->h, af->n, bf->w, bf->h, bf->n);
//    mexPrintf("  %f %f %f %f\n", af->get(0,0)[0], af->get(1,0)[0], af->get(0,1)[0], af->get(0,0)[1]);
    if (!af || !bf) { mexErrMsgTxt("internal error: no a or b image"); }
    ann = vec_init_nn<float>(p, af, bf, bmask, NULL, amaskm);
    VECBITMAP<float> *annd = vec_init_dist<float, float>(p, af, bf, ann, bmask, NULL, amaskm);
    vec_nn<float, float>(p, af, bf, ann, annd, amaskm, bmask, 0, 0, rp, 0, 0, 0, NULL, p->cores); 
    if (ann_prev) vec_minnn<float, float>(p, af, bf, ann, annd, ann_prev, bmask, 0, 0, rp, NULL, amaskm, p->cores);  
    annd_final = create_bitmap(annd->w, annd->h);
    clear(annd_final);
    delete annd;
  }
*/
  else if(mode == MODE_VECB) {
    if (sim_mode) { mexErrMsgTxt("internal error: rotation+scales not implemented with descriptor mode"); }
    if (knn_chosen > 1) { mexErrMsgTxt("internal error: kNN not implemented with descriptor mode"); }
//    mexPrintf("mode vecb_xc %dx%dx%d, %dx%dx%d\n", ab->w, ab->h, ab->n, bb->w, bb->h, bb->n);
    // input as uint8 discriptors per pixel
    if (!ab || !bb) { mexErrMsgTxt("internal error: no a or b image"); }
    ann = XCvec_init_nn<unsigned char>(p, ab, bb, bmask, NULL, amaskm);
    VECBITMAP<int> *annd = XCvec_init_dist<unsigned char, int>(p, ab, bb, ann, bmask, NULL, amaskm);
    XCvec_nn<unsigned char, int>(p, ab, bb, ann, annd, amaskm, bmask, 0, 0, rp, 0, 0, 0, NULL, p->cores); 
    if (ann_prev) XCvec_minnn<unsigned char, int>(p, ab, bb, ann, annd, ann_prev, bmask, 0, 0, rp, NULL, amaskm, p->cores);  
    annd_final = vecbitmap_to_bitmap(annd);
    delete annd;
  } else if(mode == MODE_VECF) {
    if (sim_mode) { mexErrMsgTxt("internal error: rotation+scales not implemented with descriptor mode"); }
    if (knn_chosen > 1) { mexErrMsgTxt("internal error: kNN not implemented with descriptor mode"); }
    // input as float/double discriptors per pixel
    if (!af || !bf) { mexErrMsgTxt("internal error: no a or b image"); }
    ann = XCvec_init_nn<float>(p, af, bf, bmask, NULL, amaskm);
    VECBITMAP<float> *annd = XCvec_init_dist<float, float>(p, af, bf, ann, bmask, NULL, amaskm);
    XCvec_nn<float, float>(p, af, bf, ann, annd, amaskm, bmask, 0, 0, rp, 0, 0, 0, NULL, p->cores); 
    if (ann_prev) XCvec_minnn<float, float>(p, af, bf, ann, annd, ann_prev, bmask, 0, 0, rp, NULL, amaskm, p->cores);  
    annd_final = create_bitmap(annd->w, annd->h);
    clear(annd_final);
    delete annd;	
  }

  destroy_region_masks(amaskm);

  // output ann: x | y | patch_distance
  if(nout >= 1) {
    mxArray *ans = NULL;
    if (knn_chosen > 1) {
      if (sim_mode) { mexErrMsgTxt("rotating+scaling patches return value not implemented with knn"); }
      mwSize dims[4] = { ah, aw, 3, knn_chosen };
      ans = mxCreateNumericArray(4, dims, mxINT32_CLASS, mxREAL);
      int *data = (int *) mxGetData(ans);
      for (int kval = 0; kval < knn_chosen; kval++) {
        int *xchan = &data[aw*ah*3*kval+0];
        int *ychan = &data[aw*ah*3*kval+aw*ah];
        int *dchan = &data[aw*ah*3*kval+2*aw*ah];
        for (int y = 0; y < ah; y++) {
//          int *ann_row = (int *) ann->line[y];
//          int *annd_row = (int *) annd_final->line[y];
          for (int x = 0; x < aw; x++) {
//            int pp = ann_row[x];
            int pp = vann->get(x, y)[kval];
            int pos = y + x * ah;
            xchan[pos] = INT_TO_X(pp);
            ychan[pos] = INT_TO_Y(pp);
            dchan[pos] = vannd->get(x, y)[kval];
          }
        }
      }
    } else if (ann_sim_final) {
      mwSize dims[3] = { ah, aw, 5 };
      ans = mxCreateNumericArray(3, dims, mxSINGLE_CLASS, mxREAL);
      float *data = (float *) mxGetData(ans);
      float *xchan = &data[0];
      float *ychan = &data[aw*ah];
      float *dchan = &data[2*aw*ah];
      float *tchan = &data[3*aw*ah];
      float *schan = &data[4*aw*ah];
      double angle_scale = 2.0*M_PI/NUM_ANGLES;
      for (int y = 0; y < ah; y++) {
        int *ann_row = (int *) ann->line[y];
        int *annd_row = (int *) annd_final->line[y];
        int *ann_sim_row = ann_sim_final ? (int *) ann_sim_final->line[y]: NULL;
        for (int x = 0; x < aw; x++) {
          int pp = ann_row[x];
          int pos = y + x * ah;
          xchan[pos] = INT_TO_X(pp);
          ychan[pos] = INT_TO_Y(pp);
          dchan[pos] = annd_row[x];
          if (ann_sim_final) {
            int v = ann_sim_row[x];
            int tval = INT_TO_Y(v)&(NUM_ANGLES-1);
            int sval = INT_TO_X(v);

            tchan[pos] = tval*angle_scale;
            schan[pos] = xform_scale_table[sval]*(1.0/65536.0);
          }
        }
      }
    } else {
      mwSize dims[3] = { ah, aw, 3 };
      ans = mxCreateNumericArray(3, dims, mxINT32_CLASS, mxREAL);
      int *data = (int *) mxGetData(ans);
      int *xchan = &data[0];
      int *ychan = &data[aw*ah];
      int *dchan = &data[2*aw*ah];
      for (int y = 0; y < ah; y++) {
        int *ann_row = (int *) ann->line[y];
        int *annd_row = (int *) annd_final->line[y];
        for (int x = 0; x < aw; x++) {
          int pp = ann_row[x];
          int pos = y + x * ah;
          xchan[pos] = INT_TO_X(pp);
          ychan[pos] = INT_TO_Y(pp);
          dchan[pos] = annd_row[x];
        }
      }
    }
    pout[0] = ans;
  }

  // clean up
  delete vann;
  delete vann_sim;
  delete vannd;
  delete p;
  delete rp;
  destroy_bitmap(a);
  destroy_bitmap(borig);
  delete ab;
  delete bb;
  delete af;
  delete bf;
  destroy_bitmap(ann);
  destroy_bitmap(annd_final);
  destroy_bitmap(ann_sim_final);
  if (ann_prev) destroy_bitmap(ann_prev);
  if (ann_window) destroy_bitmap(ann_window);
  if (awinsize) destroy_bitmap(awinsize);
}
Exemple #10
0
void visit(const Dumper &dumper, std::ostream &os,
    std::unordered_set<const Node *> &visited, const Node &node) {
  if (visited.count(&node)) return;
  visited.insert(&node);
  os << nn(node) << ' ' << Streamer(dumper.nodeStyle(node)) << ";\n";
  const auto &cs = node.allChildren();
  for (auto it = cs.cbegin(); it != cs.cend(); ++it) {
    const auto &child = *it->second;
    os << nn(node) << "->" << nn(child) << ' '
        << Streamer(dumper.edgeStyle(it->first)) << ";\n";
    visit(dumper, os, visited, child);
  }

  const auto &ts = node.tallies<AndTally>();
  for (auto it = ts.cbegin(); it != ts.cend(); ++it) {
    const auto &tally = **it;
    os << nn(node) << "->" << nn(tally) << ' '
        << Streamer(dumper.tallyEdgeStyle()) << ";\n";
    if (!visited.count(&tally.node())) {
      os << nn(tally) << ' ' << Streamer(dumper.tallyStyle(tally)) << ";\n";
      os << nn(tally) << "->" << nn(tally.node()) << ' '
          << Streamer(dumper.tallyEdgeStyle()) << ";\n";
      visit(dumper, os, visited, tally.node());
    }
  }
  const auto &ts2 = node.tallies<OrTally>();
  for (auto it = ts2.cbegin(); it != ts2.cend(); ++it) {
    const auto &tally = **it;
    os << nn(node) << "->" << nn(tally) << ' '
        << Streamer(dumper.tallyEdgeStyle()) << ";\n";
    if (!visited.count(&tally.node())) {
      os << nn(tally) << ' ' << Streamer(dumper.tallyStyle(tally)) << ";\n";
      os << nn(tally) << "->" << nn(tally.node()) << ' '
          << Streamer(dumper.tallyEdgeStyle()) << ";\n";
      visit(dumper, os, visited, tally.node());
    }
  }
}
Exemple #11
0
void vlad_compute(int k, int d, const float *centroids, int n, const float *v,int flags, float *desc) 
{

	int i,j,l,n_quantile,i0,i1,ai,a,ma,ni;
	int *perm ;
	float un , diff;
	float *tab,*u,*avg,*sum,*mom2,*dists;
	int *hist,*assign;


	if(flags<11 || flags>=13) 
	{
		assign=ivec_new(n);

		nn(n,k,d,centroids,v,assign,NULL,NULL);    

		if(flags==6 || flags==7) 
		{
			n_quantile = flags==6 ? 3 : 1;
			fvec_0(desc,k*d*n_quantile);
			perm      = ivec_new(n);
			tab       = fvec_new(n);
			ivec_sort_index(assign,n,perm);
			i0=0;
			for(i=0;i<k;i++) 
			{
				i1=i0;
				while(i1<n && assign[perm[i1]]==i) 
				{
					i1++;
				}

				if(i1==i0) continue;

				for(j=0;j<d;j++) 
				{        
					for(l=i0;l<i1;l++)
					{
						tab[l-i0]=v[perm[l]*d+j];
					}
					ni=i1-i0;
					fvec_sort(tab,ni);
					for(l=0;l<n_quantile;l++) 
					{
						desc[(i*d+j)*n_quantile+l]=(tab[(l*ni+ni/2)/n_quantile]-centroids[i*d+j])*ni;
					}
				}

				i0=i1;
			}
			free(perm);
			free(tab);
		} 
		else if(flags==5) 
		{
			fvec_0(desc,k*d);

			for(i=0;i<n;i++) 
			{
				for(j=0;j<d;j++) 
				{
					desc[assign[i]*d+j]+=v[i*d+j];
				}
			}

		} 
		else if(flags==8 || flags==9) 
		{
			fvec_0(desc,k*d);

			u   = fvec_new(d);

			for(i=0;i<n;i++) 
			{
				fvec_cpy(u,v+i*d,d);
				fvec_sub(u,centroids+assign[i]*d,d);
				un=(float)sqrt(fvec_norm2sqr(u,d));

				if(un==0) continue;
				if(flags==8) 
				{        
					fvec_div_by(u,d,un);
				} else if(flags==9) 
				{
					fvec_div_by(u,d,sqrt(un));
				}

				fvec_add(desc+assign[i]*d,u,d);

			}
			free(u);
		} 
		else if(flags==10) 
		{
			fvec_0(desc,k*d);

			for(i=0;i<n;i++) 
			{
				for(j=0;j<d;j++) 
				{
					desc[assign[i]*d+j]+=v[i*d+j];
				}
			}

			for(i=0;i<k;i++) 
			{
				fvec_normalize(desc+i*d,d,2.0);  
			}

		} 
		else if(flags==13) 
		{

			fvec_0(desc,k*d);

			for(i=0;i<n;i++) 
			{
				for(j=0;j<d;j++) 
				{
					desc[assign[i]*d+j]+=(float)sqr(v[i*d+j]-centroids[assign[i]*d+j]);
				}
			}     

		} 
		else if(flags==14) 
		{
			avg = fvec_new_0(k*d);

			for(i=0;i<n;i++) 
			{
				for(j=0;j<d;j++) 
				{
					avg[assign[i]*d+j]+=v[i*d+j]-centroids[assign[i]*d+j];
				}
			}

			hist=ivec_new_histogram(k,assign,n);

			for(i=0;i<k;i++) 
			{
				if(hist[i]>0) 
				{
					for(j=0;j<d;j++) 
					{
						avg[i*d+j]/=hist[i];
					}
				}
			}

			free(hist);

			fvec_0(desc,k*d);
			for(i=0;i<n;i++) 
			{
				for(j=0;j<d;j++) 
				{
					desc[assign[i]*d+j]+=(float)(sqr(v[i*d+j]-centroids[assign[i]*d+j]-avg[assign[i]*d+j]));
				}
			}

			fvec_sqrt(desc,k*d);

			free(avg);
		}  
		else if(flags==15) 
		{
			fvec_0(desc,k*d*2);
			sum = desc;

			for(i=0;i<n;i++) 
			{
				for(j=0;j<d;j++) 
				{
					sum[assign[i]*d+j]+=v[i*d+j]-centroids[assign[i]*d+j];
				}
			}

			hist = ivec_new_histogram(k,assign,n);

			mom2 = desc+k*d;

			for(i=0;i<n;i++) 
			{
				ai=assign[i];
				for(j=0;j<d;j++) 
				{
					mom2[ai*d+j]+=(float)(sqr(v[i*d+j]-centroids[ai*d+j]-sum[ai*d+j]/hist[ai]));
				}
			}
			fvec_sqrt(mom2,k*d);
			free(hist);


		} 
		else if(flags==17) 
		{
			fvec_0(desc,k*d*2);

			for(i=0;i<n;i++) 
			{
				for(j=0;j<d;j++) 
				{
					diff=v[i*d+j]-centroids[assign[i]*d+j];
					if(diff>0)
					{
						desc[assign[i]*d+j]+=diff;
					}
					else 
					{
						desc[assign[i]*d+j+k*d]-=diff;
					}
				}
			}

		} 
		else 
		{
			fvec_0(desc,k*d);

			for(i=0;i<n;i++) 
			{
				for(j=0;j<d;j++) 
				{
					desc[assign[i]*d+j]+=v[i*d+j]-centroids[assign[i]*d+j];
				}
			}


			if(flags==1) 
			{
				hist=ivec_new_histogram(k,assign,n);
				/* printf("unbalance factor=%g\n",ivec_unbalanced_factor(hist,k)); */

				for(i=0;i<k;i++) 
				{
					for(j=0;j<d;j++) 
					{
						desc[i*d+j]/=hist[i];    
					}
				}
				free(hist);
			}

			if(flags==2) 
			{
				for(i=0;i<k;i++) 
				{
					fvec_normalize(desc+i*d,d,2.0);
				}
			}

			if(flags==3 || flags==4) 
			{
				assert(!"not implemented");
			}

			if(flags==16) 
			{
				hist=ivec_new_histogram(k,assign,n);
				for(i=0;i<k;i++) 
				{
					if(hist[i]>0) 
					{
						fvec_norm(desc+i*d,d,2);
						fvec_mul_by(desc+i*d,d,sqrt(hist[i]));
					}
				}
				free(hist);
			}


		}
		free(assign);
	} 
	else if(flags==11 || flags==12) 
	{
		ma=flags==11 ? 4 : 2;
		assign=ivec_new(n*ma);

		dists=knn(n,k,d,ma,centroids,v,assign,NULL,NULL);    

		fvec_0(desc,k*d);

		for(i=0;i<n;i++) 
		{
			for(j=0;j<d;j++) 
			{
				for(a=0;a<ma;a++) 
				{
					desc[assign[ma*i+a]*d+j]+=v[i*d+j]-centroids[assign[ma*i+a]*d+j];
				}
			}
		} 

		free(dists);

		free(assign);
	}

}
Exemple #12
0
int
LSpace :: computeLoadLSToLRotationMatrix(FloatMatrix &answer, int isurf, GaussPoint *gp)
{
    // returns transformation matrix from
    // surface local coordinate system
    // to element local c.s
    // (same as global c.s in this case)
    //
    // i.e. f(element local) = T * f(edge local)

    // definition of local c.s on surface:
    // local z axis - perpendicular to surface, pointing outwards from element
    // local x axis - is in global xy plane (perpendicular to global z axis)
    // local y axis - completes the righ hand side cs.

    /*
     * OOFEM_ERROR("surface local coordinate system not supported");
     * return 1;
     */
    FloatArray gc(3);
    FloatArray h1(3), h2(3), nn(3), n(3);
    IntArray snodes(4);

    answer.resize(3, 3);

    this->interpolation.computeSurfaceMapping(snodes, dofManArray, isurf);
    for ( int i = 1; i <= 4; i++ ) {
        gc.add( * domain->giveNode( snodes.at(i) )->giveCoordinates() );
    }

    gc.times(1. / 4.);
    // determine "average normal"
    for ( int i = 1; i <= 4; i++ ) {
        int j = ( i ) % 4 + 1;
        h1 = * domain->giveNode( snodes.at(i) )->giveCoordinates();
        h1.subtract(gc);
        h2 = * domain->giveNode( snodes.at(j) )->giveCoordinates();
        h2.subtract(gc);
        n.beVectorProductOf(h1, h2);
        if ( n.computeSquaredNorm() > 1.e-6 ) {
            n.normalize();
        }

        nn.add(n);
    }

    nn.times(1. / 4.);
    if ( nn.computeSquaredNorm() < 1.e-6 ) {
        answer.zero();
        return 1;
    }

    nn.normalize();
    for ( int i = 1; i <= 3; i++ ) {
        answer.at(i, 3) = nn.at(i);
    }

    // determine lcs of surface
    // local x axis in xy plane
    double test = fabs(fabs( nn.at(3) ) - 1.0);
    if ( test < 1.e-5 ) {
        h1.at(1) = answer.at(1, 1) = 1.0;
        h1.at(2) = answer.at(2, 1) = 0.0;
    } else {
        h1.at(1) = answer.at(1, 1) = answer.at(2, 3);
        h1.at(2) = answer.at(2, 1) = -answer.at(1, 3);
    }

    h1.at(3) = answer.at(3, 1) = 0.0;
    // local y axis perpendicular to local x,z axes
    h2.beVectorProductOf(nn, h1);
    for ( int i = 1; i <= 3; i++ ) {
        answer.at(i, 2) = h2.at(i);
    }

    return 1;
}
Exemple #13
0
void LSpace :: drawTriad(FloatArray &coords, int isurf)
{
    FloatMatrix jm(3, 3);
    FloatArray gc(3);
    GraphicObj *go;

    WCRec p [ 2 ]; // point
    double coeff = 1.0;
    int i, succ;
    /*
     * // version I
     * this->interpolation.giveJacobianMatrixAt (jm, domain, nodeArray, coords);
     * // determine origin
     * this->interpolation.local2global (gc, domain, nodeArray, coords, 0.0);
     * // draw triad
     *
     */

    // version II
    // determine surface center
    IntArray snodes(4);
    FloatArray h1(3), h2(3), nn(3), n(3);
    int j;
    const char *colors[] = {
        "red", "green", "blue"
    };


    this->interpolation.computeSurfaceMapping(snodes, dofManArray, isurf);
    for ( i = 1; i <= 4; i++ ) {
        gc.add( * ( domain->giveNode( snodes.at(i) )->giveCoordinates() ) );
    }

    gc.times(1. / 4.);
    // determine "average normal"
    nn.zero();
    for ( i = 1; i <= 4; i++ ) {
        j = ( i ) % 4 + 1;
        h1 = * domain->giveNode( snodes.at(i) )->giveCoordinates();
        h1.subtract(gc);
        h2 = * domain->giveNode( snodes.at(j) )->giveCoordinates();
        h2.subtract(gc);
        n.beVectorProductOf(h1, h2);
        if ( n.dotProduct(n, 3) > 1.e-6 ) {
            n.normalize();
        }

        nn.add(n);
    }

    nn.times(1. / 4.);
    if ( nn.dotProduct(nn, 3) < 1.e-6 ) {
        return;
    }

    nn.normalize();
    for ( i = 1; i <= 3; i++ ) {
        jm.at(i, 3) = nn.at(i);
    }

    // determine lcs of surface
    // local x axis in xy plane
    double test = fabs(fabs( nn.at(3) ) - 1.0);
    if ( test < 1.e-5 ) {
        h1.at(1) = jm.at(1, 1) = 1.0;
        h1.at(2) = jm.at(2, 1) = 0.0;
    } else {
        h1.at(1) = jm.at(1, 1) = jm.at(2, 3);
        h1.at(2) = jm.at(2, 1) = -jm.at(1, 3);
    }

    h1.at(3) = jm.at(3, 1) = 0.0;
    // local y axis perpendicular to local x,z axes
    h2.beVectorProductOf(nn, h1);
    for ( i = 1; i <= 3; i++ ) {
        jm.at(i, 2) = h2.at(i);
    }


    p [ 0 ].x = gc.at(1);
    p [ 0 ].y = gc.at(2);
    p [ 0 ].z = gc.at(3);
    for ( i = 1; i <= 3; i++ ) {
        p [ 1 ].x = p [ 0 ].x + coeff *jm.at(1, i);
        p [ 1 ].y = p [ 0 ].y + coeff *jm.at(2, i);
        p [ 1 ].z = p [ 0 ].z + coeff *jm.at(3, i);

        EASValsSetColor( ColorGetPixelFromString(const_cast< char * >(colors [ i - 1 ]), & succ) );

        go = CreateLine3D(p);
        EGWithMaskChangeAttributes(WIDTH_MASK | COLOR_MASK | LAYER_MASK, go);
        EMAddGraphicsToModel(ESIModel(), go);
    }
}
Exemple #14
0
void init(){
  
  ot.setRenderPoints(true);
  
  GRandClip r(0,100,Range64f(-500,500));

  DataSegment<float,3> ps = obj.selectXYZ();
  DataSegment<float,4> cs = obj.selectRGBA32f();
  
  std::vector<FixedColVector<int,4> > nn(1000),nnres(1000);
  for(int i=0;i<obj.getDim();++i){
    ps[i] = Vec3(r,r,r);
    cs[i] = GeomColor(0,100,255,255);
    
    if(i < (int)nn.size()){
      nn[i] = FixedColVector<int,4>(r,r,r);
    }
  }
  
  Time t = Time::now();
  for(int i=0;i<obj.getDim();++i){
    FixedColVector<float,3> p = ps[i];
    ot.insert(FixedColVector<int,4>(p[0],p[1],p[2],1));
  }
  t.showAge("insertion time");
  
  std::vector<FixedColVector<int,4> > q = ot.query(0,-500,-500,500,1000,1000);
  static PointCloudObject res(q.size(),1,false);
  ps = res.selectXYZ();
  cs = res.selectRGBA32f();
  for(size_t i=0;i<q.size();++i){
    ps[i] = Vec3(q[i][0],q[i][1],q[i][2],1);
    cs[i] = GeomColor(255,0,0,255);
  }

  PCL_PC pcl_pc(obj.getDim(),1);
  DataSegment<float,3> pcl_ps = pcl_pc.selectXYZ();
  for(int i=0;i<obj.getDim();++i){
    pcl_ps[i] = ps[i];
  }

  t = Time::now();
  PCL_OT pcl_ot(16);
  t.showAge("create pcl octree");
  static boost::shared_ptr<pcl::PointCloud<pcl::PointXYZ> > ptr(&pcl_pc.pcl());
  
  t = Time::now();
  pcl_ot.setInputCloud(ptr);

  t.showAge("set input cloud to pcl tree"); // 3 times faster!
  

  
  t = Time::now();
  for(size_t i=0;i<nn.size();++i){
    nnres[i] = ot.nn(nn[i]);
  }
  t.showAge("icl nn search");

  t = Time::now();
  for(size_t i=0;i<nn.size();++i){
    std::vector<int> dstIdx;
    std::vector<float> dstDist;
    pcl_ot.nearestKSearch(pcl::PointXYZ(nn[i][0],nn[i][1],nn[i][2]), 1, dstIdx,dstDist);
  }
  t.showAge("pcl nn search");



  t = Time::now();
  for(size_t i=0;i<nn.size();++i){
    nnres[i] = ot.nn_approx(nn[i]);
  }
  t.showAge("icl nn approx search");

#ifdef TRY_PCL_OCTREE
  t = Time::now();
  for(size_t i=0;i<nn.size();++i){
    int idx(0); float dist(0);
    pcl_ot.approxNearestSearch(pcl::PointXYZ(nn[i][0],nn[i][1],nn[i][2]), idx,dist);
  }
  t.showAge("pcl nn approx search");
#endif

  
  
  scene.addObject(&res);  
  //scene.addObject(&obj);
  scene.addObject(&ot);

  scene.addCamera(Camera(Vec(0,0,1500,1)));
  
  gui << Draw3D().handle("draw3D").minSize(32,24) << Show();
  
  gui["draw3D"].link(scene.getGLCallback(0));
  gui["draw3D"].install(scene.getMouseHandler(0));
}
Exemple #15
0
int main(int argc, char **argv)
{
    if (argc == 2 && std::string(argv[1]) == "test")
        return !test();
    if (argc < 10) {
        std::cerr << "Usage: " << argv[0] << " : "
                  << "dataset_directory rows cols hiddenNeurons outputNeurons epochs minibatchSize learningRate picsPerClass" << std::endl;
        return 1;
    }

    size_t inputLayer = atoi(argv[2]) * atoi(argv[3]);
    size_t hiddenLayer = atoi(argv[4]);
    size_t outputLayer = atoi(argv[5]);
    size_t epochs = atoi(argv[6]);
    size_t minibatchSize = atoi(argv[7]);
    double learningRate = atof(argv[8]);
    size_t maxPicsPerClass = atoi(argv[9]);

    std::cout << "==============" << std::endl;
    std::cout << "==Parameters==" << std::endl;
    std::cout << "==============" << std::endl;
    std::cout << "Number of neurons in input layer : " << atoi(argv[2]) << "x" << atoi(argv[3])
            << "=" << inputLayer << std::endl;
    std::cout << "Number of neurons in hidden layer : " << hiddenLayer << std::endl;
    std::cout << "Number of neurons in output layer : " << outputLayer << std::endl;
    std::cout << "Epochs : " << epochs << std::endl;
    std::cout << "Minibatch size : " << minibatchSize << std::endl;
    std::cout << "Learning rate : " << learningRate << std::endl;
    std::cout << "Maximum pictures per class : " << maxPicsPerClass << std::endl;


    std::cout << "===================" << std::endl;
    std::cout << "==Opening dataset==" << std::endl;
    std::cout << "===================" << std::endl;
    Network::TrainingData trainingSet = getDataSet(std::string(argv[1]), outputLayer, maxPicsPerClass);
    std::random_shuffle(trainingSet.begin(), trainingSet.end());
    Network::TrainingData testSet = slice(trainingSet, 0.9);

    std::cout << "Checking training and test inputs sanity" << std::endl;
    checkSanity(trainingSet);
    checkSanity(testSet);

    std::cout << "===========================" << std::endl;
    std::cout << "==Beginning NN processing==" << std::endl;
    std::cout << "===========================" << std::endl;

//    dumpInputs(testSet);

    /* !!!!!!!! */
    Network nn({inputLayer, hiddenLayer, outputLayer});

    nn.sgd(trainingSet, testSet, epochs, minibatchSize, learningRate);
//    nn.dump();


    //    cv::namedWindow("Display Image", cv::WINDOW_AUTOSIZE);
    //    cv::imshow("Display Image", image);

    //    cv::waitKey(0);

    std::cout << "isok" << std::endl;
    return 0;
}
Exemple #16
0
int main(int argc, char *argv[])
{
    if (argc <= 1)
    {
        std::cout << "Error: Please inform options and filename." << std::endl;
        printHelp(argv[0]);
        return 1;
    }

    std::string option = argv[1];

    if (option == "--test")
    {
        testRun();
        return 0;
    }

    if (option == "--help")
    {
        printHelp(argv[0]);
        return 0;
    }

    tsp::TSPLibData data;
    std::ifstream file(argv[2]);

    if (!file.is_open())
    {
        std::cout << "Error: cannot open file." << std::endl;
        printHelp(argv[0]);
        return 0;
    }

    data.load(file);

    if (option == "--only-constructive")
    {
        tsp::TSPNearestNeighborConstruct nn(data);
        tsp::TSPTour tour = nn.run();

        if (tour.isValid())
        {
            tour.print(std::cout);
        }
        return 0;
    }

    if (option == "--localsearch")
    {
        tsp::TSPNearestNeighborConstruct nn(data);
        tsp::TSPTour tour = nn.run();

        tsp::TSP2opt local(data, tour);
        tour = local.run();

        if (tour.isValid())
        {
            tour.print(std::cout);
        }
        return 0;
    }

    if (option == "--grasp")
    {
        tsp::TSPGrasp grasp(data, 100);
        tsp::TSPTour tourGrasp = grasp.run();

        if ( tourGrasp.isValid() )
        {
            tourGrasp.print(std::cout);
        }
        return 0;
    }

    if (option == "--tabu")
    {
        tsp::TSPTabu tabu(data, 100, 10, 10);
        tsp::TSPTour tourTabu = tabu.run();

        if ( tourTabu.isValid() )
        {
            tourTabu.print(std::cout);
        }
        return 0;
    }

    return 0;
}
Exemple #17
0
static int
check_name(char *s)
{	int i;

	yylval = nn(ZN, 0, ZN, ZN);

	if (ltl_mode)
	{	for (i = 0; LTL_syms[i].s; i++)
		{	if (strcmp(s, LTL_syms[i].s) == 0)
			{	return LTL_syms[i].tok;
	}	}	}

	for (i = 0; Names[i].s; i++)
	{	if (strcmp(s, Names[i].s) == 0)
		{	yylval->val = Names[i].val;
			if (Names[i].sym)
				yylval->sym = lookup(Names[i].sym);
			if (Names[i].tok == IN && !in_for)
			{	continue;
			}
			return Names[i].tok;
	}	}

	if ((yylval->val = ismtype(s)) != 0)
	{	yylval->ismtyp = 1;
		return CONST;
	}

	if (strcmp(s, "_last") == 0)
		has_last++;

	if (strcmp(s, "_priority") == 0)
		has_priority++;

	if (Inlining >= 0 && !ReDiRect)
	{	Lextok *tt, *t = Inline_stub[Inlining]->params;

		for (i = 0; t; t = t->rgt, i++)				/* formal pars */
		 if (!strcmp(s, t->lft->sym->name)			/* varname matches formal */
		 &&   strcmp(s, Inline_stub[Inlining]->anms[i]) != 0)	/* actual pars */
		 {
#if 0
			if (verbose&32)
			printf("\tline %d, replace %s in call of '%s' with %s\n",
				lineno, s,
				Inline_stub[Inlining]->nm->name,
				Inline_stub[Inlining]->anms[i]);
#endif
			for (tt = Inline_stub[Inlining]->params; tt; tt = tt->rgt)
				if (!strcmp(Inline_stub[Inlining]->anms[i],
					tt->lft->sym->name))
				{	/* would be cyclic if not caught */
					printf("spin: %s:%d replacement value: %s\n",
						oFname->name?oFname->name:"--", lineno, tt->lft->sym->name);
					fatal("formal par of %s contains replacement value",
						Inline_stub[Inlining]->nm->name);
					yylval->ntyp = tt->lft->ntyp;
					yylval->sym = lookup(tt->lft->sym->name);
					return NAME;
				}

			/* check for occurrence of param as field of struct */
			{ char *ptr = Inline_stub[Inlining]->anms[i];
				while ((ptr = strstr(ptr, s)) != NULL)
				{	if (*(ptr-1) == '.'
					||  *(ptr+strlen(s)) == '.')
					{	fatal("formal par of %s used in structure name",
						Inline_stub[Inlining]->nm->name);
					}
					ptr++;
			}	}
			ReDiRect = Inline_stub[Inlining]->anms[i];
			return 0;
	}	 }

	yylval->sym = lookup(s);	/* symbol table */
	if (isutype(s))
		return UNAME;
	if (isproctype(s))
		return PNAME;
	if (iseqname(s))
		return INAME;

	return NAME;
}
Exemple #18
0
//O(1)
void push(struct node** node, int data)
{
	struct node* temp = nn(data);
	temp->next = *node;
	*node = temp;
}
	/*! \brief Initialize the iterator
	 *
	 * \param g Grid information
	 * \param start starting point
	 * \param stop stop point
	 * \param bc boundary conditions
	 *
	 */
	template<typename T> void Initialize(const grid_sm<dim,T> & g, const grid_key_dx<dim> & start , const grid_key_dx<dim> & stop, const size_t (& bc)[dim])
	{
		// copy the boundary conditions

		for (size_t i = 0 ; i < dim ; i++)
			this->bc[i] = bc[i];

		// compile-time array {0,0,0,....}  {2,2,2,...} {1,1,1,...}

		typedef typename generate_array<size_t,dim, Fill_zero>::result NNzero;
		typedef typename generate_array<size_t,dim, Fill_two>::result NNtwo;
		typedef typename generate_array<size_t,dim, Fill_three>::result NNthree;

		// Generate the sub-grid iterator

		grid_sm<dim,void> nn(NNthree::data);
		grid_key_dx_iterator_sub<dim> it(nn,NNzero::data,NNtwo::data);

		// Box base
		Box<dim,long int> base_b(start,stop);

		// intersect with all the boxes
		while (it.isNext())
		{
			auto key = it.get();

			if (check_invalid(key,bc) == true)
			{
				++it;
				continue;
			}

			bool intersect;

			// intersection box
			Box<dim,long int> b_int;
			Box<dim,long int> b_out;

			for (size_t i = 0 ; i < dim ; i++)
			{
				b_int.setLow(i,(key.get(i)-1)*g.getSize()[i]);
				b_int.setHigh(i,key.get(i)*g.getSize()[i]-1);
			}

			intersect = base_b.Intersect(b_int,b_out);

			// Bring to 0 and size[i]
			for (size_t i = 0 ; i < dim ; i++)
			{
				if (bc[i] == PERIODIC)
				{
					b_out.setLow(i,openfpm::math::positive_modulo(b_out.getLow(i),g.size(i)));
					b_out.setHigh(i,openfpm::math::positive_modulo(b_out.getHigh(i),g.size(i)));
				}
			}

			// if intersect add in the box list
			if (intersect == true)
				boxes.push_back(b_out);

			++it;
		}

		// initialize the first iterator
		if (boxes.size() > 0)
			grid_key_dx_iterator_sub<dim,warn>::reinitialize(grid_key_dx_iterator_sub<dim,warn>(g,boxes[0].getKP1(),boxes[0].getKP2()));
	}
	NodeTree* rrtgrow(const std::vector<float> &start,const std::vector<float> &goal,float goalbias,std::vector<float> upper,std::vector<float> lower,OpenRAVE::EnvironmentBasePtr env, OpenRAVE::RobotBasePtr robot)
	{
		RRTNode* startNode = new RRTNode(start);
		RRTNode* goalNode = new RRTNode(goal);
		setgoalConfig(goal);
		setGoalBias(goalbias);
		NodeTree* initPath = new NodeTree(startNode);
		
		NodeTree* path= new NodeTree();
		NodeTree* finalPath=new NodeTree();
		RRTNode* nearestNode = NULL;
		
		// initPath->setgoalflag(false);
		goalflag = false;

		std::vector<float>::const_iterator it;
		std::cout<<std::endl<<"Given:"<<std::endl<<"Goal:"<<goal[0]<<","<<goal[1]<<","<<goal[2]<<","<<goal[3]<<","<<goal[4]<<","<<goal[5]<<","<<goal[6]<<std::endl;
		std::cout<<std::endl<<"Start:"<<start[0]<<","<<start[1]<<","<<start[2]<<","<<start[3]<<","<<start[4]<<","<<start[5]<<","<<start[6]<<std::endl;


		for (int i=0; i<10000;++i){
			std::cout<<std::endl<<"RRT-iteration"<<i<<std::endl;
			
			RRTNode* sampledNode = initPath->getRamdomSample(upper,lower,goalNode);
			
			if(!checkifCollision(sampledNode->getConfig(),env,robot)){
				nearestNode = initPath->nearestNeighbour(sampledNode);

				std::vector<float> nn(nearestNode->getConfig().begin(),nearestNode->getConfig().end());
				std::cout<<"Nearest found:["<<nn[0]<<","<<nn[1]<<","<<nn[2]<<","<<nn[3]<<","<<nn[4]<<","<<nn[5]<<","<<nn[6]<<"]"<<std::endl;
				// std::cout<<"Nearest Node found..."<<std::endl;
				
				path = initPath->connectNodes(sampledNode, nearestNode, goalNode,initPath->stepSize(),env,robot);
				
				
			}
			else
				continue;
						 
			if(path->sizeNodes()==1){
				std::cout<<"restart subpath.."<<std::endl;
			 	continue;
			}
			else if(getNearestDistance(path->lastNode()->getConfig(),goal)==0){
				// std::cout<<"found found..."<<std::endl;
				initPath->addNodeTree(path);
				std::cout<<"...Final..."<<std::endl;
				
				for(it=path->lastNode()->getConfig().begin(); it!=path->lastNode()->getConfig().end(); ++it){
					std::cout<<"final Node:"<<(*it)<<std::endl;
				}
				
				break;
			}
			else{
				initPath->addNodeTree(path);
				std::cout<<"Intermediate Tree..."<<std::endl;
				// finalPath = initPath->getPath(initPath->sizeNodes()-1);
			}			
		}
		std::cout<<" Path found..."<<std::endl;

		finalPath = initPath->getPath(initPath->sizeNodes()-1);
		std::cout<<" initPath Size..."<<initPath->sizeNodes()<<std::endl;

		std::cout<<" FinalPath Size..."<<finalPath->sizeNodes()<<std::endl;
		
		return finalPath;
	}
int main(int argc, char **argv){

#if 0
  DOF6::TFLinkvf rot1,rot2;
  Eigen::Matrix4f tf1 = build_random_tflink(rot1,3);
  Eigen::Matrix4f tf2 = build_random_tflink(rot2,3);

  DOF6::DOF6_Source<DOF6::TFLinkvf,DOF6::TFLinkvf,float> abc(rot1.makeShared(), rot2.makeShared());
  abc.getRotation();
  abc.getTranslation();

  std::cout<<"tf1\n"<<tf1<<"\n";
  std::cout<<"tf2\n"<<tf2<<"\n";
  std::cout<<"tf1*2\n"<<tf1*tf2<<"\n";
  std::cout<<"tf2*1\n"<<tf2*tf1<<"\n";

  std::cout<<"tf1\n"<<rot1.getTransformation()<<"\n";
  std::cout<<"tf2\n"<<rot2.getTransformation()<<"\n";
  std::cout<<"tf1*2\n"<<(rot1+rot2).getTransformation()<<"\n";

  rot1.check();
  rot2.check();

  return 0;

  pcl::RotationFromCorrespondences rfc;
  Eigen::Vector3f n, nn, v,n2,n3,z,y,tv;
  float a = 0.1f;

  z.fill(0);y.fill(0);
  z(2)=1;y(1)=1;
  nn.fill(0);
  nn(0) = 1;
  Eigen::AngleAxisf aa(a,nn);

  nn.fill(100);

  n.fill(0);
  n(0) = 1;
  n2.fill(0);
  n2=n;
  n2(1) = 0.2;
  n3.fill(0);
  n3=n;
  n3(2) = 0.2;

  n2.normalize();
  n3.normalize();

  tv.fill(1);
  tv.normalize();

#if 0

#if 0
  rfc.add(n,aa.toRotationMatrix()*n+nn,
          1*n.cross(y),1*n.cross(z),
          1*(aa.toRotationMatrix()*n).cross(y),1*(aa.toRotationMatrix()*n).cross(z),
          1,1/sqrtf(3));
#else
  rfc.add(n,aa.toRotationMatrix()*n,
          0*n.cross(y),0*n.cross(z),
          0*(aa.toRotationMatrix()*n).cross(y),0*(aa.toRotationMatrix()*n).cross(z),
          1,0);
#endif

#if 1
  rfc.add(n2,aa.toRotationMatrix()*n2+nn,
          tv,tv,
          tv,tv,
          1,1);
#else
  rfc.add(n2,aa.toRotationMatrix()*n2+nn,
          1*n2.cross(y),1*n2.cross(z),
          1*(aa.toRotationMatrix()*n2).cross(y),1*(aa.toRotationMatrix()*n2).cross(z),
          1,1/sqrtf(3));
#endif

#else
  float f=1;
  Eigen::Vector3f cyl;
  cyl.fill(1);
  cyl(0)=1;
  Eigen::Matrix3f cylM;
  cylM.fill(0);
  cylM.diagonal() = cyl;
  rfc.add(n,aa.toRotationMatrix()*n,
          f*n.cross(y),f*n.cross(z),
          f*(aa.toRotationMatrix()*n).cross(y),f*(aa.toRotationMatrix()*n).cross(z),
          1,0);
  rfc.add(n2,aa.toRotationMatrix()*n2+nn,
          1*n2.cross(y),1*n2.cross(z),
          1*(aa.toRotationMatrix()*n2).cross(y),1*(aa.toRotationMatrix()*n2).cross(z),
          1,1);
#endif
  rfc.add(n3,aa.toRotationMatrix()*n3+nn,
          //tv,tv,
          //tv,tv,
          n3.cross(y),n3.cross(z),
          1*(aa.toRotationMatrix()*n3).cross(y),1*(aa.toRotationMatrix()*n3).cross(z),
          1,1);

  std::cout<<"comp matrix:\n"<<rfc.getTransformation()<<"\n\n";
  std::cout<<"real matrix:\n"<<aa.toRotationMatrix()<<"\n\n";

  return 0;

  //rfc.covariance_.normalize();
  rfc.covariance_ = (rfc.var_*rfc.covariance_.inverse().transpose()*rfc.covariance_);
  std::cout<<"comp matrix: "<<rfc.getTransformation()<<"\n\n";
  std::cout<<"real matrix: "<<aa.toRotationMatrix()*aa.toRotationMatrix()<<"\n\n";

  return 0;
#endif

  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}
Exemple #22
0
int main(int argc, char* argv[]) {
	//Parse cli options
	std::cout << &(*argv[1]);
	argc-=(argc>0); argv+=(argc>0); // skip program name argv[0] if present
	option::Stats  stats(usage, argc, argv);
	option::Option* options = new option::Option[stats.options_max];
	option::Option* buffer  = new option::Option[stats.buffer_max];
	option::Parser parse(usage, argc, argv, options, buffer);

	if (parse.error())
		return 1;

	if (options[HELP]) {
		option::printUsage(std::cout, usage);
		return 1;
	}

	std::string trainingFile = "";
	std::string stateFile = "state.json";

	for (int i = 0; i < parse.optionsCount(); ++i)
	{
		option::Option& opt = buffer[i];
		switch (opt.index())
		{
			case TRAIN:
			if (opt.arg)
				std::cout << "Training network from " << opt.arg << std::endl;
				trainingFile = opt.arg;
			break;
			case LOAD:
			if (opt.arg) 
				std::cout << "Loading network state from " << opt.arg << std::endl;
				stateFile = opt.arg;
			break;
		}
	}

	srand(time(NULL));
	std::vector<unsigned int> topology;

	topology.push_back(2);
	topology.push_back(10);
	topology.push_back(5);
	topology.push_back(1);
	Net nn(topology);

	if (stateFile.length() > 1) {
		nn.load(stateFile);	
	}

	if (trainingFile.length() > 1) {
		nn.trainFromFile(trainingFile);
	}

	if (stateFile.length() > 1) {
		nn.save(stateFile);
	}

	//Now test it with some non-training data
	std::vector<double> inputVals;
	inputVals = { 1, 0};
	nn.feedForward(inputVals);
	std::vector<double> resultVals;

	nn.getResults(resultVals);
	for (auto c : resultVals) {
		std::cout << "Inputs: 1 | 0" << std::endl;
		std::cout << "Outputs: " << c << std::endl;
		std::cout << "Average Error: " << nn.getRecentAverageError() << std::endl;
	}

	return 0;
}
Exemple #23
0
    //////////////////////////////////////////////////////////////////////
    // worker thread
    void PredictaEngine::loop()
    {
      setStatus("Waiting..");
      thread_idle = true;

      while(running){
	while(!optimize && running){
	  thread_idle = true;
	  setStatus("Waiting..");
	  usleep(100000); // 100ms (waits for action

	  train.clear();
	  scoring.clear();
	  results.clear();
	}

	if(!running) continue;

	thread_idle = false;
	time_t executionStartedTime = time(0);

	train.clear();
	scoring.clear();
	results.clear();

	//////////////////////////////////////////////////////////////////////////
	// loads at most 100.000 = 100k lines of data to memory

	setStatus("Loading data (examples)..");

	if(train.importAscii(trainingFile, 100000) == false){
	  std::string error = "Cannot load file: " + trainingFile;
	  setError(error);	  
	  optimize = false;
	  continue;
	}

	setStatus("Loading data (to be scored data)..");

	if(scoring.importAscii(scoringFile, 100000) == false){
	  std::string error = "Cannot load file: " + scoringFile;
	  train.clear();
	  scoring.clear();
	  setError(error);
	  optimize = false;
	  continue;
	}

	setStatus("Checking data validity..");

	train.removeBadData();
	scoring.removeBadData();
	
	// if number of data points in training is smaller than 2*dim(input)
	// then the optimizer fails
	
	if(train.getNumberOfClusters() < 0 || scoring.getNumberOfClusters() < 0){
	  setError("No data in input files");
	  optimize = false;
	  continue;
	}
	
	if(train.size(0) < 10){
	  setError("Not enough data (at least 10 examples) in input file");
	  optimize = false;
	  continue;
	}
	
	if(train.size(0) < 2*train.dimension(0)){
	  setError("Not enough data (at least 2*DIMENSION examples) in input file");
	  optimize = false;
	  continue;
	}

	if(train.dimension(0) != (scoring.dimension(0) + 1)){
	  setError("Incorrect dimensions in training or scoring files");
	  optimize = false;
	  continue;
	}

	if(train.dimension(0) < 2){
	  setError("Incorrect dimensions in training or scoring files");
	  optimize = false;
	  continue;
	}

	//////////////////////////////////////////////////////////////////////////
	// preprocess data using PCA (if PCA cannot be calculated the whole process fails)

	// separates training data into input and output clusters
	whiteice::dataset< whiteice::math::blas_real<double> > tmp;

	if(tmp.createCluster("input", train.dimension(0)-1) == false || 
	   tmp.createCluster("output", 1) == false){
	  setError("Internal software error");
	  optimize = false;
	  continue;
	}

	for(unsigned int i=0;i<train.size(0);i++){
	  whiteice::math::vertex< whiteice::math::blas_real<double> > t = train.access(0, i);
	  whiteice::math::vertex< whiteice::math::blas_real<double> > a;
	  whiteice::math::vertex< whiteice::math::blas_real<double> > b;
	  a.resize(t.size()-1);
	  b.resize(1);
	  b[0] = t[t.size()-1];

	  for(unsigned int j=0;j<(t.size()-1);j++)
	    a[j] = t[j];

	  if(tmp.add(0, a) == false || tmp.add(1, b) == false){
	    setError("Internal software error");
	    optimize = false;
	    continue;
	  }
	}

	if(optimize == false)
	  continue; // abort computations

	train = tmp;
	
	setStatus("Preprocessing data..");

	if(train.preprocess(0) == false /*|| train.preprocess(1) == false*/){
	  setError("Bad/singular data please add more variance to data");
	  optimize = false;
	  continue;
	}
	
	//////////////////////////////////////////////////////////////////////////
	// optimize neural network using LBFGS (ML starting point for HMC sampler)
	
	std::vector<unsigned int> arch; // use double layer wide nnetwork
	arch.push_back(train.dimension(0));
	// arch.push_back(100*train.dimension(0));
	arch.push_back(train.dimension(0) < 10 ? 10 : train.dimension(0));
	arch.push_back(train.dimension(0) < 10 ? 10 : train.dimension(0));
	arch.push_back(train.dimension(1));

	whiteice::nnetwork< whiteice::math::blas_real<double> > nn(arch);
	whiteice::LBFGS_nnetwork< whiteice::math::blas_real<double> > bfgs(nn, train, false, false);
	whiteice::math::vertex< whiteice::math::blas_real<double> > w;

	nn.randomize();

#if 0
	// deep pretraining is disabled as a default
	setStatus("Preoptimizing solution (deep learning)..");
	if(deep_pretrain_nnetwork(&nn, train, true) == false){
	  setError("ERROR: deep pretraining of nnetwork failed.\n");
	  optimize = false;
	  continue;
	}
#endif

	
	nn.exportdata(w);
	bfgs.minimize(w);

	time_t t0 = time(0);
	unsigned int iterations = 0;
	whiteice::math::blas_real<double> error;


	while(optimize && bfgs.solutionConverged() == false && bfgs.isRunning() == true){
	  if(optimize == false){ // we lost license to do this anymore..
	    setStatus("Aborting optimization");
	    break;
	  }

	  time_t t1 = time(0);
	  unsigned int counter = (unsigned int)(t1 - t0); // time-elapsed

	  if(bfgs.getSolution(w, error, iterations) == false){ // we lost license to continue..
	    setStatus("Aborting optimization");
	    setError("LBFGS::getSolution() failed");
	    optimize = false;
	    break;
	  }

	  char buffer[128];
	  snprintf(buffer, 128, "Preoptimizing solution (%d iterations, %.2f minutes): %f",
		   iterations, counter/60.0f, error.c[0]);

	  setStatus(buffer);

	  // update results only every 5 seconds
	  sleep(5);
	}

	if(optimize == false){
	  bfgs.stopComputation();
	  continue; // abort computation
	}

	
	// after convergence, get the best solution
	if(bfgs.getSolution(w, error, iterations) == false){ // we lost license to continue..
	  setStatus("Aborting optimization");
	  setError("LBFGS::getSolution() failed");
	  optimize = false;
	  continue;
	}

	nn.importdata(w);

	//////////////////////////////////////////////////////////////////////////
	// use HMC to sample from max likelihood in order to get MAP

	setStatus("Analyzing uncertainty..");

	// whiteice::UHMC< whiteice::math::blas_real<double> > hmc(nn, train, true);
	whiteice::HMC< whiteice::math::blas_real<double> > hmc(nn, train, true);
	// whiteice::linear_ETA<float> eta;

	// for high quality..
	// we use just 50 samples
	// const unsigned int NUMSAMPLES = 1000;
	// eta.start(0.0f, NUMSAMPLES);

	if(hmc.startSampler() == false){
	  setStatus("Starting sampler failed (internal error)");
	  setError("Cannot start sampler");
	  optimize = false;
	  continue;
	}

	// unsigned int samples = 0;

	t0 = time(0);
	
	// always analyzes results for given time length
	double timeElapsed = (time(0) - executionStartedTime);
	double totalTime = 0;
	
	if(timeElapsed < optimizationTime)
	  totalTime = optimizationTime - timeElapsed;
	

	while(optimize){
	  unsigned int samples = hmc.getNumberOfSamples();
	  // if(samples >= NUMSAMPLES) break;
	  
	  // eta.update((float)hmc.getNumberOfSamples());

	  time_t t1 = time(0);
	  double counter = (double)(t1 - t0); // time-elapsed
	  
	  double timeLeft = (totalTime - counter)/60.0;
	  if(timeLeft <= 0.0){
	    timeLeft = 0.0;
	    if(hmc.getNumberOfSamples() > 0)
	      break; // always gets a single sample from HMC
	  }

	  char buffer[128];
	  snprintf(buffer, 128,
		   "Analyzing uncertainty (%d iterations. %.2f%% error. ETA %.2f minutes)",
		   // 100.0*((double)samples/((double)NUMSAMPLES)),
		   samples,
		   100.0*hmc.getMeanError(1).c[0]/error.c[0],
		   timeLeft);
		   // eta.estimate()/60.0);

	  setStatus(buffer);

	  if(optimize == false){ // we lost license to continue..
	    setStatus("Uncertainty analysis aborted");
	    break;
	  }

	  // updates only every 5 seconds so that we do not take too much resources
	  sleep(5); 
	}

	if(optimize == false)
	  continue; // abort computation

	hmc.stopSampler();
	
	//////////////////////////////////////////////////////////////////////////
	// estimate mean and variance of output given inputs in 'scoring'

	setStatus("Calculating scoring..");

	whiteice::bayesian_nnetwork< whiteice::math::blas_real<double> > bnn;
	
	if(hmc.getNetwork(bnn) == false){
	  setStatus("Exporting prediction model failed");
	  setError("Internal software error");
	  optimize = false;
	  continue;
	}
	
	if(results.createCluster("results", 1) == false){
	  setError("Internal software error");
	  optimize = false;
	  continue;
	}


	unsigned int NUM = scoring.size(0);

	// demo version only scores 10 first examples given in a file.
	if(demoVersion){
	  if(NUM > 10) NUM = 10;
	}
	
	for(unsigned int i=0;i<NUM;i++){

	  char buffer[128];
	  snprintf(buffer, 128, "Scoring data (%.1f%%)..",
		   100.0*((double)i)/((double)scoring.size(0)));
	  setStatus(buffer);
	  
	  
	  whiteice::math::vertex< whiteice::math::blas_real<double> > mean;
	  whiteice::math::matrix< whiteice::math::blas_real<double> > cov;
	  whiteice::math::vertex< whiteice::math::blas_real<double> > score;
	  auto tmp = scoring[i];

	  if(train.preprocess(0, tmp) == false){
	    setStatus("Calculating prediction failed (preprocess)");
	    setError("Internal software error");
	    optimize = false;
	    break;
	  }
	  
	  if(bnn.calculate(tmp, mean, cov) == false){
	    setStatus("Calculating prediction failed");
	    setError("Internal software error");
	    optimize = false;
	    break;
	  }

	  score.resize(1);
	  score[0] = mean[0] + risk*cov(0,0);

	  if(results.add(0, score) == false){
	    setStatus("Calculating prediction failed (storage)");
	    setError("Internal software error");
	    optimize = false;
	    break;
	  }

	  if(optimize == false)
	    break; // lost our license to continue
	}

	if(optimize == false)
	  continue; // lost our license to continue


	// finally save the results
	setStatus("Saving prediction results to file..");

	if(results.exportAscii(resultsFile) == false){
	  setStatus("Saving prediction results failed");
	  setError("Internal software error");
	  optimize = false;
	  break;
	}

	setStatus("Computations complete");
	
	optimize = false;
      }
    }
Exemple #24
0
//at head
void push(struct node** href, int data)
{
	struct node* cur = nn(data);
	cur->next = *href;
	*href = cur;
}
Exemple #25
0
main()
    {
    
        struct BT *rt1=nn(5);
        rt1->l=nn(4);
        rt1->r=nn(6);
        rt1->l->l=nn(3);
        rt1->l->r=nn(2);
        rt1->r->l=nn(7);
        rt1->r->r=nn(8);
        
        struct BT *rt2=nn(5);
         rt2->r=nn(4);
        rt2->l=nn(6);
        rt2->r->r=nn(3);
        rt2->r->l=nn(2);
        rt2->l->r=nn(7);
        rt2->l->l=nn(8);
        
        if(isomorphic(rt1,rt2))
            printf("\nIsomorphic\n");
            
         else
            printf("\nNon Isomorphic\n");       
    }
Exemple #26
0
    verbly::filter<verbly::noun> parse_selrestrs(verbly::frame::selrestr selrestr)
    {
      switch (selrestr.get_type())
      {
        case verbly::frame::selrestr::type::empty:
        {
          return verbly::filter<verbly::noun>{};
        }
        
        case verbly::frame::selrestr::type::singleton:
        {
          verbly::noun n;
          
          if (selrestr.get_restriction() == "concrete")
          {
            n = database.nouns().with_singular_form("physical entity").limit(1).run().front();
          } else if (selrestr.get_restriction() == "time")
          {
            n = database.nouns().with_singular_form("time").limit(1).run().front();
          } else if (selrestr.get_restriction() == "state")
          {
            n = database.nouns().with_singular_form("state").limit(1).run().front();
          } else if (selrestr.get_restriction() == "abstract")
          {
            n = database.nouns().with_singular_form("abstract entity").limit(1).run().front();
          } else if (selrestr.get_restriction() == "time")
          {
            n = database.nouns().with_singular_form("time").limit(1).run().front();
          } else if (selrestr.get_restriction() == "scalar")
          {
            n = database.nouns().with_singular_form("number").limit(1).run().front();
          } else if (selrestr.get_restriction() == "currency")
          {
            auto nn2 = database.nouns().with_singular_form("currency").limit(2).run();
            std::vector<verbly::noun> nn(std::begin(nn2), std::end(nn2));
            n = nn[1];
          } else if (selrestr.get_restriction() == "location")
          {
            n = database.nouns().with_singular_form("location").limit(1).run().front();
          } else if (selrestr.get_restriction() == "organization")
          {
            n = database.nouns().with_singular_form("organization").limit(1).run().front();
          } else if (selrestr.get_restriction() == "int_control")
          {
            n = database.nouns().with_singular_form("causal agent").limit(1).run().front();
          } else if (selrestr.get_restriction() == "natural")
          {
            n = database.nouns().with_singular_form("natural object").limit(1).run().front();
          } else if (selrestr.get_restriction() == "phys_obj")
          {
            n = database.nouns().with_singular_form("physical object").limit(1).run().front();
          } else if (selrestr.get_restriction() == "solid")
          {
            n = database.nouns().with_singular_form("solid").limit(1).run().front();
          } else if (selrestr.get_restriction() == "shape")
          {
            n = database.nouns().with_singular_form("shape").limit(1).run().front();
          } else if (selrestr.get_restriction() == "substance")
          {
            n = database.nouns().with_singular_form("substance").limit(1).run().front();
          } else if (selrestr.get_restriction() == "idea")
          {
            n = database.nouns().with_singular_form("idea").limit(1).run().front();
          } else if (selrestr.get_restriction() == "sound")
          {
            auto nn2 = database.nouns().with_singular_form("sound").limit(4).run();
            std::vector<verbly::noun> nn(std::begin(nn2), std::end(nn2));
            n = nn[3];
          } else if (selrestr.get_restriction() == "communication")
          {
            n = database.nouns().with_singular_form("communication").limit(1).run().front();
          } else if (selrestr.get_restriction() == "region")
          {
            n = database.nouns().with_singular_form("region").limit(1).run().front();
          } else if (selrestr.get_restriction() == "place")
          {
            n = database.nouns().with_singular_form("place").limit(1).run().front();
          } else if (selrestr.get_restriction() == "machine")
          {
            n = database.nouns().with_singular_form("machine").limit(1).run().front();
          } else if (selrestr.get_restriction() == "animate")
          {
            n = database.nouns().with_singular_form("animate being").limit(1).run().front();
          } else if (selrestr.get_restriction() == "plant")
          {
            auto nn2 = database.nouns().with_singular_form("plant").limit(2).run();
            std::vector<verbly::noun> nn(std::begin(nn2), std::end(nn2));
            n = nn[1];
          } else if (selrestr.get_restriction() == "comestible")
          {
            n = database.nouns().with_singular_form("food").limit(1).run().front();
          } else if (selrestr.get_restriction() == "artifact")
          {
            n = database.nouns().with_singular_form("artifact").limit(1).run().front();
          } else if (selrestr.get_restriction() == "vehicle")
          {
            n = database.nouns().with_singular_form("vehicle").limit(1).run().front();
          } else if (selrestr.get_restriction() == "human")
          {
            n = database.nouns().with_singular_form("person").limit(1).run().front();
          } else if (selrestr.get_restriction() == "animal")
          {
            n = database.nouns().with_singular_form("animal").limit(1).run().front();
          } else if (selrestr.get_restriction() == "body_part")
          {
            n = database.nouns().with_singular_form("body part").limit(1).run().front();
          } else if (selrestr.get_restriction() == "garment")
          {
            n = database.nouns().with_singular_form("clothing").limit(1).run().front();
          } else if (selrestr.get_restriction() == "tool")
          {
            n = database.nouns().with_singular_form("tool").limit(1).run().front();
          } else {
            return verbly::filter<verbly::noun>{};
          }
          
          return verbly::filter<verbly::noun>{n, !selrestr.get_pos()};
        }
        
        case verbly::frame::selrestr::type::group:
        {
          verbly::filter<verbly::noun> ret;
          ret.set_orlogic(selrestr.get_orlogic());

          std::transform(std::begin(selrestr), std::end(selrestr), std::back_inserter(ret), [&] (verbly::frame::selrestr sr) {
            return parse_selrestrs(sr);
          });
          
          return ret;
        }
      }
    }
Exemple #27
0
  bool pBFGS_nnetwork<T>::minimize(unsigned int NUMTHREADS)
  {
    thread_mutex.lock();

    if(thread_running){
      thread_mutex.unlock();
      return false; // already running
    }
    
    bfgs_mutex.lock();
    
    if(optimizers.size() > 0){
      bfgs_mutex.unlock();
      thread_mutex.unlock();
      return false; // already running
    }


    optimizers.resize(NUMTHREADS);
    
    for(auto& o : optimizers)
      o = nullptr;


    try{
      unsigned int index = 0;
      
      for(auto& o : optimizers){
	o.reset(new BFGS_nnetwork<T>(net, data, overfit, negativefeedback));
	
	nnetwork<T> nn(this->net);

	// we keep a single instance (i=0) of
	// the original nn in a starting set
	if(index != 0){
	  nn.randomize();
	  normalize_weights_to_unity(nn);
	  T alpha = T(0.5f);
	  negative_feedback_between_neurons(nn, data, alpha);
	}
	
	math::vertex<T> w;
	nn.exportdata(w);

	if(index == 0){
	  global_best_x = w;
	  global_best_y = T(10e10);
	  global_iterations = 0;
	}
	
	o->minimize(w);
      }
    }
    catch(std::exception& e){
      optimizers.clear();
      thread_running = false;

      bfgs_mutex.unlock();
      thread_mutex.unlock();
      
      return false;
    }

    bfgs_mutex.unlock();

    
    thread_running = true;
    
    thread_is_running_mutex.lock();
    thread_is_running = 0;
    thread_is_running_mutex.unlock();
    
    try{
      updater_thread = std::thread(std::bind(&pBFGS_nnetwork<T>::updater_loop, this));
      updater_thread.detach();
    }
    catch(std::exception& e){
      {
	std::lock_guard<std::mutex> lock(bfgs_mutex);
	optimizers.clear();
      }
      thread_running = false;
      
      return false;
    }
    
    thread_mutex.unlock();
    
    return true;
  }
// Convert conserved variables to characteristic variables
void ConvertQtoW(int ixy, 
		 const dTensorBC5& aux, 
		 const dTensorBC5& qold, 
                 dTensorBC5& dwp, 
		 dTensorBC5& dwm, 
		 dTensorBC5& w_cent,
		 void (*ProjectLeftEig)(int,
					const dTensor1&,
					const dTensor1&,
					const dTensor2&,
					dTensor2&))
{
  const int mx    = qold.getsize(1);
  const int my    = qold.getsize(2);
  const int mz    = qold.getsize(3);
  const int meqn  = qold.getsize(4);
  const int mbc   = qold.getmbc();
  const int maux  = aux.getsize(4);
  const int ksize = w_cent.getsize(5);  
  const int space_order = dogParams.get_space_order();

  // ----------------------------------------------------------
  //
  // Key: storage of characteristic variables
  //       
  //      wx_cent(i,j,k,me,1)  = Lx * q(i,j,k,me,2)
  //      wx_cent(i,j,k,me,2)  = Lx * q(i,j,k,me,5)
  //      wx_cent(i,j,k,me,3)  = Lx * q(i,j,k,me,6)
  //      wx_cent(i,j,k,me,4)  = Lx * q(i,j,k,me,8)
  //
  //      dw_right(i,j,k,me,1) = Lx * (q(i+1,j,k,me,1)-q(i,j,k,me,1))
  //      dw_right(i,j,k,me,2) = Lx * (q(i,j+1,k,me,2)-q(i,j,k,me,2))
  //      dw_right(i,j,k,me,3) = Lx * (q(i,j,k+1,me,2)-q(i,j,k,me,2))
  //      dw_right(i,j,k,me,4) = Lx * (q(i+1,j,k,me,2)-q(i,j,k,me,2))
  //
  //      dw_left(i,j,k,me,1)  = Lx * (q(i,j,k,me,1)-q(i-1,j,k,me,1))
  //      dw_left(i,j,k,me,2)  = Lx * (q(i,j,k,me,2)-q(i,j-1,k,me,2))
  //      dw_left(i,j,k,me,3)  = Lx * (q(i,j,k,me,2)-q(i,j,k-1,me,2))
  //      dw_left(i,j,k,me,4)  = Lx * (q(i,j,k,me,2)-q(i-1,j,k,me,2))
  //
  //      wy_cent(i,j,k,me,1)  = Ly * q(i,j,k,me,3)
  //      wy_cent(i,j,k,me,2)  = Ly * q(i,j,k,me,5)
  //      wy_cent(i,j,k,me,3)  = Ly * q(i,j,k,me,7)
  //      wy_cent(i,j,k,me,4)  = Ly * q(i,j,k,me,9)
  //
  //      dw_back(i,j,k,me,1)  = Ly * (q(i,j+1,k,me,1)-q(i,j,k,me,1))
  //      dw_back(i,j,k,me,2)  = Ly * (q(i+1,j,k,me,3)-q(i,j,k,me,3))
  //      dw_back(i,j,k,me,3)  = Ly * (q(i,j,k+1,me,3)-q(i,j,k,me,3))
  //      dw_back(i,j,k,me,4)  = Ly * (q(i,j+1,k,me,3)-q(i,j,k,me,3))
  //
  //      dw_front(i,j,k,me,1) = Ly * (q(i,j,k,me,1)-q(i,j-1,k,me,1))
  //      dw_front(i,j,k,me,2) = Ly * (q(i,j,k,me,3)-q(i-1,j,k,me,3))
  //      dw_front(i,j,k,me,3) = Ly * (q(i,j,k,me,3)-q(i,j,k-1,me,3))
  //      dw_front(i,j,k,me,4) = Ly * (q(i,j,k,me,3)-q(i,j-1,k,me,3))
  //
  //      wz_cent(i,j,k,me,1)  = Lz * q(i,j,k,me,4)
  //      wz_cent(i,j,k,me,2)  = Lz * q(i,j,k,me,6)
  //      wz_cent(i,j,k,me,3)  = Lz * q(i,j,k,me,7)
  //      wz_cent(i,j,k,me,4)  = Lz * q(i,j,k,me,10)
  //
  //      dw_up(i,j,k,me,1)    = Lz * (q(i,j,k+1,me,1)-q(i,j,k,me,1))
  //      dw_up(i,j,k,me,2)    = Lz * (q(i+1,j,k,me,4)-q(i,j,k,me,4))
  //      dw_up(i,j,k,me,3)    = Lz * (q(i,j+1,k,me,4)-q(i,j,k,me,4))
  //      dw_up(i,j,k,me,4)    = Lz * (q(i,j,k+1,me,4)-q(i,j,k,me,4))
  //
  //      dw_down(i,j,k,me,1)  = Lz * (q(i,j,k,me,1)-q(i,j,k-1,me,1))
  //      dw_down(i,j,k,me,2)  = Lz * (q(i,j,k,me,4)-q(i-1,j,k,me,4))
  //      dw_down(i,j,k,me,3)  = Lz * (q(i,j,k,me,4)-q(i,j-1,k,me,4))
  //      dw_down(i,j,k,me,4)  = Lz * (q(i,j,k,me,4)-q(i,j,k-1,me,4))
  //
  // ---------------------------------------------------------- 
   
  //
  // Loop over all interior grid cells
  //
  switch( ixy )
    {
    case 1:
#pragma omp parallel for
      for (int i=(3-mbc); i<=(mx+mbc-2); i++)
	for (int j=(3-mbc); j<=(my+mbc-2); j++)
	  for (int k=(3-mbc); k<=(mz+mbc-2); k++)
	    {
	      iTensor1 nn(4);
	      dTensor1 Aux_ave(maux),Q_ave(meqn);
	      dTensor2 Qin(meqn,ksize),Wout(meqn,ksize);
	      	      
	      // index for x-direction
	      nn.set(1, 2 );
	      nn.set(2, 5 );
	      nn.set(3, 6 );
	      nn.set(4, 8 );
	      
	      // Store q and aux values in temporary arrays
	      for (int m=1; m<=meqn; m++)
		{  Q_ave.set(m, qold.get(i,j,k,m,1) );   }
	      
	      for (int m=1; m<=maux; m++)
		{  Aux_ave.set(m, aux.get(i,j,k,m,1) );  }
	      
	      // ------------------------------------------------
	      // Part I:  dw_right = Lx*( q(i+1,j,k) - q(i,j,k) )
	      // ------------------------------------------------
	      switch( space_order )
		{
		case 3:
		  for (int m=1; m<=meqn; m++)
		    {  
		      Qin.set(m,1, qold.get(i+1,j,k,m,1) - qold.get(i,j,k,m,1) );
		      Qin.set(m,2, qold.get(i,j+1,k,m,2) - qold.get(i,j,k,m,2) );
		      Qin.set(m,3, qold.get(i,j,k+1,m,2) - qold.get(i,j,k,m,2) );
		      Qin.set(m,4, qold.get(i+1,j,k,m,2) - qold.get(i,j,k,m,2) );
		    }
		  break;
		  
		case 2:
		  for (int m=1; m<=meqn; m++)
		    {  
		      Qin.set(m,1, qold.get(i+1,j,k,m,1) - qold.get(i,j,k,m,1) );
		    }
		  break;
		}
	      
	      // Project Qin onto left eigenvectors in cell (i,j,k)
	      ProjectLeftEig(1,Aux_ave,Q_ave,Qin,Wout);
	      
	      // Store results in dw_right
	      for (int m=1; m<=meqn; m++)
		for (int ell=1; ell<=ksize; ell++)
		  {  dwp.set(i,j,k,m,ell, Wout.get(m,ell) );  }		  
	      // --------------------------------------------
	      
	      // ------------------------------------------------
	      // Part II:  dw_left = Lx*( q(i,j,k) - q(i-1,j,k) )
	      // ------------------------------------------------
	      switch( space_order )		  
		{
		case 3:
		  for (int m=1; m<=meqn; m++)
		    {
		      Qin.set(m,1, qold.get(i,j,k,m,1) - qold.get(i-1,j,k,m,1) );
		      Qin.set(m,2, qold.get(i,j,k,m,2) - qold.get(i,j-1,k,m,2) );
		      Qin.set(m,3, qold.get(i,j,k,m,2) - qold.get(i,j,k-1,m,2) );
		      Qin.set(m,4, qold.get(i,j,k,m,2) - qold.get(i-1,j,k,m,2) );
		    }
		  break;
		  
		case 2:
		  for (int m=1; m<=meqn; m++)
		    {  
		      Qin.set(m,1, qold.get(i,j,k,m,1) - qold.get(i-1,j,k,m,1) );
		    }
		  break;
		}
	      
	      // Project Qin onto left eigenvectors in cell (i,j,k)
	      ProjectLeftEig(1,Aux_ave,Q_ave,Qin,Wout);
		
	      // Store results in dw_left
	      for (int m=1; m<=meqn; m++)
		for (int ell=1; ell<=ksize; ell++)
		  {  dwm.set(i,j,k,m,ell, Wout.get(m,ell) );  }
	      // --------------------------------------------
		
	      // --------------------------------------------
	      // Part III:  wx_cent = Lx*q(i,j,k)
	      // --------------------------------------------
	      for (int m=1; m<=meqn; m++)
		for (int ell=1; ell<=ksize; ell++)
		  {  Qin.set(m,ell, qold.get(i,j,k,m,nn.get(ell)) );  }	      
	      
	      // Project Qin onto left eigenvectors in cell (i,j,k)
	      ProjectLeftEig(1,Aux_ave,Q_ave,Qin,Wout);
	
	      // Store results in wx_cent
	      for (int m=1; m<=meqn; m++)
		for (int ell=1; ell<=ksize; ell++)
		  {  w_cent.set(i,j,k,m,ell, Wout.get(m,ell) );  }	      
	      // --------------------------------------------
	    }      
      break;
      
    case 2:
#pragma omp parallel for
      for (int i=(3-mbc); i<=(mx+mbc-2); i++)
	for (int j=(3-mbc); j<=(my+mbc-2); j++)
	  for (int k=(3-mbc); k<=(mz+mbc-2); k++)
	    {
	      iTensor1 nn(4);
	      dTensor1 Aux_ave(maux),Q_ave(meqn);
	      dTensor2 Qin(meqn,ksize),Wout(meqn,ksize);
	      	      
	      // index for x-direction
	      nn.set(1, 3 );
	      nn.set(2, 5 );
	      nn.set(3, 7 );
	      nn.set(4, 9 );
	      
	      // Store q and aux values in temporary arrays
	      for (int m=1; m<=meqn; m++)
		{  Q_ave.set(m, qold.get(i,j,k,m,1) );   }
	      
	      for (int m=1; m<=maux; m++)
		{  Aux_ave.set(m, aux.get(i,j,k,m,1) );  }
	      
	      // ------------------------------------------------
	      // Part I:  dw_right = Ly*( q(i,j+1,k) - q(i,j,k) )
	      // ------------------------------------------------
	      switch( space_order )
		{
		case 3:
		  for (int m=1; m<=meqn; m++)
		    {  
		      Qin.set(m,1, qold.get(i,j+1,k,m,1) - qold.get(i,j,k,m,1) );
		      Qin.set(m,2, qold.get(i+1,j,k,m,3) - qold.get(i,j,k,m,3) );
		      Qin.set(m,3, qold.get(i,j,k+1,m,3) - qold.get(i,j,k,m,3) );
		      Qin.set(m,4, qold.get(i,j+1,k,m,3) - qold.get(i,j,k,m,3) );
		    }
		  break;
		  
		case 2:
		  for (int m=1; m<=meqn; m++)
		    {  
		      Qin.set(m,1, qold.get(i,j+1,k,m,1) - qold.get(i,j,k,m,1) );
		    }
		  break;
		}
	      
	      // Project Qin onto left eigenvectors in cell (i,j,k)
	      ProjectLeftEig(2,Aux_ave,Q_ave,Qin,Wout);
	      
	      // Store results in dw_right
	      for (int m=1; m<=meqn; m++)
		for (int ell=1; ell<=ksize; ell++)
		  {  dwp.set(i,j,k,m,ell, Wout.get(m,ell) );  }		  
	      // --------------------------------------------
	      
	      // ------------------------------------------------
	      // Part II:  dw_left = Ly*( q(i,j,k) - q(i,j-1,k) )
	      // ------------------------------------------------
	      switch( space_order )		  
		{
		case 3:
		  for (int m=1; m<=meqn; m++)
		    {
		      Qin.set(m,1, qold.get(i,j,k,m,1) - qold.get(i,j-1,k,m,1) );
		      Qin.set(m,2, qold.get(i,j,k,m,3) - qold.get(i-1,j,k,m,3) );
		      Qin.set(m,3, qold.get(i,j,k,m,3) - qold.get(i,j,k-1,m,3) );
		      Qin.set(m,4, qold.get(i,j,k,m,3) - qold.get(i,j-1,k,m,3) );
		    }
		  break;
		  
		case 2:
		  for (int m=1; m<=meqn; m++)
		    {  
		      Qin.set(m,1, qold.get(i,j,k,m,1) - qold.get(i,j-1,k,m,1) );
		    }
		  break;
		}
	      
	      // Project Qin onto left eigenvectors in cell (i,j,k)
	      ProjectLeftEig(2,Aux_ave,Q_ave,Qin,Wout);
		
	      // Store results in dw_left
	      for (int m=1; m<=meqn; m++)
		for (int ell=1; ell<=ksize; ell++)
		  {  dwm.set(i,j,k,m,ell, Wout.get(m,ell) );  }
	      // --------------------------------------------
		
	      // --------------------------------------------
	      // Part III:  wy_cent = Ly*q(i,j,k)
	      // --------------------------------------------
	      for (int m=1; m<=meqn; m++)
		for (int ell=1; ell<=ksize; ell++)
		  {  Qin.set(m,ell, qold.get(i,j,k,m,nn.get(ell)) );  }	      
	      
	      // Project Qin onto left eigenvectors in cell (i,j,k)
	      ProjectLeftEig(2,Aux_ave,Q_ave,Qin,Wout);
	
	      // Store results in wx_cent
	      for (int m=1; m<=meqn; m++)
		for (int ell=1; ell<=ksize; ell++)
		  {  w_cent.set(i,j,k,m,ell, Wout.get(m,ell) );  }	      
	      // --------------------------------------------
	    }
      break;
      
    case 3:
#pragma omp parallel for
      for (int i=(3-mbc); i<=(mx+mbc-2); i++)
	for (int j=(3-mbc); j<=(my+mbc-2); j++)
	  for (int k=(3-mbc); k<=(mz+mbc-2); k++)
	    {
	      iTensor1 nn(4);
	      dTensor1 Aux_ave(maux),Q_ave(meqn);
	      dTensor2 Qin(meqn,ksize),Wout(meqn,ksize);
	      	      
	      // index for x-direction
	      nn.set(1, 4  );
	      nn.set(2, 6  );
	      nn.set(3, 7  );
	      nn.set(4, 10 );
	      
	      // Store q and aux values in temporary arrays
	      for (int m=1; m<=meqn; m++)
		{  Q_ave.set(m, qold.get(i,j,k,m,1) );   }
	      
	      for (int m=1; m<=maux; m++)
		{  Aux_ave.set(m, aux.get(i,j,k,m,1) );  }
	      
	      // ------------------------------------------------
	      // Part I:  dw_right = Lz*( q(i,j,k+1) - q(i,j,k) )
	      // ------------------------------------------------
	      switch( space_order )
		{
		case 3:
		  for (int m=1; m<=meqn; m++)
		    {  
		      Qin.set(m,1, qold.get(i,j,k+1,m,1) - qold.get(i,j,k,m,1) );
		      Qin.set(m,2, qold.get(i+1,j,k,m,4) - qold.get(i,j,k,m,4) );
		      Qin.set(m,3, qold.get(i,j+1,k,m,4) - qold.get(i,j,k,m,4) );
		      Qin.set(m,4, qold.get(i,j,k+1,m,4) - qold.get(i,j,k,m,4) );
		    }
		  break;
		  
		case 2:
		  for (int m=1; m<=meqn; m++)
		    {  
		      Qin.set(m,1, qold.get(i,j,k+1,m,1) - qold.get(i,j,k,m,1) );
		    }
		  break;
		}
	      
	      // Project Qin onto left eigenvectors in cell (i,j,k)
	      ProjectLeftEig(3,Aux_ave,Q_ave,Qin,Wout);
	      
	      // Store results in dw_right
	      for (int m=1; m<=meqn; m++)
		for (int ell=1; ell<=ksize; ell++)
		  {  dwp.set(i,j,k,m,ell, Wout.get(m,ell) );  }		  
	      // --------------------------------------------
	      
	      // ------------------------------------------------
	      // Part II:  dw_left = Lz*( q(i,j,k) - q(i,j,k-1) )
	      // ------------------------------------------------
	      switch( space_order )		  
		{
		case 3:
		  for (int m=1; m<=meqn; m++)
		    {
		      Qin.set(m,1, qold.get(i,j,k,m,1) - qold.get(i,j,k-1,m,1) );
		      Qin.set(m,2, qold.get(i,j,k,m,4) - qold.get(i-1,j,k,m,4) );
		      Qin.set(m,3, qold.get(i,j,k,m,4) - qold.get(i,j-1,k,m,4) );
		      Qin.set(m,4, qold.get(i,j,k,m,4) - qold.get(i,j,k-1,m,4) );
		    }
		  break;
		  
		case 2:
		  for (int m=1; m<=meqn; m++)
		    {  
		      Qin.set(m,1, qold.get(i,j,k,m,1) - qold.get(i,j,k-1,m,1) );
		    }
		  break;
		}
	      
	      // Project Qin onto left eigenvectors in cell (i,j,k)
	      ProjectLeftEig(3,Aux_ave,Q_ave,Qin,Wout);
		
	      // Store results in dw_left
	      for (int m=1; m<=meqn; m++)
		for (int ell=1; ell<=ksize; ell++)
		  {  dwm.set(i,j,k,m,ell, Wout.get(m,ell) );  }
	      // --------------------------------------------
		
	      // --------------------------------------------
	      // Part III:  wz_cent = Lz*q(i,j,k)
	      // --------------------------------------------
	      for (int m=1; m<=meqn; m++)
		for (int ell=1; ell<=ksize; ell++)
		  {  Qin.set(m,ell, qold.get(i,j,k,m,nn.get(ell)) );  }	      
	      
	      // Project Qin onto left eigenvectors in cell (i,j,k)
	      ProjectLeftEig(3,Aux_ave,Q_ave,Qin,Wout);
	
	      // Store results in wx_cent
	      for (int m=1; m<=meqn; m++)
		for (int ell=1; ell<=ksize; ell++)
		  {  w_cent.set(i,j,k,m,ell, Wout.get(m,ell) );  }	      
	      // --------------------------------------------
	    }
      break;
    }
  
}
int main(int argc, char* argv[])
{
	std::vector<Mat<float> > mse;
	
	//Neural Networks settings :
	Topology topo;
	unsigned int nbrneurons = 25;
	unsigned int nbrlayer = 1;
	unsigned int nbrinput = width*height;
	unsigned int nbroutput = 10;
	
	//topo.push_back(nbrinput,NTNONE);	//input layer
	topo.push_back(nbrinput,NTSIGMOID);	//input layer
	
	//topo.push_back(nbrneurons, NTSIGMOID);
	topo.push_back(15, NTSIGMOID);
	
	//topo.push_back(nbroutput, NTSOFTMAX);	//linear output
	topo.push_back(nbroutput, NTSIGMOID);	//linear output
	
	NN<float> nn(topo);
	nn.learning = false;
	//------------------------------
	
	//DATASET SETTINGS :
	report.open(report_fn.c_str(), ios::out);
    image.open(training_image_fn.c_str(), ios::in | ios::binary); // Binary image file
    label.open(training_label_fn.c_str(), ios::in | ios::binary ); // Binary label file

	// Reading file headers
    char number;
    for (int i = 1; i <= 16; ++i) {
        image.read(&number, sizeof(char));
	}
    for (int i = 1; i <= 8; ++i) {
        label.read(&number, sizeof(char));
	}
	
	//------------------------------
	
	//checking rotation :
	Mat<float> im1(8,8, (char)1);
	Mat<float> im2( rotate(im1,PI/2.0f) );
	
	im1.afficher();
	im2.afficher();
	//--------------------------------
	
	//checking arctan !!!
	float y = -4.0f;
	float x = 4.0f;
	std::cout << arctan(y,x)*180.0f/(PI) << std::endl;
	//--------------------------------------------------
	
	//checking reading :
	char labelval = 0;
	float theta = PI/2;
	im1 = inputMNIST(labelval);
	im2 = rotate(im1,theta);
	im2.afficher();
		
	std::cout << "Rotation of : " << theta*180.0f/PI << std::endl;
	//---------------------------------------------------
	
	
	
	int iteration = 25000;
	int offx = 2;
	int offy = 2;
	int size = 28;
	int countSuccess = 0;
	
	while( iteration)
	{
		Mat<float> rotatedinput( inputMNIST(labelval) );
		//let us choose the rotation :
		float theta = ((float)(rand()%360))*PI/180.0f;
		
		//let us apply it :
		rotatedinput = extract( rotate(rotatedinput, theta), offx,offy, offx+(size-1), offy+(size-1) );
		
		Mat<float> input( reshapeV( rotatedinput ) );
		Mat<float> target( 0.0f, 10,1);
		target.set( 1.0f, labelval+1, 1);
		
		if(labelval < 9)
		{
			Mat<float> output( nn.feedForward( input) );
	
			int idmax = idmin( (-1.0f)*output).get(1,1);
	
			transpose( operatorL(target,output) ).afficher();
	
			std::cout << " LEARNING ITERATION : " << iteration << " ; IDMAX = " << idmax << std::endl;
	
	
			nn.backProp(target);
			//nn.backPropCrossEntropy(target);
			
			
			//counting :
			if(idmax == labelval+1)
			{
				countSuccess++;
			}
			
			//-------------------
			
			if( iteration % 1000 == 0)
			{
				std::cout << " TEST : " << countSuccess << " / " << 1000 << std::endl;
				mse.push_back(Mat<float>((float)countSuccess,1,1));
		
				writeInFile(std::string("./mse.txt"), mse);
		
				countSuccess = 0;
			}
			
			iteration--;
			
			
		}
		
		
		
	}
	
	std::cout << " VALIDATION TEST : in progress.." << std::endl;
	
	iteration = 1000;
	int success = 0;
	while( iteration)
	{
		Mat<float> rotatedinput( inputMNIST(labelval) );
		//let us choose the rotation :
		//float theta = rand()%360;
		float theta = ((float)(rand()%360))*PI/180.0f;
		
		//let us apply it :
		rotatedinput = extract( rotate(rotatedinput, theta), offx,offy, offx+(size-1), offy+(size-1) );
		
		Mat<float> input( reshapeV( rotatedinput ) );
		Mat<float> target( 0.0f, 10,1);
		target.set( 1.0f, labelval+1, 1);
		
		if(labelval < 5)
		{
			Mat<float> output( nn.feedForward( input));
			int idmax = idmin( (-1.0f)*output).get(1,1);
		
			transpose(output).afficher();
			std::cout << " ITERATION : " << iteration << " ; IDMAX = " << idmax << std::endl;
		
			if(idmax == labelval+1)
			{
				success++;
			}
			
			iteration--;
		}
		
	}
	
	std::cout << "VALIDATION TEST : " << success << " / 1000." << std::endl;
	
	report.close();
	label.close();
	image.close();
	
	nn.save(std::string("neuralnetworksDIGITROTATED"));
		
	return 0;
}
Exemple #30
0
int main(int argc, char **argv) {
/*    NeuralNetwork nn(Eigen::Vector4d(3,3,3,1), 0.2, 0.1);

    typedef Eigen::Matrix<double,1,1> Vector1d;
    Vector1d rtrue;
    rtrue << 1;
    Vector1d rfalse;
    rfalse << 0;

    int NUM_ITERS = 100000;


    std::cout << "Beginning learning phase...\niteration 0";
    
    
    
    std::vector<std::pair<Eigen::VectorXd, Eigen::VectorXd>> training_data;
    training_data.push_back(std::make_pair<Eigen::VectorXd, Eigen::VectorXd>(Eigen::Vector3d(1,1,1), rtrue));
    training_data.push_back(std::make_pair<Eigen::VectorXd, Eigen::VectorXd>(Eigen::Vector3d(1,0,0), rtrue));
    training_data.push_back(std::make_pair<Eigen::VectorXd, Eigen::VectorXd>(Eigen::Vector3d(0,1,0), rtrue));
    training_data.push_back(std::make_pair<Eigen::VectorXd, Eigen::VectorXd>(Eigen::Vector3d(0,0,1), rtrue));
    training_data.push_back(std::make_pair<Eigen::VectorXd, Eigen::VectorXd>(Eigen::Vector3d(1,1,0), rfalse));
    training_data.push_back(std::make_pair<Eigen::VectorXd, Eigen::VectorXd>(Eigen::Vector3d(0,1,1), rfalse));
    training_data.push_back(std::make_pair<Eigen::VectorXd, Eigen::VectorXd>(Eigen::Vector3d(1,0,1), rfalse));
    training_data.push_back(std::make_pair<Eigen::VectorXd, Eigen::VectorXd>(Eigen::Vector3d(0,0,0), rfalse));
    
    nn.TrainBatch(training_data, 100000, 0.000001);
    
    std::cout <<"Learning done."<< std::endl;
    std::cout <<"Beginning testing phase...\n";

    std::cout << nn.Evaluate(Eigen::Vector3d(1,1,1)) << " vs " << 1 << std::endl;
    std::cout << nn.Evaluate(Eigen::Vector3d(1,1,0)) << " vs " << 0 << std::endl;
    std::cout << nn.Evaluate(Eigen::Vector3d(1,0,0)) << " vs " << 1 << std::endl;
    std::cout << nn.Evaluate(Eigen::Vector3d(1,0,1)) << " vs " << 0 << std::endl;
    std::cout << nn.Evaluate(Eigen::Vector3d(0,1,1)) << " vs " << 0 << std::endl;
    std::cout << nn.Evaluate(Eigen::Vector3d(0,1,0)) << " vs " << 1 << std::endl;
    std::cout << nn.Evaluate(Eigen::Vector3d(0,0,1)) << " vs " << 1 << std::endl;
    std::cout << nn.Evaluate(Eigen::Vector3d(0,0,0)) << " vs " << 0 << std::endl;
*/

    Reader reader;
    Eigen::VectorXd layers(4);
    layers << 10, 10, 5,1;
    NeuralNetwork nn(layers, 0.2, 0.1);
    
    std::cout << "Reading training data..." << std::flush;
    std::vector<std::pair<Eigen::VectorXd, Eigen::VectorXd>> training_data;
    std::cout << reader.ReadTrainingData("training_sample.csv",10,1,training_data) << " lines read." << std::endl;
    
    std::cout << "Beginning learning phase..." << std::endl;
    nn.TrainBatch(training_data, 50000, 0.001);
    std::cout << "End training phase..." << std::endl;
    nn.PrintWeights();
    std::cout << "Reading evaluation data..." << std::flush;
    std::vector<Eigen::VectorXd> evaluation_input_data;
    std::vector<Eigen::VectorXd> evaluation_output_data;
    
    int evaluation_rowcount = 0;
    std::cout << (evaluation_rowcount = reader.ReadEvaluationData("training_normalized.csv",10,1,evaluation_input_data, evaluation_output_data)) << " lines read." << std::endl;
    std::cout << "Beginning testing phase..." << std::endl;
    double cumulative_mse = 0;
    int success_count = 0;
    double success_square_error_threshold = 0.16;
    for(int i = 0; i < evaluation_rowcount; ++i) {
      Eigen::VectorXd output = nn.Evaluate(evaluation_input_data[i]);
      Eigen::VectorXd error = evaluation_output_data[i] - output;
      double square_error = error.cwiseProduct(error).sum() / error.size();
      cumulative_mse += square_error;
      if(square_error < success_square_error_threshold) {
	++success_count;
      }
      
      //std::cout << nn.Evaluate(evaluation_input_data[i]) << " vs. " << evaluation_output_data[i] << std::endl;
    }
    
    nn.PrintWeights();
    nn.PrintStates();
    
    
    std::cout << "End testing phase. Mean square error " << cumulative_mse/evaluation_rowcount << "\n";
    std::cout << success_count << " / " << evaluation_rowcount << " rows classified correctly (mse < " << success_square_error_threshold << ")\n";

    //std::cout << nn.Evaluate(evaluation_input_data[0]) << " vs. " << evaluation_output_data[0] << std::endl;
    //nn.PrintStates();
    //std::cout << nn.Evaluate(evaluation_input_data[1]) << " vs. " << evaluation_output_data[1] << std::endl;
    //nn.PrintStates();
    //std::cout << nn.Evaluate(evaluation_input_data[4]) << " vs. " << evaluation_output_data[4] << std::endl;
    //nn.PrintStates();
    
    return 0;
}