Exemplo n.º 1
0
void slack_matrix::init_geqz_constraints(Matrix& m)
{
	geqz_constraints = new bool[m.num_vars()];
	memset(geqz_constraints, 0, m.num_vars()*sizeof(bool));
	Matrix::iterator it = m.begin();
	for(; it!= m.end(); it++)
	{
		bignum constant = it->second;
		if(constant > 0) continue;

		int index = -1;
		Equation* eq = it->first;
		for(int c=0; c<eq->num_entries(); c++)
		{
			bignum e = eq->get(c);
			if(e > 0){
				index = -1;
				break;
			}
			else if(e<0 && index != -1)
			{
				index = -1;
				break;
			}
			else if(e<0)
				index = c;
		}
		// Mark entry as 1 if var has non-negativity constraint
		if(index != -1)
			geqz_constraints[index] = true;

	}

}
Exemplo n.º 2
0
// -----------Private methods ------------------//
void slack_matrix::populate_slack_matrix(Matrix& m)
{
	/*
	 * If the initial system is Ax <= b,
	 * the slack matrix will contain it in the form
	 * -x0 + y +Ax -b
	 * and the last row is the objective function, initially -x0.
	 */
	Matrix::iterator it = m.begin();
	int last = m.size()-1;

	/*
	vector<int> rows;
	std::set<int> used;
	for(int i=0; i <= last; i++)
	{
		int cur = rand()%(last+1);
		while(used.count(cur) > 0){
			cur++;
			cur = cur%(last+1);
		}
		used.insert(cur);
		rows.push_back(cur);

	}
	*/

	int r = 0;
	int i = 0;
	for(; it!=m.end(); it++, r++, i++)
	{
		r = i;//rows[i];
		// s0 entry is initially -1 for all rows
		sm->set(r, 0, -1);

		// In row r, set the corresponding slack variable to 1.
		sm->set(r, i+1, 1);

		Equation *eq = it->first;
		for(int c=0; c < eq->num_entries(); c++) {
			int start_index = index_mapping[c];
			bignum coef = eq->get(c);
			sm->set(r, start_index, coef);


			/* Figure out whether we needed to split
			 * x as x1-x2 for missing non-negative constraints
			 */
			if(geqz_constraints[c]) continue;
			sm->set(r, start_index+1, -coef);
		}
		//Deal with original constant
		sm->set_constant(r, -it->second);

		//Fill last slot with index of pivot variable for this row
		sm->set_pivot(r, i+1);

	}
	/*
	 *  Populate objective function row: Initially we want
	 *  to maximize -x0
	 */
	sm->set(last+1, 0, -1);
}