示例#1
0
void System::exportLP(io::Output& out) {
	static CString texts[] = { "<", "<=", "=", ">=", ">" };
	out << "/* IPET system */\n"; 
	
	// Output the objective function
	if(ofun->comparator() >= 0)
		out << "max: ";
	else
		out << "min: ";
	ofun->dump(out);
	out << ";\n";
	
	// Output the constraints
	for(Constraint *cons = conss; cons; cons = cons->next()) {
		cons->dump(out);
		out << " " << texts[cons->comparator() + 2]
			<< " " << (int)cons->constant() << ";";
		const string& label = cons->label();
		if(label)
			out << "\t/* " << label << "*/";
		out << io::endl; 
	}
	
	// Output int constraints
	for(genstruct::HashTable<ilp::Var *, Var *>::Iterator var(vars);
	var; var++)
		out << "int " << var->makeVarName() << ";\n";
}
示例#2
0
// Overload
bool System::solve(WorkSpace *ws) {
	static short comps[] = { LE, LE, EQ, GE, GE };
	static double corr[] = { -1, 0, 0, 0, +1 };
	
	// allocate and initialize the lp_solve data structure
	lprec *lp = make_lp(0, cols);
	set_verbose(lp, IMPORTANT);
	REAL row[cols + 1];
	for(int i = 1; i <= cols; i++)
		row[i] = 0;
		//set_int(lp, i, TRUE);

	// set the type of variable
	for(var_map_t::Iterator var(vars); var; var++) {
		switch(var->variable()->type()) {
		case ilp::Var::INT:		set_int(lp, var->column(), TRUE); break;
		case ilp::Var::BIN:		set_binary(lp, var->column(), TRUE); break;
		case ilp::Var::FLOAT:	break;
		default:				ASSERT(false); break;
		}
		set_col_name(lp, var->column(), (char*)var->variable()->name().chars());
	}
	
	// Build the object function
	ofun->fillRow(row);
	row[0] = 0;
	set_obj_fn(lp, row);
	ofun->resetRow(row);
	if(ofun->comparator() >= 0)
		set_maxim(lp);
	else
		set_minim(lp);
	
	// Build the matrix
	for(Constraint *cons = conss; cons; cons = cons->next()) {
		cons->fillRow(row);
		Constraint::comparator_t comp = cons->comparator();
		double cst = cons->constant();
		add_constraint(lp, row,
			comps[comp - Constraint::LT],
			cst + corr[comp - Constraint::LT]);
		/*cout << "=> ";
		for(int i = 0; i <= cols; i++)
			cout << row[i] << '\t';
		cout << io::endl;*/
		cons->resetRow(row);
	}
	
	// if required, record the cancellation test
	if(ws)
		put_abortfunc(lp, test_cancellation, ws);

	// Launch the resolution
	if (!ILPNAME(ws).get().isEmpty()) {
		Path p(ILPNAME(ws));
		write_lp(lp, (char*)p.toString().chars());
	}
	int fail = ::solve(lp);
	
	// Record the result
	int result = false;
	if(!ws || !ws->isCancelled()) {
		if(fail == OPTIMAL) {
			result = true;

			// Record variables values
			for(elm::genstruct::HashTable<ilp::Var *, Var *>::Iterator var(vars);
			var; var++)
				var->setValue((double)lp->best_solution[lp->rows + var->column()]);


			// Get optimization result
			//cout << "=> " << get_objective(lp) << " <=> " << int(get_objective(lp)) << "<=\n";
			val = rint(get_objective(lp));
		}
		
		// !!DEBUG!!
		else {

			// messages
#			define ERRORM(x)	{ x, #x }
			static struct {
				int code;
				cstring msg;
			} msgs[] = {
				ERRORM(UNKNOWNERROR),
				ERRORM(DATAIGNORED),
				ERRORM(NOBFP),
				ERRORM(NOMEMORY),
				ERRORM(NOTRUN),
				ERRORM(OPTIMAL),
				ERRORM(SUBOPTIMAL),
				ERRORM(INFEASIBLE),
				ERRORM(UNBOUNDED),
				ERRORM(DEGENERATE),
				ERRORM(NUMFAILURE),
				ERRORM(USERABORT),
				ERRORM(TIMEOUT),
				ERRORM(RUNNING),
				ERRORM(PRESOLVED),
				{ 0, "" }
			};

			// display message
			int i;
			for(i = 0; msgs[i].msg; i++)
				if(msgs[i].code == fail)
					break;
			cerr << "ERROR: failed due to " << fail << " (" << msgs[i].msg << ")\n";
		}
	}

	// Clean up
	delete_lp(lp);
	return result;
}