Пример #1
0
sigma2::sigma2(vec rs)
{
	receive_symbols = rs;
	ivec generators;
	int constraint_length;	
	generators.set_size(2, false);
	generators(0) = 0133;
	generators(1) = 0145;
	constraint_length = 7;
	conv_code.set_generator_polynomials(generators, constraint_length);
}
Пример #2
0
	int SRGAntColony(SRGGraph& g, std::vector<bool>& relays, std::size_t genSize, std::size_t iterations)
	{
		PheromoneMatrix p(relays.size());
		p.setEvaporationRate(0.3);
		p.setQ(relays.size()*0.3);
		std::vector<bool> best(relays.size(),true), oldBest(relays.size(),true);
		int opt = relays.size();
		//put a proper hating criterion in
		bool halt = false;

        std::vector<RanGen*> generators(NUM_THREADS);
        for(int i=0; i<NUM_THREADS; ++i)
            generators[i] = new RanGen(rand());
		for(int i=0; !halt || i<iterations; ++i)
		{
		    std:copy(best.begin(), best.end(), oldBest.begin());
		    std::vector<std::vector<std::size_t> > tours(genSize, std::vector<std::size_t>(0));
		    std::vector<size_t> result(genSize);
		    for(int j=0; j<genSize/NUM_THREADS; ++j)
		    {
		        //some parallel code?
		        pthread_t threads[NUM_THREADS];
		        ACOInfo * infos[NUM_THREADS];
		        std::vector<std::vector<bool> > relayArray(NUM_THREADS, std::vector<bool>(relays.size(),true));
		        for(int k=0; k<NUM_THREADS; ++k)
                {
                    tours[j*NUM_THREADS+k].reserve(relays.size());
                    infos[k] = new ACOInfo(g,&relayArray[k],&tours[j*NUM_THREADS+k],&p, generators[k], &result[j*NUM_THREADS+k]);
                    int rc = pthread_create(&threads[k], NULL, DropThread, (void *) infos[k]);
                }
                for(int k=0; k<NUM_THREADS; ++k)
                    pthread_join(threads[k],NULL);
                for(int k=0; k<NUM_THREADS; ++k)
                {
                    if(result[j*NUM_THREADS+k] < opt)
                    {
                        std::copy(relayArray[k].begin(), relayArray[k].end(), best.begin());
                        opt = result[j*NUM_THREADS+k];
                    }
                    delete infos[k];
                }
		    }
		    for(int j=0; j<genSize; ++j)
            {
		        p.update(tours[j],(float) result[j]);
            }
            halt = true;
            for(int j=0; j<best.size(); ++j)
                if(best[j]!=oldBest[j])
                    halt = false;
		}
		std::copy(best.begin(), best.end(), relays.begin());
		return opt;
	}
Пример #3
0
// returns a generator using the parameters provided in the ptree. The remaining
// parameters use their default values. Note that params must at least contain
// the "name" property to identify the generator.
// params is non-const as the generator adds its additional default-values
// to this ptree
shared_ptr<Generator> Generator::from_parameters(const ptree& params)
{

    // Required parameter: name of the generator
    // if not available, we cannot lookup the generator instance and thus
    // cannot provide a Generator instance.
    string generator_name = "unitialized_name";

    try {
        generator_name = params.get<string>("generator.name");
    } catch (boost::property_tree::ptree_bad_path& e) {
        std::cerr << "Exception when trying to read the 'generator.name' field from the generator property tree: " << e.what() << std::endl;
        // in case this exception is caught, the next few lines will fail and raise a new exception
    }

    generators_t::const_iterator cit = generators().find(generator_name);
    if (cit == generators().end())
    {
        throw std::runtime_error("generator " + generator_name + " not registered -- probably need to include the corresponding cpp file.");
    }
    return generators().at(generator_name)(params);
}
Пример #4
0
std::vector<std::vector<long> >
random_generators (int d, int n,
                   const long& lo, const long& hi,
                   const long& seed) {
  std::vector<std::vector<long> > generators (n, std::vector<long> (d));
  std::uniform_int_distribution<long> dis (lo, hi);
  
  std::mt19937 gen(seed);
  
  for ( int k = 0; k < n; ++k ) {
    for ( int i = 0; i < d; ++i ) {
      generators[k][i] = dis(gen);
    }
  }

  return generators;
}
Пример #5
0
	real PseudoBoolean<real>::minimize_generators(vector<label>& x, int& labeled, bool heuristic)
	{
		bool should_continue;
		real bound;
		labeled = 0;
		int n = int( x.size() );

		ASSERT(!heuristic);

		// TODO: should be provided by user
		Generators<real> generators("generators/generators.txt");

		do {
			// Create symmetric relaxation
			GeneratorPseudoBoolean<real> spb(generators);
			spb.create_lp(*this);

			// Minimize relaxation
			int new_labeled = 0;
			bound = spb.minimize(x, new_labeled);

			// If we have more persistencies, continue
			should_continue = new_labeled > labeled;
			labeled = new_labeled;
			if (labeled == n) {
				//Nothing more to do
				should_continue = false;
			}

			// Reduce this function
			reduce(x);

		} while (should_continue);

		return bound;
	}
    double ParallelOptimize::optimize(stereo_base::Depth &result, const int max_iter) const {
        typedef CompactLabelSpace Space;
        typedef ParallelFusionPipeline<Space> Pipeline;

        bool victorMethod = true;
        result.initialize(width, height, -1);
        const int kFusionSize = 4;

        ParallelFusionOption pipelineOption;
        pipelineOption.num_threads = num_threads + 1;
        pipelineOption.max_iteration = model->nLabel / num_threads / kFusionSize * max_iter;
        const int kLabelPerThread = model->nLabel / pipelineOption.num_threads;

        Pipeline::GeneratorSet generators((size_t)pipelineOption.num_threads);
        Pipeline::SolverSet solvers((size_t)pipelineOption.num_threads);
        vector<Space> initials((size_t)pipelineOption.num_threads);
        vector<ThreadOption> threadOptions((size_t)pipelineOption.num_threads);

        vector<vector<int> > labelSubSpace;
        splitLabel(labelSubSpace);

        const int kPix = model->width * model->height;

        //slave threads
        const int kOtherThread = std::min(num_threads-1, 1);
        for(auto i=0; i<pipelineOption.num_threads - 1; ++i){
            const int startid = labelSubSpace[i].front();
            initials[i].init(kPix, vector<int>(1, startid));
            threadOptions[i].kTotal = kFusionSize + kOtherThread;
            threadOptions[i].kOtherThread = kOtherThread;
            threadOptions[i].solution_exchange_interval = 1;
            if(multiway) {
                generators[i] = shared_ptr<ProposalGenerator<Space> >(new MultiwayStereoGenerator(kPix, labelSubSpace[i]));
                solvers[i] = shared_ptr<FusionSolver<Space> >(new MultiwayStereoSolver(model));
            }else{
                generators[i] = shared_ptr<ProposalGenerator<Space> >(new SimpleStereoGenerator(kPix, labelSubSpace[i]));
                solvers[i] = shared_ptr<FusionSolver<Space> >(new SimpleStereoSolver(model));
            }

            printf("Initial energy on thread %d: %.5f\n", i, solvers[i]->evaluateEnergy(initials[i]));
        }

        //monitor thread
        threadOptions.back().is_monitor = true;
        solvers.back() = shared_ptr<FusionSolver<Space> >(new SimpleStereoMonitor(model));
        generators.back() = shared_ptr<ProposalGenerator<Space> >(new DummyGenerator());

        StereoPipeline parallelFusionPipeline(pipelineOption);
        float t = (float)getTickCount();
        printf("Start runing parallel optimization\n");
        parallelFusionPipeline.runParallelFusion(initials, generators, solvers, threadOptions);
        t = ((float)getTickCount() - t) / (float)getTickFrequency();

        SolutionType<Space > solution;
        parallelFusionPipeline.getBestLabeling(solution);

        printf("Done! Final energy: %.5f, running time: %.3fs\n", solution.first, t);

        std::dynamic_pointer_cast<SimpleStereoMonitor>(solvers.back())->dumpData(file_io.getDirectory() + "/temp");

        dumpOutData(parallelFusionPipeline, file_io.getDirectory()+"/temp/plot_"+method);

        for(auto i=0; i<model->width * model->height; ++i){
            result.setDepthAtInd(i, solution.second(i,0));
        }
        return solution.first;
    }
bool CExporter::buildMeshLod(std::vector<ExportVertex>& exportVertices, std::vector< std::vector<grp::LodIndices> >& buffers)
{
	std::vector<LodGenerator*> generators(buffers.size());
	for (size_t i = 0; i < buffers.size(); ++i)
	{
		generators[i] = new LodGenerator(exportVertices, buffers[i]);
		generators[i]->calculate(m_options.lodMaxError, m_options.lodLevelScale);
	}

	//所有顶点按塌陷顺序重新排序
	std::map<int, int> vertexMap;
	int newIndex = 0;
	//以误差从大到小的顺序填充所有mesh buffer的所有lod级别用到的顶点
	std::vector<int> lastLevel(buffers.size());
	for (size_t i = 0; i < buffers.size(); ++i)
	{
		assert(buffers[i].size() > 0);
		lastLevel[i] = buffers[i].size() - 1;
	}
	while (true)
	{
		int maxErrorBuffer = -1;
		float maxError = -1.0f;
		for (size_t i = 0; i < lastLevel.size(); ++i)
		{
			if (lastLevel[i] < 0)
			{
				continue;
			}
			//这里有点费解,但又懒得解释
			if (lastLevel[i] == buffers[i].size() - 1)
			{
				maxErrorBuffer = i;
				maxError = FLT_MAX;
			}
			else if (buffers[i][lastLevel[i] + 1].maxError > maxError)
			{
				maxErrorBuffer = i;
				maxError = buffers[i][lastLevel[i] + 1].maxError;
			}
		}
		if (maxErrorBuffer < 0)
		{
			break;
		}
		//填充顶点
		grp::LodIndices& lodIndices = buffers[maxErrorBuffer][lastLevel[maxErrorBuffer]];
		for (size_t i = 0; i < lodIndices.indices.size(); ++i)
		{
			int index = lodIndices.indices[i];
			if (vertexMap.find(index) == vertexMap.end())
			{
				vertexMap.insert(std::make_pair(index, newIndex));
				++newIndex;
			}
		}
		--lastLevel[maxErrorBuffer];
	}
	//顶点重新排序
	std::vector<ExportVertex> sorted(vertexMap.size());
	for (std::map<int, int>::iterator iterMap = vertexMap.begin();
		iterMap != vertexMap.end();
		++iterMap)
	{
		assert(iterMap->second < sorted.size());
		sorted[iterMap->second] = exportVertices[iterMap->first];
	}
	//整理index buffer
	for (size_t bufferIndex = 0; bufferIndex < buffers.size(); ++bufferIndex)
	{
		std::vector<grp::LodIndices>& buffer = buffers[bufferIndex];
		for (size_t levelIndex = 0; levelIndex < buffer.size(); ++levelIndex)
		{
			grp::LodIndices& lodIndice = buffer[levelIndex];
			lodIndice.maxIndex = 0;
			for (size_t indexIndex = 0; indexIndex < lodIndice.indices.size(); ++indexIndex)
			{
				grp::Index32& index = lodIndice.indices[indexIndex];
				assert(vertexMap.find(index) != vertexMap.end());
				index = vertexMap[index];
				if (index > lodIndice.maxIndex)
				{
					lodIndice.maxIndex = index;
				}
			}

		}
	}
	//整理冗余关系
	for (size_t vertexIndex = 0; vertexIndex < sorted.size(); ++vertexIndex)
	{
		ExportVertex& vertex = sorted[vertexIndex];
		std::map<int, int>::iterator found = vertexMap.find(vertex.copyPos);
		if (found != vertexMap.end())
		{
			vertex.copyPos = found->second;
		}
		found = vertexMap.find(vertex.copyNormal);
		if (found != vertexMap.end())
		{
			vertex.copyNormal = found->second;
		}
	}
	//纠正冗余关系,保证copyPos和copyNormal总是小于顶点下标
	for (size_t vertexIndex = 0; vertexIndex < sorted.size(); ++vertexIndex)
	{
		ExportVertex& vertex = sorted[vertexIndex];
		if (vertex.copyPos >= 0)
		{
			changeCopyPos(sorted, vertexIndex, vertex.copyPos);
		}
		if (vertex.copyNormal >= 0)
		{
			changeCopyNormal(sorted, vertexIndex, vertex.copyNormal);
		}
	}
	exportVertices.swap(sorted);

	return true;
}