Exemplo n.º 1
0
void Mat::Filter(const Mat& filter, Mat &filtered, bool type) const {  
  if (!type) { // valid filtration
    filtered.resize(mat_.size1()+1-filter.size1(), mat_.size2()+1-filter.size2());
    for (size_t i = 0; i < filtered.size1(); ++i) {
      for (size_t j = 0; j < filtered.size2(); ++j) {
        filtered(i, j) = 0;
        for (size_t u = 0; u < filter.size1(); ++u) {
          for (size_t v = 0; v < filter.size2(); ++v) {
            filtered(i, j) += filter(u, v) * mat_(i+u, j+v);
          }        
        }
      }
    }
  } else { // full filtration    
    filtered.resize(mat_.size1()+filter.size1()-1, mat_.size2()+filter.size2()-1);
    for (long i = 0; i < filtered.size1(); ++i) {
      for (long j = 0; j < filtered.size2(); ++j) {
        filtered(i, j) = 0;
        size_t minu = std::max((long) 0, (long) filter.size1() - i - 1);
        size_t minv = std::max((long) 0, (long) filter.size2() - j - 1);
        size_t maxu = std::min((long) filter.size1(), (long) filtered.size1() - i);
        size_t maxv = std::min((long) filter.size2(), (long) filtered.size2() - j);
        for (size_t u = minu; u < maxu; ++u) {
          for (size_t v = minv; v < maxv; ++v) {
            filtered(i, j) += filter(u, v) * 
              mat_(i+u+1-filter.size1(), j+v+1-filter.size2());            
          }        
        }
      }
    }
  }
}
Exemplo n.º 2
0
void Mat::SubMat(const std::vector<size_t> ind, size_t dim, Mat &submat) const {
  
  mexAssert(ind.size() > 0, "In SubMat the index vector is empty");
  size_t minind = *(std::min_element(ind.begin(), ind.end()));
  mexAssert(minind >= 0, "In SubMat one of the indices is less than zero");    
  if (dim == 1) {
    size_t maxind = *(std::max_element(ind.begin(), ind.end()));    
    mexAssert(maxind < mat_.size1(), "In SubMat one of the indices is larger than the array size");    
    submat.resize(ind.size(), mat_.size2());
    for (size_t i = 0; i < ind.size(); ++i) {
      for (size_t j = 0; j < mat_.size2(); ++j) {
        submat(i, j) = mat_(ind[i], j);
      }
    }
  } else if (dim == 2) {
    size_t maxind = *(std::max_element(ind.begin(), ind.end()));    
    mexAssert(maxind < mat_.size2(), "In SubMat one of the indices is larger than the array size");
    submat.resize(mat_.size1(), ind.size());
    for (size_t i = 0; i < mat_.size1(); ++i) {
      for (size_t j = 0; j < ind.size(); ++j) {
        submat(i, j) = mat_(i, ind[j]);
      }
    }    
  } else {
    mexAssert(false, "In Mat::SubMat the second parameter must be either 1 or 2");
  }
}
Exemplo n.º 3
0
void Mat::MaxTrim(Mat &trimmed, std::vector<size_t> &coords) const {
  
  mexAssert(trimmed.size1() <= mat_.size1(), "In 'Mat::Trim' the trimmed image is larger than original");
  mexAssert(trimmed.size2() <= mat_.size2(), "In 'Mat::Trim' the trimmed image is larger than original");
  size_t lv = std::floor((double) (trimmed.size1() - 1)/2);  
  size_t lh = std::floor((double) (trimmed.size2() - 1)/2);  
  
  double maxval = mat_(lv, lh);
  coords[0] = lv;
  coords[1] = lh;
  for (size_t i = lv; i < lv + mat_.size1() - trimmed.size1(); ++i) {
    for (size_t j = lh; j < lh + mat_.size2() - trimmed.size2(); ++j) {      
      if (maxval < mat_(i, j)) {
        maxval = mat_(i, j);
        coords[0] = i;
        coords[1] = j;
      }
    }
  }  
  for (size_t i = 0; i < trimmed.size1(); ++i) {
    for (size_t j = 0; j < trimmed.size2(); ++j) {
      trimmed(i, j) = mat_(coords[0]+i-lv, coords[1]+j-lh);      
    }
  }  
}
Exemplo n.º 4
0
Mat& Mat::AddVect(const Mat &vect, size_t dim) {
  
  if (dim == 1) {
    mexAssert(vect.size2() == 1, "In 'Mat::AddVect' the second dimension must be 1"); 
    mexAssert(mat_.size1() == vect.size1(),
      "In 'Mat::AddVect' the second dimension of matrix and length of vector are of the different size");
    for (size_t i = 0; i < mat_.size1(); ++i) {
      for (size_t j = 0; j < mat_.size2(); ++j) {
        mat_(i, j) += vect(i, 0);
      }
    }   
  } else if (dim == 2) {
    mexAssert(vect.size1() == 1, "In 'Mat::AddVect' the first dimension must be 1"); 
    mexAssert(mat_.size2() == vect.size2(),
      "In 'Mat::AddVect' the first dimension of matrix and length of vector are of the different size");
    for (size_t i = 0; i < mat_.size1(); ++i) {
      for (size_t j = 0; j < mat_.size2(); ++j) {
        mat_(i, j) += vect(0, j);
      }
    }
  } else {
    mexAssert(false, "In Mat::AddVect the dimension parameter must be either 1 or 2");
  }
  return *this;
}
Exemplo n.º 5
0
std::vector<size_t> Mat::MaxInd(size_t dim) const {
  std::vector<size_t> arrmax;
  if (dim == 1) {
    arrmax.assign(mat_.size2(), 0);    
    for (size_t i = 0; i < mat_.size1(); ++i) {
      for (size_t j = 0; j < mat_.size2(); ++j) {
        if (mat_(i, j) > mat_(arrmax[j], j)) {
          arrmax[j] = i;
        }        
      }
    }    
  } else if (dim == 2) {
    arrmax.assign(mat_.size1(), 0);
    for (size_t i = 0; i < mat_.size1(); ++i) {
      for (size_t j = 0; j < mat_.size2(); ++j) {
        if (mat_(i, j) > mat_(i, arrmax[i])) {
          arrmax[i] = j;
        }
      }     
    }    
  } else {
    mexAssert(false, "In Mat::MaxInd the dimension parameter must be either 1 or 2");
  }
  return arrmax;
}
Exemplo n.º 6
0
Mat& Mat::Sigmoid() {
  for (size_t i = 0; i < mat_.size1(); ++i) {
    for (size_t j = 0; j < mat_.size2(); ++j) {
      mat_(i, j) = 1 / (1 + exp(-mat_(i, j)));
    }
  }
  return *this;
}
Exemplo n.º 7
0
Mat& Mat::ElemMax(double a) {
  for (size_t i = 0; i < mat_.size1(); ++i) {
    for (size_t j = 0; j < mat_.size2(); ++j) {      
      if (mat_(i, j) < a) mat_(i, j) = a;
    }
  }
  return *this;
}
Exemplo n.º 8
0
const double& Mat::operator () (size_t ind) const {
  if (mat_.size1() == 1) {    
    return mat_(0, ind);
  } else if (mat_.size2() == 1) {    
    return mat_(ind, 0);
  } else {
    mexAssert(false, "In 'Mat::(ind)' matrix is not really a vector");     
  }
}
Exemplo n.º 9
0
Mat& Mat::Sign() {
  for (size_t i = 0; i < mat_.size1(); ++i) {
    for (size_t j = 0; j < mat_.size2(); ++j) {
      if (mat_(i, j) > 0) {
        mat_(i, j) = 1;
      } else if (mat_(i, j) < 0) {
        mat_(i, j) = -1;
      } else {
        mat_(i, j) = 0;
      }
    }
  }
  return *this;
}
Exemplo n.º 10
0
Mat& Mat::ReshapeFrom(const std::vector< std::vector<Mat> > &squeezed) {
  // stretches 1, 3 and 4 dimensions size_to the 1st one. The 2nd dimension stays the same.
  size_t outputmaps = squeezed.size();
  mexAssert(outputmaps > 0, "In 'Mat::ReshapeFrom' the number of output maps is zero");
  size_t batchsize = squeezed[0].size();
  mexAssert(batchsize > 0, "In 'Mat::ReshapeFrom' the number of batches is zero");
  size_t mapsize[2];
  mapsize[0] = squeezed[0][0].size1();
  mapsize[1] = squeezed[0][0].size2();
  size_t numel = mapsize[0] * mapsize[1];
  mat_.resize(outputmaps * numel, batchsize);  
  for (size_t j = 0; j < outputmaps; ++j) {
    for (size_t k = 0; k < batchsize; ++k) {
      mexAssert(squeezed[j][k].size1() == mapsize[0], "In 'Mat::ReshapeFrom' the first dimension is not constant");
      mexAssert(squeezed[j][k].size2() == mapsize[1], "In 'Mat::ReshapeFrom' the second dimension is not constant");
      for (size_t u = 0; u < mapsize[0]; ++u) {
        for (size_t v = 0; v < mapsize[1]; ++v) {            
          size_t ind = j*numel + u*mapsize[1] + v;
          mat_(ind, k) = squeezed[j][k](u, v);
        }
      }
    }      
  }
  return *this;
}
Exemplo n.º 11
0
Mat& Mat::operator += (double a) {
  for (size_t i = 0; i < mat_.size1(); ++i) {
    for (size_t j = 0; j < mat_.size2(); ++j) {      
      mat_(i, j) += a;
    }
  }
  return *this;
}
Exemplo n.º 12
0
void Mat::MaxScale(const std::vector<size_t> &scale, Mat &scaled) const {
  
  for (size_t i = 0; i < scaled.size1(); ++i) {
    for (size_t j = 0; j < scaled.size2(); ++j) {
      size_t maxu = std::min(scale[0],  mat_.size1()-i*scale[0]);
      size_t maxv = std::min(scale[1],  mat_.size2()-j*scale[1]);
      scaled(i, j) = mat_(i*scale[0], j*scale[1]);
      for (size_t u = 0; u < maxu; ++u) {
        for (size_t v = 0; v < maxv; ++v) {          
          if (scaled(i, j) < mat_(i*scale[0]+u, j*scale[1]+v)) {
            scaled(i, j) = mat_(i*scale[0]+u, j*scale[1]+v);
          }
        }
      }      
    }
  }
}
Exemplo n.º 13
0
Mat& Mat::CondProd(const Mat &condmat, double threshold, bool incase, double a) {
  for (size_t i = 0; i < mat_.size1(); ++i) {
    for (size_t j = 0; j < mat_.size2(); ++j) {      
      if (incase == (condmat(i, j) > threshold)) mat_(i, j) *= a; // xor
    }
  }
  return *this;
}
Exemplo n.º 14
0
Mat& Mat::Rand() {
  for (size_t i = 0; i < mat_.size1(); ++i) {
    for (size_t j = 0; j < mat_.size2(); ++j) {
      mat_(i, j) = (double) rand() / RAND_MAX;
    }
  }
  return *this;
}
Exemplo n.º 15
0
Mat& Mat::assign(double val) {
  for (size_t i = 0; i < mat_.size1(); ++i) {
    for (size_t j = 0; j < mat_.size2(); ++j) {
      mat_(i, j) = val;
    }
  }
  return *this;
}
Exemplo n.º 16
0
double Mat::Sum() const {
  double matsum = 0;
  for (size_t i = 0; i < mat_.size1(); ++i) {
    for (size_t j = 0; j < mat_.size2(); ++j) {
      matsum += mat_(i, j);
    }
  }      
  return matsum;  
}
Exemplo n.º 17
0
std::vector<double> Mat::ToVect() const {
  std::vector<double> vect(mat_.size1() * mat_.size2());  
  for (size_t i = 0; i < mat_.size1(); ++i) {
    for (size_t j = 0; j < mat_.size2(); ++j) {      
      vect[i*mat_.size2()+j] = mat_(i, j);
    }
  }
  return vect;
}
Exemplo n.º 18
0
Mat& Mat::SigmDer(const Mat& a) {
  mexAssert(mat_.size1() == a.size1() && mat_.size2() == a.size2(), 
    "In 'Mat::SigmDer' the matrices are of the different size");
  for (size_t i = 0; i < mat_.size1(); ++i) {
    for (size_t j = 0; j < mat_.size2(); ++j) {
      mat_(i, j) *= a(i, j) * (1 - a(i, j));
    }
  }
  return *this;
}  
Exemplo n.º 19
0
void Mat::Sum(size_t dim, Mat &vect) const {
  
  if (dim == 1) {
    vect.init(1, mat_.size2(), 0);
    for (size_t i = 0; i < mat_.size1(); ++i) {
      for (size_t j = 0; j < mat_.size2(); ++j) {
        vect(0, j) += mat_(i, j);
      }
    }    
  } else if (dim == 2) {    
    vect.init(mat_.size1(), 1, 0);
    for (size_t i = 0; i < mat_.size1(); ++i) {
      for (size_t j = 0; j < mat_.size2(); ++j) {
        vect(i, 0) += mat_(i, j);
      }     
    }    
  } else {
    mexAssert(false, "In Mat::Sum the dimension parameter must be either 1 or 2");
  }  
}
Exemplo n.º 20
0
Mat& Mat::FromVect(const std::vector<double> &vect, const std::vector<size_t> &newsize) {
  mexAssert(vect.size() == newsize[0] * newsize[1], 
    "In 'Mat::FromVect' the vector and sizes do not correspond");
  mat_.resize(newsize[0], newsize[1]);
  for (size_t i = 0; i < mat_.size1(); ++i) {
    for (size_t j = 0; j < mat_.size2(); ++j) {      
      mat_(i, j) = vect[i*mat_.size2()+j];
    }
  }
  return *this;
}
Exemplo n.º 21
0
Mat& Mat::MultVect(const std::vector<double> &vect, size_t dim) {
  
  if (dim == 1) {
    mexAssert(mat_.size1() == vect.size(),
      "In 'Mat::MultVect' the second dimension of matrix and length of vector are of the different size");
    for (size_t i = 0; i < mat_.size1(); ++i) {
      for (size_t j = 0; j < mat_.size2(); ++j) {
        mat_(i, j) *= vect[i];
      }
    }   
  } else if (dim == 2) {
    mexAssert(mat_.size2() == vect.size(),
      "In 'Mat::MultVect' the first dimension of matrix and length of vector are of the different size");
    for (size_t i = 0; i < mat_.size1(); ++i) {
      for (size_t j = 0; j < mat_.size2(); ++j) {
        mat_(i, j) *= vect[j];
      }
    }
  } else {
    mexAssert(false, "In Mat::MultVect the dimension parameter must be either 1 or 2");
  }
  return *this;
}
Exemplo n.º 22
0
void Mat::MeanScaleDer(const std::vector<size_t> &scale, Mat &scaled) const {

  for (size_t i = 0; i < mat_.size1(); ++i) {
    for (size_t j = 0; j < mat_.size2(); ++j) {
      size_t maxu = std::min(scale[0], scaled.size1()-i*scale[0]);
      size_t maxv = std::min(scale[1], scaled.size2()-j*scale[1]);      
      double scaled_val = mat_(i, j) / (maxu * maxv);
      for (size_t u = 0; u < maxu; ++u) {
        for (size_t v = 0; v < maxv; ++v) {          
          scaled(i*scale[0]+u, j*scale[1]+v) = scaled_val;
        }
      }      
    }
  }
}
Exemplo n.º 23
0
void Mat::MaxScaleDer(const std::vector<size_t> &scale, const Mat &val, const Mat &prev_val, Mat &scaled) const {
  
  for (size_t i = 0; i < mat_.size1(); ++i) {
    for (size_t j = 0; j < mat_.size2(); ++j) {
      size_t maxu = std::min(scale[0], scaled.size1()-i*scale[0]);
      size_t maxv = std::min(scale[1], scaled.size2()-j*scale[1]);      
      for (size_t u = 0; u < maxu; ++u) {
        for (size_t v = 0; v < maxv; ++v) {          
          if (prev_val(i*scale[0]+u, j*scale[1]+v) == val(i, j)) {
            scaled(i*scale[0]+u, j*scale[1]+v) = mat_(i, j);
          } else {
            scaled(i*scale[0]+u, j*scale[1]+v) = 0;
          }
        }
      }      
    }
  }
}
Exemplo n.º 24
0
void Mat::MaxRestore(Mat &restored, const std::vector<size_t> &coords) const {
  
  mexAssert(restored.size1() >= mat_.size1(), "In 'Mat::MaxRestore' the restored image is smaller than original");
  mexAssert(restored.size2() >= mat_.size2(), "In 'Mat::MaxRestore' the restored image is smaller than original");
  size_t lv = std::floor((double) (mat_.size1() - 1)/2);  
  size_t lh = std::floor((double) (mat_.size2() - 1)/2);  
  
  for (size_t i = 0; i < restored.size1(); ++i) {
    for (size_t j = 0; j < restored.size2(); ++j) {
      if (coords[0] <= i + lv && i + lv < coords[0] + mat_.size1() &&
          coords[1] <= j + lh && j + lh < coords[1] + mat_.size2()) {
        restored(i, j) = mat_(i+lv-coords[0], j+lh-coords[1]);
      } else {
        restored(i, j) = 0;
      }
    }
  }
}
Exemplo n.º 25
0
void Mat::ReshapeTo(std::vector< std::vector<Mat> > &squeezed,
                    size_t outputmaps, size_t batchsize, 
                    const std::vector<size_t> &mapsize) const {
  size_t numel = mapsize[0] * mapsize[1];
  mexAssert(outputmaps * numel == mat_.size1(), "In 'Mat::ReshapeTo' the first dimension do not correspond the parameters");
  mexAssert(batchsize == mat_.size2(), "In 'Mat::ReshapeTo' the second dimension do not correspond the parameters");
  squeezed.resize(outputmaps);
  for (size_t j = 0; j < outputmaps; ++j) {
    squeezed[j].resize(batchsize);
    for (size_t k = 0; k < batchsize; ++k) {
      squeezed[j][k].resize(mapsize);
      for (size_t u = 0; u < mapsize[0]; ++u) {
        for (size_t v = 0; v < mapsize[1]; ++v) {            
          size_t ind = j*numel + u*mapsize[1] + v;
          squeezed[j][k](u, v) = mat_(ind, k);
        }
      }
    }      
  }
}
Exemplo n.º 26
0
const double& Mat::operator() (size_t ind1, size_t ind2) const {
  return mat_(ind1, ind2);
}
Exemplo n.º 27
0
/** execution method of primal heuristic */
static
SCIP_DECL_HEUREXEC(heurExecForward)
{  /*lint --e{715}*/
	
   SCIP_PROBDATA* probdata;
	int	n;
	int	p;
	int	ndep;

	/* "_" means the matrix for blas */
	SCIP_Real*	y;				/* [n] */
	SCIP_Real*	orig_X_;		/* [n*p] */
	SCIP_Real*	orig_Q_;		/* [p*p] <- (X^t) X */
	SCIP_Real*	orig_q;		/* [p]   <- (X^t) y */
	SCIP_Real	r;

	int*	Mdep;					/* [ndep] */
	int*	groupX;				/* [ndep*p] */

	/* for forward selection */
	int	dim;
	int*	list;					/* [p] */
	SCIP_Real*	a;				/* [dim] */
	SCIP_Real*	a_old;		/* [dim-1] */
	SCIP_Real*	a_new;		/* [dim] */
	SCIP_Real	RSS;			/* residual sum of square */
	SCIP_Real	RSS_new;
	SCIP_Real	AIC;
	SCIP_Real	AIC_new;

	int	ublb;
	int	*Branchz;		/* [3*p] */

	/*
	 *	X: sub matrix of orig_X_ 
	 *	Y:	(X^t X)^-1 
	 * X_new = (X, x_i);
	 * Z: (X_new ^t X_new)^-1
	 *		= ( V   v
	 			 v^t u )
	 */

	SCIP_Real*	Xy;	/* sub vector of orig_q */ 
	SCIP_Real*	X_;	
	SCIP_Real*	Y_;	/* [(dim-1)*(dim-1)] */
	SCIP_Real*	Z_;	/* [dim*dim] */
	SCIP_Real*	W_;	/* [dim*dim] */
	SCIP_Real*	V_;	/* [(dim-1)*(dim-1)] */
	SCIP_Real*	v;		/* [dim-1] */
	SCIP_Real	u;
	
	SCIP_Real*	b;		/* [dim-1] */
	SCIP_Real*	c;		/* [dim-1] */
	SCIP_Real*	d;		/* [n] */

	/* variables */
	SCIP_VAR**	var_a;		/* [p] continuous variables */
	SCIP_VAR**	var_z;		/* [p] 01 variables */
	SCIP_VAR**	var_ep;		/* [n] continuous variables */
	SCIP_VAR*	var_rss;		/* continuous variable, residual sum of squares */
	SCIP_VAR*	var_log;		/* continuous variable, log(rss) */

	/* set solution */ 
	SCIP_Real *ep;
	
	int	nsols;
	int	store;
	SCIP_SOL**	sols;
	SCIP_Real	objval;

	SCIP_SOL*	sol;
	SCIP_Real*	solvals;
	SCIP_Bool	success;
	int			nvars	=	SCIPgetNVars(scip);
	SCIP_VAR**	vars;

	int 	i,j,t,ct;
	int	memo;

   assert(heur != NULL);
   assert(scip != NULL);
   assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
   assert(result != NULL);

#if MYPARA_LOG
	printf("forward selection!\n");
#endif

   /* get heuristic data */
	/*
   SCIP_HEURDATA* heurdata;
   heurdata = SCIPheurGetData(heur);
   assert(heurdata != NULL);
   assert(lastsolindices != NULL);
	*/

	/* get values from probdata */
   probdata = SCIPgetProbData(scip);
   assert(probdata != NULL);

	n	=	SCIPprobdataGetNdatas(probdata);
	p	=	SCIPprobdataGetNexvars(probdata);
	ndep	=	SCIPprobdataGetNdep(probdata);

	y	=	SCIPprobdataGety(probdata);
	orig_X_	=	SCIPprobdataGetX(probdata);
	orig_Q_	=	SCIPprobdataGetQ(probdata);
	orig_q	=	SCIPprobdataGetq(probdata);
	r	=	SCIPprobdataGetr(probdata);

	if( ndep ){
		Mdep		=	SCIPprobdataGetMdep(probdata);
		groupX	=	SCIPprobdataGetgroupX(probdata);
	}else{
		Mdep		=	NULL;
		groupX	=	NULL;
	}

	/* variables */
	var_a		=	SCIPprobdataGetVars_a(probdata);
	var_z		=	SCIPprobdataGetVars_z(probdata);
	var_ep	=	SCIPprobdataGetVars_ep(probdata);
	var_rss	=	SCIPprobdataGetVar_rss(probdata);
	var_log	=	SCIPprobdataGetVar_log(probdata);

	/* get branching info */
	/* alloc */
	SCIP_CALL( SCIPallocBufferArray(scip, &Branchz, 3*p));
	
	GenerateZeroVecInt( 3*p, Branchz);

	for(i=0; i<p; ++i){
		ublb					=	SCIPround(scip, SCIPcomputeVarUbLocal(scip, var_z[i]) 
								+	SCIPcomputeVarLbLocal(scip, var_z[i]));
		*(Branchz+(ublb*p)+i) 	= 	1;
	}

#if MYPARA_LOG
	for(i=0; i<3; i++){
		for(j=0; j<p; j++){
			printf("%d, ", *(Branchz+(i*p)+j));
		}
		newline();
	}
#endif

	if( ndep ){
		for(i=0; i<ndep; i++){
			memo = -1; 
			for(j=0; j<p; j++){
				if( *(groupX+(i*p)+j)==1 ){
					if( *(Branchz+j)==1 ) break;
					if( *(Branchz+p+j)==1 ) memo=j;
					if( j==Mdep[i] ){
						if( memo==-1 ){
							printf("error in heur_backward.c\n");
							stop();
						}
						*(Branchz+p+memo) = 0;
						*(Branchz+memo) = 1;
						break;
					}
				}
			}
		}
	}
	
#if MYPARA_LOG
	printf("linear dependent\n");
	if( ndep ){
		for(i=0; i<3; i++){
			for(j=0; j<p; j++){
				printf("%d, ", *(Branchz+(i*p)+j));
			}
			newline();
		}
	}
#endif
	
	/* alloc */
	SCIP_CALL( SCIPallocBufferArray(scip, &X_, n*p));
	SCIP_CALL( SCIPallocBufferArray(scip, &Xy, p));
	SCIP_CALL( SCIPallocBufferArray(scip, &d, n));
	SCIP_CALL( SCIPallocBufferArray(scip, &list, p));
	
	/* initialize from Branchz */
#if MYPARA_LOG
	printf("initialization\n");
#endif

	GenerateZeroVecInt( p, list);

	dim = 0;
	memo = -1;
	AIC = 1e+06;
	SCIP_CALL( SCIPallocBufferArray(scip, &a_old, dim+1));

	for(i=0; i<p; i++){
		
		if( Branchz[i]==1 ){ /* if z_i is fixed to 0 */
			list[i] = -1;
		}else if( Branchz[p+i]==1 ){ /* if z_i is unfixed */
			list[i] = 0;
		}else if( Branchz[2*p+i]==1 ){ /* if z_i is fixed 1 */
			dim++;
			list[i] = dim;

			if( dim == 1 ){

				a_old[0] = orig_q[i] / mat_( orig_Q_, p, i, i);
				RSS = RSSvalue( 1, a_old, &orig_q[i], r);
				AIC = AICvalue( n, dim, RSS);

				/* update X_ and Xy */
				mydcopy_( &orig_X_[n * i], &X_[n * (dim-1)], n);
				Xy[dim-1] = orig_q[i];

				/* generate Y ( dim = 1 ) */
				SCIP_CALL( SCIPallocBufferArray( scip, &Y_, dim*dim));
				Y_[0] = 1 / mat_( orig_Q_, p, i, i);
		
			}else{
				/* alloc */
				SCIPfreeBufferArray(scip, &a_old);
				SCIP_CALL( SCIPallocBufferArray( scip, &a_old, dim));
				SCIP_CALL( SCIPallocBufferArray( scip, &b, dim-1));
				SCIP_CALL( SCIPallocBufferArray( scip, &c, dim-1));
				SCIP_CALL( SCIPallocBufferArray( scip, &v, dim-1));
				SCIP_CALL( SCIPallocBufferArray( scip, &V_, (dim-1)*(dim-1)));
				SCIP_CALL( SCIPallocBufferArray( scip, &Z_, (dim)*(dim)));
				
				/* 1. b <- X^t x_i */
				dgemv_t( X_, n, dim-1, &orig_X_[n * i], b);
				//printv( dim-1, b);

				/* 2. c <- Y b */
				dgemv_2( Y_, dim-1, dim-1, b, c);
				//printv( dim-1, c);

				/* 3. d <- - X c + x_i */
				dgemv_1( X_, n, dim-1, c, &orig_X_[n * i], -1.0, 1.0, d);
				//printv( n, d);

				/* 4. u <- 1/<x_i, d> */
				u = 1.0 / myddot_( &orig_X_[n * i], d, n);
				//prints(u);
				
				/* 5. v <- - u c */
				mydscal_( c, dim-1, -u, v);
				//printv( dim-1, v);

				/* 6. V <- Y + u c c^t */
				dger_1( Y_, c, c, dim-1, dim-1, u, V_);
				//printM_( V_, dim-1, dim-1);

				/* 7. Z */
				/* V */
				for(j=0; j<(dim-1); j++){
					for(t=0; t<(dim-1); t++){
						*(Z_ + j + (t*dim) ) = mat_( V_, dim-1, j, t);
					}
				}
				/* v */
				for(j=0; j<(dim-1); j++){
					*(Z_ + dim-1 + (j*dim) )  = v[j];
					*(Z_ + j + ((dim-1)*dim)) = v[j];
				}

				/* u */
				*(Z_ + dim-1 + ((dim-1)*dim)) = u;
				//printM_( Z_, dim, dim);

				/* 8. a_old <- Z (Xy) */
				Xy[dim-1] = orig_q[i];
				dgemv_2( Z_, dim, dim, Xy, a_old);
				//printv( dim, a_old);

				RSS = RSSvalue( dim, a_old, Xy, r);
				AIC = AICvalue( n, dim, RSS);

				/* copy */
				SCIPfreeBufferArray(scip, &Y_);
				SCIP_CALL( SCIPallocBufferArray(scip, &Y_, dim*dim));
				mydcopy_( Z_, Y_, dim*dim);
	
				/* update X_ and Xy */
				mydcopy_( &orig_X_[n * i], &X_[n * (dim-1)], n);
				Xy[dim-1] = orig_q[i];

				/* free */
				SCIPfreeBufferArray(scip, &b);
				SCIPfreeBufferArray(scip, &c);
				SCIPfreeBufferArray(scip, &v);
				SCIPfreeBufferArray(scip, &V_);
				SCIPfreeBufferArray(scip, &Z_);
			}

#if MYPARA_LOG
			printf("---> %dth variable, AIC:%f\n", i, AIC);
#endif

		}else{
			printf("error:heur_forward.c\n");
			stop();
		}
	}


	if( dim == 0 ){
#if MYPARA_LOG
		printf("[dim:0]\n");
#endif
		dim++;
		RSS = 1e+06;
		for(i=0; i<p; i++){
			if( list[i] == 0 ){
				a_old[0] = orig_q[i] / mat_( orig_Q_, p, i, i);
				RSS_new = RSSvalue( 1, a_old, &orig_q[i], r);
				if( RSS_new < RSS ){
					RSS = RSS_new;
					memo = i;
				}
#if MYPARA_LOG
			printf("%d: RSS = %f\n", i, RSS_new);
#endif
			}
		}
		
		if( memo < 0 || memo >= p ){
			printf("error in heur_forward.c\n");
			stop();
		}
	
		AIC = AICvalue( n, dim, RSS);
		list[memo] = dim;
		
		/* update X_ and Xy */
		mydcopy_( &orig_X_[n * memo], &X_[n * (dim-1)], n);
		Xy[dim-1] = orig_q[memo];
	
		/* generate Y ( dim = 1 ) */
		SCIP_CALL( SCIPallocBufferArray( scip, &Y_, dim*dim));
		Y_[0] = 1 / mat_( orig_Q_, p, memo, memo);
	
#if MYPARA_LOG
		printf("---> %dth variable, AIC:%f\n", memo, AIC);
#endif
	} /* if ( dim==0 ) */

	while(1){
		dim++;
		memo = -1;
		RSS = 1e+06;
#if MYPARA_LOG
		printf("(dim=%d) ", dim);
		Longline();
#endif

		/* alloc */
		SCIP_CALL( SCIPallocBufferArray( scip, &a_new, dim));
		SCIP_CALL( SCIPallocBufferArray( scip, &a, dim));
		SCIP_CALL( SCIPallocBufferArray( scip, &b, dim-1));
		SCIP_CALL( SCIPallocBufferArray( scip, &c, dim-1));
		SCIP_CALL( SCIPallocBufferArray( scip, &v, dim-1));
		SCIP_CALL( SCIPallocBufferArray( scip, &V_, (dim-1)*(dim-1)));
		SCIP_CALL( SCIPallocBufferArray( scip, &Z_, (dim)*(dim)));
		SCIP_CALL( SCIPallocBufferArray( scip, &W_, (dim)*(dim)));
		
		for(i=0; i<p; i++){
			/*
			 * 1. b <- X^t x_i
			 * 2.	c <- Y b
			 * 3. d <- - X c + x_i
			 * 4. u <- 1 / <x_i, d> 
			 * 5. v <- - u c
			 * 6. V <- Y + u c c^t
			 * 7. Z <- ( V    v
			 	          v^t  u )
			 * 8. a_new <- Z (Xy)
			 */

			if( list[i]==0 ){

				/* 1. b <- X^t x_i */
				dgemv_t( X_, n, dim-1, &orig_X_[n * i], b);
				//printv( dim-1, b);

				/* 2. c <- Y b */
				dgemv_2( Y_, dim-1, dim-1, b, c);
				//printv( dim-1, c);

				/* 3. d <- - X c + x_i */
				dgemv_1( X_, n, dim-1, c, &orig_X_[n * i], -1.0, 1.0, d);
				//printv( n, d);

				/* 4. u <- 1/<x_i, d> */
				u = 1.0 / myddot_( &orig_X_[n * i], d, n);
				//prints(u);
				
				/* 5. v <- - u c */
				mydscal_( c, dim-1, -u, v);
				//printv( dim-1, v);

				/* 6. V <- Y + u c c^t */
				dger_1( Y_, c, c, dim-1, dim-1, u, V_);
				//printM_( V_, dim-1, dim-1);

				/* 7. Z */
				/* V */
				for(j=0; j<(dim-1); j++){
					for(t=0; t<(dim-1); t++){
						*(Z_ + j + (t*dim) ) = mat_( V_, dim-1, j, t);
					}
				}
				/* v */
				for(j=0; j<(dim-1); j++){
					*(Z_ + dim-1 + (j*dim) )  = v[j];
					*(Z_ + j + ((dim-1)*dim)) = v[j];
				}

				/* u */
				*(Z_ + dim-1 + ((dim-1)*dim)) = u;
				//printM_( Z_, dim, dim);

				/* 8. a_new <- Z (Xy) */
				Xy[dim-1] = orig_q[i];
				dgemv_2( Z_, dim, dim, Xy, a_new);
				//printv( dim, a_new);

				/* test */
				RSS_new = RSSvalue( dim, a_new, Xy, r);
				if( RSS_new < RSS ){
					RSS = RSS_new;
					memo = i;
					mydcopy_( Z_, W_, dim*dim);
					mydcopy_( a_new, a, dim);
				}

#if MYPARA_LOG
				printf("%d: RSS = %f\n", i, RSS_new);
#endif

			}
		}

		if( memo < 0 || memo >= p ){
			if( memo == -1 ){
				for(i=0; i<p; i++){
					if( list[i] == 0 ){
						memo = i;
						break;
					}
				}
				if( memo != -1 ){
					printf("error in heur_forward.c\n");
					stop();
				}
			}else{
				printf("error in heur_forward.c\n");
				stop();
			}
		}

		AIC_new = AICvalue( n, dim, RSS);
		if( AIC_new < AIC ){
			AIC = AIC_new;
			list[memo] = dim;

#if MYPARA_LOG
			printf("---> %dth variable, AIC:%f\n", memo, AIC);
#endif

			/* copy and free */
			SCIPfreeBufferArray(scip, &Y_);
			SCIP_CALL( SCIPallocBufferArray(scip, &Y_, dim*dim));
			mydcopy_( W_, Y_, dim*dim);

			SCIPfreeBufferArray(scip, &a_old);
			SCIP_CALL( SCIPallocBufferArray(scip, &a_old, dim));
			mydcopy_( a, a_old, dim);

			/* update X_ and Xy */
			mydcopy_( &orig_X_[n * memo], &X_[n * (dim-1)], n);
			Xy[dim-1] = orig_q[memo];

		}else{
			memo = -1;
			SCIPfreeBufferArray(scip, Y_);
#if MYPARA_LOG
			printf("--> no selection, (AIC:%f)\n", AIC_new);
#endif
		}

		/* free */
		SCIPfreeBufferArray(scip, &a_new);
		SCIPfreeBufferArray(scip, &a);
		SCIPfreeBufferArray(scip, &b);
		SCIPfreeBufferArray(scip, &c);
		SCIPfreeBufferArray(scip, &v);
		SCIPfreeBufferArray(scip, &V_);
		SCIPfreeBufferArray(scip, &Z_);
		SCIPfreeBufferArray(scip, &W_);

		if( memo == -1 ){
			dim--;
			break;
		}
	}

	nsols = SCIPgetNSols(scip);
	
	if( nsols < MP_NUM_SOL ){
		store = 1;
	}else{
		sols = SCIPgetSols(scip);
		objval = AIC;
		nsols = MP_NUM_SOL;

		if( objval < SCIPgetSolOrigObj(scip,sols[nsols-1]) ){
			store = 1;
		}else{
			store = 0;
		}
	}

	if( store ){
		/*  generate solution  */
		/* alloc */
		SCIP_CALL( SCIPallocBufferArray(scip, &ep, n));
		dgemv_1( X_, n, dim, a_old, y, -1.0, 1.0, ep);
		
	
		/* set solution */
		/* alloc */
		SCIP_CALL( SCIPallocBufferArray(scip, &solvals, nvars));
		SCIP_CALL( SCIPallocBufferArray(scip, &vars, nvars));
	
		ct=0;
		
		/* a */
		for(i=0; i<p; ++i){
			vars[ct] = var_a[i];
			if( list[i] > 0 ){
				solvals[ct] = a_old[list[i]-1];
			}else{
				solvals[ct] = 0.0;
			}
			ct++;
		}
	
		/* z */
		for(i=0; i<p; i++){
			vars[ct] = var_z[i];
			if( list[i] > 0 ){
				solvals[ct] = 1.0;
			}else{
				solvals[ct] = 0.0;
			}
			ct++;
		}
	
		/* ep */
		for(i=0; i<n; ++i){
			vars[ct]		=	var_ep[i];
			solvals[ct]	=	ep[i];
			ct++;
		}
	
		vars[ct]		=	var_rss;
		solvals[ct] =	myddot_( ep, ep, n);
		ct++;
	
		vars[ct]		=	var_log;
		solvals[ct]	=	log(myddot_( ep, ep, n));
		ct++;
	
		if( ct!=nvars ){
			SCIPerrorMessage("It is unexpected error in set sol,");
			printf("( ct, nvars) = ( %d, %d)", ct, nvars);
			stop();
		}
	
		SCIP_CALL( SCIPcreateSol(scip, &sol, heur));
		SCIP_CALL( SCIPsetSolVals(scip, sol, nvars, vars, solvals));
		SCIP_CALL( SCIPtrySolFree(scip, &sol, TRUE, FALSE, TRUE, TRUE, &success));

		/* free */
		SCIPfreeBufferArray(scip, &ep);
		SCIPfreeBufferArray(scip, &solvals);
		SCIPfreeBufferArray(scip, &vars);
	}

	/* free */
	SCIPfreeBufferArray(scip, &d);
	SCIPfreeBufferArray(scip, &X_);
	SCIPfreeBufferArray(scip, &Xy);
	SCIPfreeBufferArray(scip, &a_old);
	SCIPfreeBufferArray(scip, &list);
	SCIPfreeBufferArray(scip, &Branchz);

	*result = SCIP_FOUNDSOL;
   return SCIP_OKAY;
}
Exemplo n.º 28
0
void Pose::UpdateMat() {
    Matx33d rmat;
    Rodrigues(rvec_, rmat);

    mat_(0, 0) = rmat(0, 0);
    mat_(0, 1) = rmat(0, 1);
    mat_(0, 2) = rmat(0, 2);
    mat_(1, 0) = rmat(1, 0);
    mat_(1, 1) = rmat(1, 1);
    mat_(1, 2) = rmat(1, 2);
    mat_(2, 0) = rmat(2, 0);
    mat_(2, 1) = rmat(2, 1);
    mat_(2, 2) = rmat(2, 2);

    mat_(0, 3) = tvec_(0);
    mat_(1, 3) = tvec_(1);
    mat_(2, 3) = tvec_(2);
}
Exemplo n.º 29
0
static
SCIP_DECL_BRANCHEXECPS(branchExecpsMyfullstrong)
{  /*lint --e{715}*/

	SCIP_PROBDATA*	probdata;
   SCIP_VAR**	cands;
   int	ncands;
	SCIP_NODE*	childnode_0;		/* z_j = 0 */
	SCIP_NODE*	childnode_1;		/* z_j = 1 */

	/* probdata */
	int	p;
	int	ndep;
	SCIP_VAR**	var_z;		/* [p] 01 variables */

	/* "_" means the matrix for blas */
	SCIP_Real*	orig_Q_;		/* [p*p] <- (X^t) X */
	SCIP_Real*	orig_q;		/* [p]   <- (X^t) y */
	SCIP_Real	r;

	int*	Mdep;					/* [ndep] */
	int*	groupX;				/* [ndep*p] */

	int	dim;
	SCIP_Real	RSS;			/* residual sum of square */
	SCIP_Real	RSS_new;
	SCIP_Real*	a;				/* [dim] */

	int	ublb;
	int	*Branchz;			/* [3*p] */
	int	*Branchz_new;		/* [3*p] */

	SCIP_Real*	Q_;	/* sub matrix of orig_Q_ */
	SCIP_Real*	Xy;	/* sub vector of orig_q */ 

	int*	list;			/* list of candidate variables */

	int	i,j,t,memo,ct;
	int	ind;
	int	dpv;

#if MYPARA_LOG
	printf("[myfullstrong brnaching]");
	Longline();
#endif

   /* get branching rule data */
	/*
   SCIP_BRANCHRULEDATA* branchruledata;
   branchruledata = SCIPbranchruleGetData(branchrule);
   assert(branchruledata != NULL);
	*/
	
	/* get problem data*/
	probdata = SCIPgetProbData(scip);
   assert(probdata != NULL);

	p	=	SCIPprobdataGetNexvars(probdata);
	ndep	=	SCIPprobdataGetNdep(probdata);

	orig_Q_	=	SCIPprobdataGetQ(probdata);
	orig_q	=	SCIPprobdataGetq(probdata);
	r	=	SCIPprobdataGetr(probdata);
	var_z		=	SCIPprobdataGetVars_z(probdata);

	if( ndep ){
		Mdep		=	SCIPprobdataGetMdep(probdata);
		groupX	=	SCIPprobdataGetgroupX(probdata);
	}else{
		Mdep		=	NULL;
		groupX	=	NULL;
	}

	/* alloc */
	SCIP_CALL( SCIPallocBufferArray(scip, &list, p));
	SCIP_CALL( SCIPallocBufferArray(scip, &Branchz, 3*p));
	SCIP_CALL( SCIPallocBufferArray(scip, &Branchz_new, 3*p));

	
	GenerateZeroVecInt( p, list);
	GenerateZeroVecInt( 3*p, Branchz);

   /* get pseudo candidates (non-fixed integer variables) */
   SCIP_CALL( SCIPgetPseudoBranchCands(scip, &cands, NULL, &ncands) );
	
	for(i=0; i<ncands; i++){
		for(j=0; j<p; j++){
			if( cands[i]==var_z[j] ){
				list[j] = 1;
				break;	
			}
		}
	}

#if MYPARA_LOG
	printf("list:");
	printintv( p, list);
#endif

	/* get branching info */
	for(i=0; i<p; ++i){
		ublb					=	SCIPround(scip, SCIPcomputeVarUbLocal(scip, var_z[i]) 
								+	SCIPcomputeVarLbLocal(scip, var_z[i]));
		*(Branchz+(ublb*p)+i) 	= 	1;
	}

#if MYPARA_LOG
	for(i=0; i<3; i++){
		for(j=0; j<p; j++){
			printf("%d, ", *(Branchz+(i*p)+j));
		}
		newline();
	}
#endif
	
	RSS = -1.0;
	ind = -1;

	for(i=0; i<p; i++){

		/* copy */
		for(j=0; j<(3*p); j++){
			Branchz_new[j] = Branchz[j];
		}

		/* 
		 * solve 
		 *   Q a = Xy
		 */

		if( list[i] == 1 ){

			Branchz_new[i] = 1;
			Branchz_new[p+i] = 0;
			
			if( ndep ){
				for(t=0; t<ndep; t++){
					memo = -1; 
					for(j=0; j<p; j++){
						if( *(groupX+(t*p)+j)==1 ){
							if( Branchz_new[j]==1 ) break;
							if( Branchz_new[p+j]==1 ) memo=j;
							if( j==Mdep[t] ){
								if( memo==-1 ){
									printf("error in branch_myfullstrong.c\n");
									stop();
								}
								*(Branchz_new+p+memo) = 0;
								*(Branchz_new+memo) = 1;
								break;
							}
						}
					}
				}
			}

			dim = p - sumint( &Branchz_new[0], p);
	
			/* alloc */
			SCIP_CALL( SCIPallocBufferArray( scip, &a, dim));
			SCIP_CALL( SCIPallocBufferArray( scip, &Q_, dim*dim));
			SCIP_CALL( SCIPallocBufferArray( scip, &Xy, dim));

			/* generate Q and Xy */
			/* Q */
			ct = 0;
			for(j=0; j<p; j++){
				if( (Branchz_new[j]==0) && (j != i) ){
					for(t=0; t<p; t++){
						if( (Branchz_new[t]==0) && (t != i ) ){
							Q_[ct++] = mat_( orig_Q_, p, j, t);
						}
					}
				}
			}

			if( ct != (dim*dim) ){
				printf("error in branch_myfullstrong.c\n");
				stop();
			}

			/* Xy */
			ct = 0;
			for(j=0; j<p; j++){
				if( (Branchz_new[j]==0) && (j != i) ){
					Xy[ct++] = orig_q[j];
				}
			}

			if( ct != dim ){
				printf("error in branch_myfullstrong.c\n");
				stop();
			}

			dpv = _dposv_( Q_, Xy, dim, a);

			if( dpv == 0 ){
				/* test */
				RSS_new = RSSvalue( dim, a, Xy, r);
				if( RSS_new > RSS ){
					RSS = RSS_new;
					ind = i;
				}
#if MYPARA_LOG
				printf("%d: RSS = %f\n", i, RSS_new);
#endif
			}

			/* free */
			SCIPfreeBufferArray(scip, &Q_);
			SCIPfreeBufferArray(scip, &Xy);
			SCIPfreeBufferArray(scip, &a);

		 }
	}

#if MYPARA_LOG
	printf("max->%dth var. \n", ind);
#endif

	if( ind == -1 ){
		/* free */
		SCIPfreeBufferArray(scip, &list);
		SCIPfreeBufferArray(scip, &Branchz);
		SCIPfreeBufferArray(scip, &Branchz_new);

   	*result = SCIP_DIDNOTRUN;
		return SCIP_OKAY;
	}

	SCIP_CALL( SCIPbranchVar( scip, var_z[ind], &childnode_0, NULL, &childnode_1));

	/* free */
	SCIPfreeBufferArray(scip, &list);
	SCIPfreeBufferArray(scip, &Branchz);
	SCIPfreeBufferArray(scip, &Branchz_new);

   *result = SCIP_BRANCHED;

   return SCIP_OKAY;
}