Exemplo n.º 1
0
/* save constraints in structmodel for training the full model in the future */
void save_constraints(STRUCT_LEARN_PARM *sparm, STRUCTMODEL *sm)
{
  int i, count;
  CONSTSET c;

  /* read the constraints */
  c = read_constraints(sparm->confile, sm);
  count = 0;
  for(i = 0; i < c.m; i++)
  {
    sparm->cset.lhs = (DOC**)realloc(sparm->cset.lhs, sizeof(DOC*)*(sparm->cset.m+1));
    sparm->cset.lhs[sparm->cset.m] = create_example(sparm->cset.m, 0, 1000000+sparm->cset.m, 1, create_svector(c.lhs[i]->fvec->words, "", 1.0));
    sparm->cset.rhs = (double*)realloc(sparm->cset.rhs, sizeof(double)*(sparm->cset.m+1));
    sparm->cset.rhs[sparm->cset.m] = c.rhs[i];
    sparm->cset.m++;
    count++;
  }

  /* clean up */
  free(c.rhs); 
  for(i = 0; i < c.m; i++) 
    free_example(c.lhs[i], 1);
  free(c.lhs);

  printf("%d constraints added, totally %d constraints\n", count, sparm->cset.m);
}
Exemplo n.º 2
0
BundlerParserOutput read_bundler_out(const std::string &filename)
{
	std::ifstream file;
	file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
	file.open(filename);

	FileFormat format = read_file_format(file);
	ObjectCounts counts = read_object_counts(file);

	auto constraints = read_constraints(file, counts.constraints);
	auto cam_list = std::make_shared<CameraArray>();
	read_cams(file, format, counts.cameras, *cam_list);
	auto point_list = std::make_shared<PointArray>();
	read_points(file, format, counts.points, *point_list);

	file.close();

	World world(cam_list, point_list, constraints);

	return BundlerParserOutput(std::move(world), format);
}
Exemplo n.º 3
0
/* main */
int main(int argc, char *argv[]) {
	std::ios_base::sync_with_stdio(false);

	// arguments: p1 p2 [y|(* \ {y})] [true|false]
	size_t p1 = argc > 2 ? fast_atoi(argv[1]) : 1;		// default = 1
	size_t p2 = argc > 2 ? fast_atoi(argv[2]) : 1;		// default = 1

	bool has_constraints = false;						// if there are constraints
	if (argc > 3) has_constraints = strcmp(argv[3],"1") || strcmp(argv[3], "y");
	/* if default_constraints_mode is true:
	 * 		constraints in input mean ONLY THESE MATCH
	 * otherwise:
	 * 		constraints in input mean ALL EXCEPT THESE MATCH
	 */
	bool default_constraints_mode = false;				// the semantic of the constraints
	//if (has_constraints) default_constraints_mode = strcmp(argv[4], "t");


	/* (1) read strings, extract sigmas and constraints too */
	std::string s1, s2;
	std::string sigma1(""), sigma2("");

	read_stdin(s1, s2);
	extract_sigma(s1, sigma1);
	extract_sigma(s2, sigma2);

	size_t sigma1l = sigma1.size();
	size_t sigma2l = sigma2.size();
	size_t s1l = s1.size();
	size_t s2l = s2.size();

	std::vector<p_constr> constraints;
	if (has_constraints)
		read_constraints(constraints);


	/* define the mapping from char -> int */
	std::map<char, int> map1;
	std::map<char, int> map2;

	define_mapping(sigma1, map1);
	define_mapping(sigma2, map2);

	/* integer representations of strings and sigmas */
	unsigned* s1i = new unsigned[s1l];
	unsigned* s2i = new unsigned[s2l];
	unsigned* sigma1i = new unsigned[sigma1l];
	unsigned* sigma2i = new unsigned[sigma2l];

	for (size_t i = 0; i < sigma1l; ++i) sigma1i[i] = i;		// sigma1
	for (size_t i = 0; i < sigma2l; ++i) sigma2i[i] = i;		// sigma2
	for (size_t i = 0; i < s1l; ++i) s1i[i] = map1[s1[i]];		// s1
	for (size_t i = 0; i < s2l; ++i) s2i[i] = map2[s2[i]];		// s2


	if (_DEBUG) {
		std::cout << "strings: " << s1 << ", " << s2 << endl;
		std::cout << "sigmas: " << sigma1 << ", " << sigma2 << endl;
		std::cout << "int rep (s1): ";
		for (size_t i = 0; i < s1l; ++i) std::cout << s1i[i]; std::cout << endl;
		std::cout << "int rep (s2): ";
		for (size_t i = 0; i < s2l; ++i) std::cout << s2i[i]; std::cout << endl;
		std::cout << "int rep (sigma1): ";
		for (size_t i = 0; i < sigma1l; ++i) std::cout << sigma1i[i]; std::cout << endl;
		std::cout << "int rep (sigma2): ";
		for (size_t i = 0; i < sigma2l; ++i) std::cout << sigma2i[i]; std::cout << endl;
	}


	/* identity (classical) matching schema */
	matching_schema<bool> ms(sigma1l, sigma2l, p1, p2, true, default_constraints_mode);
	ms.set_general(sigma1, sigma2, false);

	if (has_constraints)
		ms.set_constraints(map1, map2, constraints, !default_constraints_mode);


	if (_DEBUG) {
		if (has_constraints) {
			std::cout << "Constraints: ";
			for(std::vector<p_constr>::size_type i = 0; i < constraints.size(); ++i)
				std::cout << constraints[i].first << ", " << constraints[i].second << endl;
		}
		ms.print_matching_schema(sigma1, sigma2);
	}


	//int d = bruteforce(s1i, s2i, s1l, s2l, sigma1i, sigma2i, sigma1l, sigma2l, ms);
	int d = hill_climbing(s1i, s2i, s1l, s2l, sigma1i, sigma2i, sigma1l, sigma2l, p1, ms);
	std::cout << d << endl;

	//Alignment<int> a = compute_alignment(s1i, s2i, s1l, s2l, ms);
	//std::cout << distance_from_alignment(a, sigma1, sigma2, ms, false) << endl;


	return 0;
}