Example #1
0
bool ParallelMcmcmc::restore(void){
	if(stream.is_open())
		stream.close();

	stream.open((filename+".stream").c_str(), std::fstream::in);

	size_t pos = stream.tellg();
	size_t lastsample = pos;

	while(!stream.eof()){
		fromStream(stream,false);

		lastsample = pos;
		pos = stream.tellg();
	}

	if(numSteppingStones > 0)
	{
	    int index = 0;
	    for ( size_t i = 0; i < numSteppingStones; i++)
	    {
	        stream.clear();
	        stream.seekg(lastsample);

	        stream >> currentGeneration;

	        if(numChains > 1){
	            stream >> index;
	        }

	        chains[i]->fromStream(stream);
	        chains[i]->setChainActive(i == 0);
	        chains[i]->setChainHeat(computeBeta(delta,sigma,i));
	        chainIdxByHeat[i] = i;
	    }
Example #2
0
bool Optimize::eval_f(Ipopt::Index n, const Ipopt::Number *x, bool new_x, Ipopt::Number &obj_value)
{
    UNUSED(n);
    UNUSED(new_x);
    if (updateCross(x) == false) return false;
    obj_value = 0;
    PointArray px = PointArray(x);
    Ipopt::Number t[3];
    Ipopt::Number r = 0;
    double tmp;
    int i = 0, size = EnergyVertices.size();
    for (; i < size; i++)
    {
        obj_value += computeBeta(i, x) / VerticesWeight[i];
    }
    obj_value *= 2 * pOp->BendingEnergyCoef;
    i = 0;
    size = PositionConstraints.size();
    for (; i < size; i++)
    {
        sub(px(PositionConstraints[i]), positions[i].data(), t);
        r += sqrnorm(t);
    }
    r *= pOp->PositionConstraintsWeight;
    obj_value += r;
    r = 0;
    i = 0;
    size = TangentConstraints.size();
    for (; i < size; ++i)
    {
        computeTangentEdge(i, x, t);
        r += 1 - dot(t, tangents[i].data());
    }
    obj_value += r * pOp->TangentConstraintsCoef;

    r = 0;
    i = 0;
    size = PlaneConstraints.size();
    for (; i < size; ++i)
    {
        tmp = (getPoint(PlaneConstraints[i], x) | PlaneConstraintsInfo[i].first) + PlaneConstraintsInfo[i].second;
        r += tmp * tmp;
    }
    obj_value += r * pOp->PlaneConstraintsCoef;
    return true;
}
Example #3
0
GrabCut::GrabCut( Image<Color>* image )
{
	m_image = image;

	m_w = m_image->width();
	m_h = m_image->height();

	m_trimap = new Image<TrimapValue>( m_w, m_h );
	m_trimap->fill(TrimapUnknown);

	m_GMMcomponent = new Image<unsigned int>( m_w, m_h );

	m_hardSegmentation = new Image<SegmentationValue>( m_w, m_h );
	m_hardSegmentation->fill(SegmentationBackground);

	m_softSegmentation = 0;		// Not yet implemented

	m_TLinksImage = new Image<Color>(m_w, m_h);
	m_TLinksImage->fill(Color(0,0,0));
	m_NLinksImage = new Image<Real>(m_w, m_h);
	m_NLinksImage->fill(0);
	m_GMMImage = new Image<Color>(m_w, m_h);
	m_GMMImage->fill(Color(0,0,0));
	m_AlphaImage = new Image<Real>(m_w, m_h);
	m_AlphaImage->fill(0);

	m_foregroundGMM = new GMM(5);
	m_backgroundGMM = new GMM(5);

	//set some constants
	m_lambda = 50;
	computeL();
	computeBeta();
	
	m_NLinks = new Image<NLinks>( m_w, m_h );
	computeNLinks();

	m_graph = 0;
	m_nodes = new Image<Graph::node_id>( m_w, m_h );
}
Vertices<CoordType> SpatialModelMaximalRepulsion3D<CoordType>::drawSample(const int numPoints)
{
  ENTER("Vertices<CoordType> SpatialModelMaximalRepulsion3D<CoordType>::drawSample(const int)");

  const int outerSteps = getNumMonteCarloCycles();
  const int innerSteps = numPoints;
  const CoordType beta = computeBeta();
  const CoordType maxRadius = this->getTriMesh().equivalentRadius() / 50.0;
  Vertices<CoordType> vertices = SpatialModelHardcoreDistance3D<CoordType>::drawSample( numPoints );
  RandomGenerator& randomGenerator = this->getRandomGenerator();
  CoordType currentEnergy, newEnergy, deltaEnergy;
  Vector<CoordType> vertex;
  bool acceptTransition;
  int i, j, v;

  ConvergenceTest<CoordType> convergenceTest;  
  CoordType sumEnergy;
  bool converged = false;
  _energyProfile.setZeros( outerSteps );
  convergenceTest.setData( _energyProfile );
  convergenceTest.setRange( 100 );

  for (i = 0; !converged && i < outerSteps; ++i)
  {
#if 0
    if ( (i%20)==0 )
    {
      string filename = "max-repulsion-vertices-" + StringTools::toString(i,4,'0') + ".vx";
      vertices.save( filename, true );
    }
#endif
    for (j = 0, sumEnergy = 0.0; j < innerSteps; ++j)
    {
      v = randomGenerator.uniformL( numPoints );
      vertex = vertices[v];
      if ( j == 0 ) currentEnergy = energy( vertices );
      moveVertex( vertices, v, maxRadius );
      newEnergy = energy( vertices );
      deltaEnergy = newEnergy - currentEnergy;
      acceptTransition = deltaEnergy <= .0 || randomGenerator.uniformLF() < exp( -beta*deltaEnergy );

      if ( acceptTransition )
        currentEnergy = newEnergy;
      else vertices[v] = vertex;
      sumEnergy += currentEnergy;
    }

    _energyProfile[i] = sumEnergy / innerSteps;
    converged = convergenceTest.isPositive( i );
  }

  if ( !converged )
  {
    Exception exception;
    exception.setWhere( "Vertices<CoordType> SpatialModelMaximalRepulsion3D<CoordType>::drawSample(const int)" );
    exception.setWhat( "Convergence not reached after " + StringTools::toString(outerSteps) + " iterations" );
    throw exception;
  }

  LEAVE();

  return vertices;
}
Example #5
0
ParallelMcmcmc::ParallelMcmcmc(Model* m, const std::vector<Move*> &moves, const std::vector<Monitor*> &mons, std::string fn, std::string sT, int ev, int nc, int np, int si, double dt, double st, double sh, bool saveall, size_t ss) : Cloneable( ),
			filename(fn),
			numChains(nc),
			numProcesses(np),
			scheduleType(sT),
			every(ev),
			currentGeneration(0),
			swapInterval(si),
			delta(dt),
			sigma(st),
			startingHeat(sh),
			saveall(saveall),
			numSteppingStones(ss)
{
    activeIndex = 0;
    std::vector<Monitor*> mons2 = mons;

    if(numSteppingStones > 0)
        mons2.clear();
    
    size_t numMcmc = std::max(numSteppingStones,numChains);

    for (size_t i = 0; i < numMcmc; i++)
    {
        // get chain heat
        double b = computeBeta(delta,sigma,i) * startingHeat;
        
        // create chains
        bool a = (i == 0 ? true : false);
        Mcmc* oneChain = new Mcmc(m, moves, mons2, scheduleType, a, b, i);
    	oneChain->setChainIndex(i);
        oneChain->startMonitors();
        
        // add chain to team
        chains.push_back(oneChain);
        chainIdxByHeat.push_back(i);
    }

    numNodes = chains[0]->getNumNodes();
    //std::cout << "\n";
    
    steppingStones = std::vector<std::vector<double> >(numSteppingStones, std::vector<double>());
    
    numMcmc = numSteppingStones > 0 ? numSteppingStones : numChains;

    if (numMcmc < numProcesses)
        numProcesses = numMcmc;

    // assign chains to processors
#if defined (USE_LIB_OPENMP)
    omp_set_num_threads((unsigned)numProcesses);
#endif
    
    chainsPerProcess.resize(numProcesses);
    for (size_t i = 0, j = 0; i < numMcmc; i++, j++)
    {
        if (j >= numProcesses)
            j = 0;
        chainsPerProcess[j].push_back(i);
    }
}