GLPK(GNU线性规划工具包)中建模语言MathProg的使用

发布于:2025-05-18 ⋅ 阅读:(16) ⋅ 点赞:(0)

      GNU MathProg是一种用于描述线性数学规划模型的建模语言用GNU MathProg语言编写的模型描述由一组语句和数据块组成

      在MathProg中,模型以集合、参数、变量、约束和目标(sets, parameters, variables, constraints, objectives称为模型对象)的形式进行描述

      在MathProg中,模型描述(model description)由两部分组成:模型部分和数据部分

      (1).模型部分是模型描述的主要部分,包含模型对象的声明,并且对于基于相应模型的所有问题都是通用的。

      (2).数据部分是模型描述的可选部分,包含特定于特定问题实例的数据。

      模型描述采用ASCII字符集的纯文本格式编码

      这里使用 https://blog.csdn.net/fengbingchun/article/details/147855898  中饮食问题的数据生成diet.mod,内容如下所示:

# sets
set Foods := {'food1', 'food2', 'food3', 'food4', 'food5', 'food6', 'food7', 'food8', 'food9'};
set Nutrients := {'nutrient1', 'nutrient2', 'nutrient3', 'nutrient4'};

# parameters
param unit_prices{Foods};
param nutrient_values{Foods, Nutrients};
param min_bounds{Foods};
param max_bounds{Foods};
param min_nutrient{Nutrients};
param max_untrient{Nutrients};

# variable
var quantity{Foods} >= 0;
var select{Foods} binary;

# objective function
minimize total_cost: sum{f in Foods} unit_prices[f] * quantity[f];

# constraint
s.t. quantity_min{f in Foods}: quantity[f] >= min_bounds[f] * select[f];
s.t. quantity_max{f in Foods}: quantity[f] <= max_bounds[f] * select[f];

s.t. nutrient_min{n in Nutrients}: sum{f in Foods} nutrient_values[f, n] * quantity[f] >= min_nutrient[n];
s.t. nutrient_max{n in Nutrients}: sum{f in Foods} nutrient_values[f, n] * quantity[f] <= max_untrient[n];

s.t. total_quantity: sum{f in Foods} quantity[f] = 99.0;

s.t. mandatory_food: select['food5'] = 1;

s.t. num_select_foods: sum{f in Foods} select[f] = 5;

solve;

printf "total cost: %.4f\n", total_cost;
printf {f in Foods: select[f] > 0.5}: "selected: %s: %.4f, limit: [%.1f, %.1f]\n", f, quantity[f], min_bounds[f], max_bounds[f];
printf "total quantity: %.1f\n", sum{f in Foods} quantity[f];

# data
data;

param unit_prices :=
    'food1' 0.886
    'food2' 0.863
    'food3' 0.855
    'food4' 0.917
    'food5' 0.237
    'food6' 0.856
    'food7' 0.833
    'food8' 0.904
    'food9' 0.876;

param nutrient_values : 'nutrient1' 'nutrient2' 'nutrient3' 'nutrient4' :=
    'food1' 0.21 65.10 0.72 88.5
    'food2' 0.08 64.58 0.63 76.9
    'food3' 0.22 64.81 0.53 86.1
    'food4' 0.58 65.84 1.09 57.8
    'food5' 0.033 46.07 14.15 0
    'food6' 0.059 65.25 0.39 87.2
    'food7' 0.14 64.25 0.57 93.7
    'food8' 0.033 63.06 1.36 79.0
    'food9' 0.076 65.20 0.59 99.0;

param min_bounds :=
    'food1' 1.0
    'food2' 1.0
    'food3' 1.0
    'food4' 1.0
    'food5' 7.0
    'food6' 1.0
    'food7' 1.0
    'food8' 1.0
    'food9' 1.0;

param max_bounds :=
    'food1' 55.0
    'food2' 55.0
    'food3' 55.0
    'food4' 55.0
    'food5' 9.0
    'food6' 55.0
    'food7' 55.0
    'food8' 55.0
    'food9' 55.0;

param min_nutrient :=
    'nutrient1' 0.
    'nutrient2' 6200.
    'nutrient3' 9.9
    'nutrient4' 6500.;

param max_untrient :=
    'nutrient1' 49.
    'nutrient2' 6230.
    'nutrient3' 782.
    'nutrient4' 7000.;

end;

      .mod文件:模型文件(model file),纯文本文件,由GNU MathProg建模语言编写。

      (1).可在.mod中使用printf打印中间结果

      (2).数据既可以放在.mod文件中,也可以存放于单独的.dat文件中

      glpsol.exe调用.mod

      (1).可通过执行"glpsol.exe --help"查看glpsol.exe支持的参数

      (2).执行"glpsol.exe -m diet.mod -o result.txt",执行结果如下图所示:与之前结果相同

      C API调用.mod实现

int test_glpk_mod()
{
	auto tran = glp_mpl_alloc_wksp();

	const char* model_file{ "../../../testdata/diet.mod" };
	if (auto ret = glp_mpl_read_model(tran, model_file, 0); ret != 0) {
		std::cerr << "Error: failed to read model: " << model_file << ", error code: " << ret << std::endl;
		return ret;
	}

	if (auto ret = glp_mpl_generate(tran, nullptr); ret != 0) {
		std::cerr << "Error: failed to generate model:  " << model_file << ", error code: " << ret << std::endl;
		return ret;
	}

	auto lp = glp_create_prob();
	glp_mpl_build_prob(tran, lp);

	glp_iocp parm;
	glp_init_iocp(&parm);
	parm.presolve = GLP_ON;
	parm.msg_lev = GLP_MSG_ERR;
	auto ret = glp_intopt(lp, &parm);
	if (ret != 0) {
		std::cerr << "Error: failed to solve: error code: " << ret << std::endl;
		glp_delete_prob(lp);
		glp_mpl_free_wksp(tran);
		return ret;
	}

	ret = ret = glp_mip_status(lp);
	if (ret != GLP_OPT) {
		std::cerr << "Error: there is no optimal solution, status: " << ret << std::endl;
		glp_delete_prob(lp);
		glp_mpl_free_wksp(tran);
		return ret;
	}

	glp_mpl_postsolve(tran, lp, GLP_MIP); // GLP_SOL: LP, glp_simplex; GLP_MIP: MIP, glp_intopt; execute the printf statement in the mod file

	std::cout << "minimum cost: " << glp_mip_obj_val(lp) << std::endl; // LP: glp_get_obj_val; MIP: glp_mip_obj_val
	for (auto j = 0; j < foods.size(); ++j) {
		if (auto ret = glp_mip_col_val(lp, j+1); ret > 0.5)
			std::cout << foods[j] << ": quantity: " << ret << ", limit: [" << bounds[j][0] << "," << bounds[j][1] << "]" << std::endl;
	}

	glp_delete_prob(lp);
	glp_mpl_free_wksp(tran);
	return 0;
}

      执行结果如下图所示:与之前结果相同

      GitHubhttps://github.com/fengbingchun/Messy_Test


网站公告

今日签到

点亮在社区的每一天
去签到