? ? ? 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 filestd::cout << "minimum cost: " << glp_mip_obj_val(lp) << std::endl; // LP: glp_get_obj_val; MIP: glp_mip_obj_valfor (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;
}
? ? ? 執行結果如下圖所示:與之前結果相同
? ? ? GitHub:https://github.com/fengbingchun/Messy_Test