Ejemplo n.º 1
0
void
ProblemsReader::readConstraint(Problem& problem, unsigned int /*i*/) {

	LinearConstraint constraint;

	double       value;
	char         rel[2];
	unsigned int numVariables;

	*_stream >> value >> rel >> numVariables;

	Relation relation;
	if (rel[0] == '<')
		relation = GreaterEqual; // switch relation, since in file: value rel term
	else if (rel[0] == '>')
		relation = LessEqual; // switch relation, since in file: value rel term
	else
		relation = Equal;

	constraint.setRelation(relation);
	constraint.setValue(value);

	for (unsigned int j = 0; j < numVariables; j++) {

		int id;
		*_stream >> id;

		if (id >= 0) {

			unsigned int varNum = problem.getConfiguration()->getVariable(id);
			constraint.setCoefficient(varNum, 1.0);

		} else {

			unsigned int varNum = problem.getConfiguration()->getVariable(-id);
			constraint.setCoefficient(varNum, -1.0);
		}
	}

	problem.getLinearConstraints()->add(constraint);

	LOG_ALL(streamproblemreaderlog) << "found constraint " << constraint << std::endl;
}
Ejemplo n.º 2
0
void
BundleCollector::addHyperplane(std::vector<double>& a, double b) {
	/*
	  <w,a> + b ≤  ξ
	        <=>
	  <w,a> - ξ ≤ -b
	*/

	unsigned int dims = a.size();

	LinearConstraint constraint;

	for (unsigned int i = 0; i < dims; i++)
		constraint.setCoefficient(i, a[i]);
	constraint.setCoefficient(dims, -1.0);
	constraint.setRelation(LessEqual);
	constraint.setValue(-b);

	_constraints->add(constraint);

	_constraintAdded();
}
void
MinimalImpactTEDWriter::write(std::string filename) {

	updateInputs();

	// Create the internal pipeline
	createPipeline();

	// Remove the old file
	/*if( std::remove( filename ) != 0 ) {
		LOG_DEBUG(minimalImpactTEDlog) << "Old file to output minimal impact TED approximation sucessfully deleted." << std::endl;
	} 
	else {
		LOG_DEBUG(minimalImpactTEDlog) << "Could not delete old file to output minimal impact TED approximation." << std::endl;
	}*/

	 // Get a vector with all gold standard segments.
        const std::vector<boost::shared_ptr<Segment> > goldStandard = _goldStandard->getSegments();

	int constant = 0;

	LOG_DEBUG(minimalImpactTEDlog) << "in write function." << std::endl;

	// Loop through variables
	std::set<unsigned int> variables = _problemConfiguration->getVariables();
	for ( unsigned int varNum = 0 ; varNum < variables.size() ; varNum++ ) {

		// Introduce constraints that flip the segment corresponding to the ith variable
		// compared to the goldstandard.
	
		// Is the segment that corresponds to the variable part of the gold standard?
                unsigned int segmentId = _problemConfiguration->getSegmentId(varNum);

                bool isContained = false;
                foreach (boost::shared_ptr<Segment> s, goldStandard) {
                        if (s->getId() == segmentId) {
                                isContained = true;
                        }
                }
	
		LinearConstraint constraint;
		constraint.setRelation(Equal);

                if (isContained == true) {
			// Force segment to not be used in reconstruction
			constraint.setCoefficient(varNum,1.0);
			constraint.setValue(0);
                }
                else {
			// Force segment to be used in reconstruction
			constraint.setCoefficient(varNum,1.0);
			constraint.setValue(1);
                }

		_linearConstraints->add(constraint);
		
		_linearSolver->setInput("linear constraints",_linearConstraints);

		pipeline::Value<Errors> errors = _teDistance->getOutput("errors");
		unsigned int sumErrors = errors->getNumSplits() + errors->getNumMerges() + errors->getNumFalsePositives() + errors->getNumFalseNegatives();
		int sumErrorsInt = (int) sumErrors;

		if (isContained == true) {
			// Forced segment to not be part of the reconstruction.
			// This resulted in a number of errors that are going to be stored in the constant.
			// To make net 0 errors when the variable is on, minus the number of errors will be written to the file.

			writeToFile(filename, -sumErrorsInt, varNum, false);

			constant += sumErrorsInt;
		}
		else {
			// Forced segment to be part of the reconstruction.
			// This resulted in a number of errors that are going to be written to the file.

			writeToFile(filename, sumErrorsInt, varNum, false);
		}

		// Remove constraint
		_linearConstraints->removeLastConstraint();
	}

	// Write constant to file
	writeToFile(filename, constant, 0, true);
}
Ejemplo n.º 4
0
double
IlpSolver::min_surface(const Parameters& parameters) {

	std::size_t num_vars = _num_nodes*_num_levels;

	LOG_DEBUG(ilpsolverlog) << "creating objective for " << num_vars << " binary variables" << std::endl;
	LinearObjective objective(num_vars);

	LOG_DEBUG(ilpsolverlog) << "setting objective coefficients" << std::endl;
	for (GraphType::NodeIt n(_graph); n != lemon::INVALID; ++n) {

		double sum = 0;

		for (int l = 0; l < _num_levels; l++) {

			std::size_t var_num = _graph.id(n)*_num_levels + l;
			double costs = _level_costs[n][l];
			double accumulated_costs = costs - sum;
			sum += accumulated_costs;

			objective.setCoefficient(var_num, accumulated_costs);
		}
	}

	LinearConstraints constraints;

	// pick at least lowest level
	LOG_DEBUG(ilpsolverlog) << "adding indicator constraints" << std::endl;
	for (GraphType::NodeIt n(_graph); n != lemon::INVALID; ++n) {

		std::size_t var_num = _graph.id(n)*_num_levels;

		LinearConstraint indicator;
		indicator.setCoefficient(var_num, 1.0);
		indicator.setRelation(Equal);
		indicator.setValue(1.0);
		constraints.add(indicator);
	}

	// column inclusion constraints
	LOG_DEBUG(ilpsolverlog) << "adding column inclusion constraints" << std::endl;
	for (GraphType::NodeIt n(_graph); n != lemon::INVALID; ++n) {

		for (int l = 1; l < _num_levels; l++) {

			std::size_t upper_var_num = _graph.id(n)*_num_levels + l;
			std::size_t lower_var_num = _graph.id(n)*_num_levels + l - 1;

			LinearConstraint inclusion;
			inclusion.setCoefficient(upper_var_num,  1.0);
			inclusion.setCoefficient(lower_var_num, -1.0);
			inclusion.setRelation(LessEqual);
			inclusion.setValue(0.0);

			constraints.add(inclusion);
		}
	}

	// gradient constraints
	LOG_DEBUG(ilpsolverlog) << "adding gradient constraints" << std::endl;
	for (GraphType::EdgeIt e(_graph); e != lemon::INVALID; ++e) {

		GraphType::Node u = _graph.u(e);
		GraphType::Node v = _graph.v(e);

		for (auto& p : { std::make_pair(u, v), std::make_pair(v, u) }) {

			GraphType::Node u_ = p.first;
			GraphType::Node v_ = p.second;

			for (int l = _max_gradients[e]; l < _num_levels; l++) {

				std::size_t upper_var_num = _graph.id(u_)*_num_levels + l;
				std::size_t lower_var_num = _graph.id(v_)*_num_levels + l - _max_gradients[e];

				LinearConstraint inclusion;
				inclusion.setCoefficient(upper_var_num,  1.0);
				inclusion.setCoefficient(lower_var_num, -1.0);
				inclusion.setRelation(LessEqual);
				inclusion.setValue(0.0);

				constraints.add(inclusion);
			}
		}
	}

	if (parameters.enforce_zero_minimum) {

		LOG_USER(ilpsolverlog) << "enforcing minima of zero" << std::endl;

		if (parameters.num_neighbors < 0)
			UTIL_THROW_EXCEPTION(
					UsageError,
					"if 'enforce_zero_minimum' is set, 'num_neighbors' has to be set, too.");

		// for each indicator between 1 and _num_levels - 1: if top indicator t 
		// is 0, one of the neighbors n ∈ N must be zero, too:
		//
		//   t=0 ⇒ Σn<|N|
		//
		//   Σn - t ≤ |N| - 1 if t=0, one of neighbors has to be 0
		//                    if t=1, constraint always true

		for (GraphType::NodeIt n(_graph); n != lemon::INVALID; ++n) {

			for (int l = 1; l < _num_levels - 1; l++) {

				LinearConstraint zero_minimum;

				// Σn
				for (GraphType::IncEdgeIt e(_graph, n); e != lemon::INVALID; ++e) {

					GraphType::Node nb = _graph.oppositeNode(n, e);
					std::size_t nb_var_num = _graph.id(nb)*_num_levels + l;
					zero_minimum.setCoefficient(nb_var_num, 1.0);
				}

				// -t
				std::size_t t_var_num = _graph.id(n)*_num_levels + l + 1;
				zero_minimum.setCoefficient(t_var_num, -1.0);

				// ≤ |N| - 1
				zero_minimum.setRelation(LessEqual);
				zero_minimum.setValue(parameters.num_neighbors - 1);

				constraints.add(zero_minimum);
			}
		}
	}

	SolverFactory factory;
	_solver = std::unique_ptr<LinearSolverBackend>(factory.createLinearSolverBackend());

	LOG_DEBUG(ilpsolverlog) << "initialize solver" << std::endl;
	if (parameters.solve_relaxed_problem) {

		_solver->initialize(num_vars, Continuous);
		// we have to force values to be within 0 and 1
		for (std::size_t i = 0; i < num_vars; i++) {
			LinearConstraint lower_bound, upper_bound;
			lower_bound.setCoefficient(i, 1.0);
			lower_bound.setRelation(GreaterEqual);
			lower_bound.setValue(0.0);
			upper_bound.setCoefficient(i, 1.0);
			upper_bound.setRelation(LessEqual);
			upper_bound.setValue(1.0);
			constraints.add(lower_bound);
			constraints.add(upper_bound);
		}

	} else {

		_solver->initialize(num_vars, Binary);
	}
	LOG_DEBUG(ilpsolverlog) << "setting objective" << std::endl;
	_solver->setObjective(objective);
	LOG_DEBUG(ilpsolverlog) << "setting setting constraints" << std::endl;
	_solver->setConstraints(constraints);

	LOG_ALL(ilpsolverlog) << objective << std::endl;
	for (auto c : constraints)
		LOG_ALL(ilpsolverlog) << c << std::endl;

	LinearSolverBackend::Parameters solverParameters;
	solverParameters.numThreads = parameters.num_threads;
	solverParameters.verbose    = parameters.verbose;

	LOG_DEBUG(ilpsolverlog) << "solving" << std::endl;
	std::string message;
	if (!_solver->solve(_solution, message, solverParameters))
		UTIL_THROW_EXCEPTION(
				LinearSolverBackendException,
				"linear program could not be solved: " << message);

	LOG_ALL(ilpsolverlog) << _solution.getVector() << std::endl;

	return _solution.getValue();
}
void
TolerantEditDistance::findBestCellLabels() {

	pipeline::Value<LinearConstraints>      constraints;
	pipeline::Value<LinearSolverParameters> parameters;

	// the default are binary variables
	parameters->setVariableType(Binary);

	// introduce indicators for each cell and each possible label of that cell
	unsigned int var = 0;
	for (unsigned int cellIndex = 0; cellIndex < _toleranceFunction->getCells()->size(); cellIndex++) {

		cell_t& cell = (*_toleranceFunction->getCells())[cellIndex];

		// first indicator variable for this cell
		unsigned int begin = var;

		// one variable for the default label
		assignIndicatorVariable(var++, cellIndex, cell.getGroundTruthLabel(), cell.getReconstructionLabel());

		// one variable for each alternative
		foreach (float l, cell.getAlternativeLabels()) {

			unsigned int ind = var++;
			_alternativeIndicators.push_back(ind);
			assignIndicatorVariable(ind, cellIndex, cell.getGroundTruthLabel(), l);
		}

		// last +1 indicator variable for this cell
		unsigned int end = var;

		// every cell needs to have a label
		LinearConstraint constraint;
		for (unsigned int i = begin; i < end; i++)
			constraint.setCoefficient(i, 1.0);
		constraint.setRelation(Equal);
		constraint.setValue(1);
		constraints->add(constraint);
	}
	_numIndicatorVars = var;

	// labels can not disappear
	foreach (float recLabel, _toleranceFunction->getReconstructionLabels()) {

		LinearConstraint constraint;
		foreach (unsigned int v, getIndicatorsByRec(recLabel))
			constraint.setCoefficient(v, 1.0);
		constraint.setRelation(GreaterEqual);
		constraint.setValue(1);
		constraints->add(constraint);
	}

	// introduce indicators for each match of ground truth label to 
	// reconstruction label
	foreach (float gtLabel, _toleranceFunction->getGroundTruthLabels())
		foreach (float recLabel, _toleranceFunction->getPossibleMatchesByGt(gtLabel))
			assignMatchVariable(var++, gtLabel, recLabel);

	// cell label selection activates match
	foreach (float gtLabel, _toleranceFunction->getGroundTruthLabels()) {
		foreach (float recLabel, _toleranceFunction->getPossibleMatchesByGt(gtLabel)) {

			unsigned int matchVar = getMatchVariable(gtLabel, recLabel);

			// no assignment of gtLabel to recLabel -> match is zero
			LinearConstraint noMatchConstraint;

			foreach (unsigned int v, getIndicatorsGtToRec(gtLabel, recLabel)) {

				noMatchConstraint.setCoefficient(v, 1);

				// at least one assignment of gtLabel to recLabel -> match is 
				// one
				LinearConstraint matchConstraint;
				matchConstraint.setCoefficient(matchVar, 1);
				matchConstraint.setCoefficient(v, -1);
				matchConstraint.setRelation(GreaterEqual);
				matchConstraint.setValue(0);
				constraints->add(matchConstraint);
			}

			noMatchConstraint.setCoefficient(matchVar, -1);
			noMatchConstraint.setRelation(GreaterEqual);
			noMatchConstraint.setValue(0);
			constraints->add(noMatchConstraint);
		}
	}