【VS/GUROBI】解LP示例;column generation

【模型】

\ LP format - for model browsing. Use MPS format to capture full model detail.

Maximize
  x_0 + x_1 + 2 x_2
Subject To
 cons_1: x_0 + 2 x_1 + x_2 <= 4
 cons_2: x_0 + x_1 >= 1
Bounds
Binaries
 x_0 x_1 x_2
End

【代码】

#pragma once
#include"Util.h"
#include
class Model_gurobi
{
private:
	GRBEnv * _env;
	GRBModel * _model;
	GRBVar* _vars;
	GRBConstr* _cons;

	
public:
	Model_gurobi();
	~Model_gurobi();

	void test();//官网示例
	void test_2();//修改示例,可批量添加变量,可用于column generation
};

#include "Model_gurobi.h"



Model_gurobi::Model_gurobi()
{
	_env = new GRBEnv();
	_model = new GRBModel(*_env);
}


Model_gurobi::~Model_gurobi()
{
}

void Model_gurobi::test()
{
	try {

		// Create variables

		GRBVar x = _model->addVar(0.0, 1.0, 0.0, GRB_BINARY, "x");
		GRBVar y = _model->addVar(0.0, 1.0, 0.0, GRB_BINARY, "y");
		GRBVar z = _model->addVar(0.0, 1.0, 0.0, GRB_BINARY, "z");

		// Set objective: maximize x + y + 2 z

		_model->setObjective(x + y + 2 * z, GRB_MAXIMIZE);

		// Add constraint: x + 2 y + 3 z <= 4

		_model->addConstr(x + 2 * y + 3 * z <= 4, "c0");

		// Add constraint: x + y >= 1

		_model->addConstr(x + y >= 1, "c1");

		// Optimize model

		_model->optimize();

		cout << x.get(GRB_StringAttr_VarName) << " "
			<< x.get(GRB_DoubleAttr_X) << endl;
		cout << y.get(GRB_StringAttr_VarName) << " "
			<< y.get(GRB_DoubleAttr_X) << endl;
		cout << z.get(GRB_StringAttr_VarName) << " "
			<< z.get(GRB_DoubleAttr_X) << endl;

		cout << "Obj: " << _model->get(GRB_DoubleAttr_ObjVal) << endl;

		_model->write("model.lp");

	}
	catch (GRBException e) {
		cout << "Error code = " << e.getErrorCode() << endl;
		cout << e.getMessage() << endl;
	}
	catch (...) {
		cout << "Exception during optimization" << endl;
	}
}

void Model_gurobi::test_2()
{
	try {

		// Create variables
		int count = 2;
		double* lb = new double[count];
		double* ub = new double[count];
		double* obj = new double[count];
		char* type = new char[count];
		string* names = new string[count];
		for (int i = 0; i < count; ++i)
		{
			lb[i] = 0.0;
			ub[i] = 1.0;
			obj[i] = 0.0;
			type[i] = GRB_BINARY;
			names[i] = string("x_") + to_string(i);
		}
		_vars = _model->addVars(lb, ub, obj, type, names, count);

		// Create constraints
		GRBLinExpr* exps = new GRBLinExpr[2];
		double* para_1 = new double[count];
		para_1[0] = 1;
		para_1[1] = 2;
		exps[0].addTerms(para_1, _vars, count);

		double* para_2 = new double[count];
		para_2[0] = 1;
		para_2[1] = 1;
		exps[1].addTerms(para_2, _vars, count);

		char* senses = new char[2];
		senses[0] = GRB_LESS_EQUAL;
		senses[1] = GRB_GREATER_EQUAL;
		double* rhsVars = new double[2];
		rhsVars[0] = 4.0;
		rhsVars[1] = 1.0;
		string* names_c = new string[2];
		names_c[0] = string("cons_1");
		names_c[1] = string("cons_2");

		_cons = _model->addConstrs(exps, senses, rhsVars, names_c, 2);

		// Set objective
		GRBLinExpr obj_exp;
		double* para = new double[count];
		para[0] = 1.0;
		para[1] = 1.0;
		obj_exp.addTerms(para, _vars, count);

		_model->setObjective(obj_exp, GRB_MAXIMIZE);

		// Output
		_model->update();
		_model->write("model_2_0.lp");

		// Add column
		GRBColumn* col = new GRBColumn();
		col->addTerm(1.0, _cons[0]);
		_model->addVar(0.0, 1.0, 2.0, GRB_BINARY, *col, "x_2");


		// Optimize model
		_model->optimize();

		// Output 
		_model->write("model_2.lp");


		for (int i = 0; i < count; ++i)
		{
			cout << _vars[i].get(GRB_StringAttr_VarName) << " "
				<< _vars[i].get(GRB_DoubleAttr_X) << endl;
		}


		cout << "Obj: " << _model->get(GRB_DoubleAttr_ObjVal) << endl;

	}
	catch (GRBException e) {
		cout << "Error code = " << e.getErrorCode() << endl;
		cout << e.getMessage() << endl;
	}
	catch (...) {
		cout << "Exception during optimization" << endl;
	}
}



你可能感兴趣的:(visual,studio,Gurobi)