1、先给出Makefile
# 目标文件
TARGET = my_program# 源文件
SRC = blend.cpp# 头文件目录(如果有)
INCLUDES = -I./# CPLEX 路径设置(根据你的 CPLEX 安装路径)
CPLEX_INCLUDE = -I/mnt/c/users/daniel/Ubuntu/cplex/cplex/include
CONCERT_INCLUDE = -I/mnt/c/users/daniel/Ubuntu/cplex/concert/include
CPLEX_LIB = -L/mnt/c/users/daniel/Ubuntu/cplex/cplex/lib/x86-64_linux/static_pic
CONCERT_LIB = -L/mnt/c/users/daniel/Ubuntu/cplex/concert/lib/x86-64_linux/static_pic
CPLEX_LIBS = -lilocplex -lconcert -lcplex -lm -lpthread -ldl# 编译器
CXX = g++# 编译选项
CXXFLAGS = -fdiagnostics-color=always $(INCLUDES) $(CPLEX_INCLUDE) $(CONCERT_INCLUDE)# 链接选项
LDFLAGS = $(CPLEX_LIB) $(CONCERT_LIB) $(CPLEX_LIBS)# 生成目标文件
$(TARGET): $(SRC)$(CXX) $(CXXFLAGS) -o $(TARGET) $(SRC) $(LDFLAGS)# 清理目标
clean:rm -f $(TARGET)
2、文件解释
\qquad (1) SRC 后面放所有的.cpp文件,所有自己写的头文件放在和.cpp同一个文件目录下,通过INCLUDES 进行调用
\qquad (2) 其中“mnt/c/users/daniel/Ubuntu/cplex/”表示在Linux中安装的Cplex的绝对路径位置,可以根据自己的情况进行替换。
\qquad (3) CPLEX_INCLUDE 和 CONCERT_INCLUDE 表示cplex头文件的路径
\qquad (4) CPLEX_LIBS 用于指定 CPLEX 的库文件,用于链接。这包括 -lilocplex、-lcplex 等
\qquad (5) “blend.cpp”是官方给的测试文件,可以直接拷贝,下面给出源文件以供测试。
// -------------------------------------------------------------- -*- C++ -*-
// File: blend.cpp
// Version 22.1.0
// --------------------------------------------------------------------------
// Licensed Materials - Property of IBM
// 5725-A06 5725-A29 5724-Y48 5724-Y49 5724-Y54 5724-Y55 5655-Y21
// Copyright IBM Corporation 2000, 2022. All Rights Reserved.
//
// US Government Users Restricted Rights - Use, duplication or
// disclosure restricted by GSA ADP Schedule Contract with
// IBM Corp.
// --------------------------------------------------------------------------
//
// blend.cpp -- A blending problem/* ------------------------------------------------------------Problem Description
-------------------Goal is to blend four sources to produce an alloy: pure metal, raw
materials, scrap, and ingots.Each source has a cost.
Each source is made up of elements in different proportions.
Alloy has minimum and maximum proportion of each element.Minimize cost of producing a requested quantity of alloy.------------------------------------------------------------ */#include <ilcplex/ilocplex.h>ILOSTLBEGINIloInt nbElements, nbRaw, nbScrap, nbIngot;
IloNum alloy;
IloNumArray nm, nr, ns, ni, p, P;IloNumArray2 PRaw, PScrap, PIngot;void define_data(IloEnv env) {nbElements = 3;nbRaw = 2;nbScrap = 2;nbIngot = 1;alloy = 71;nm = IloNumArray(env, nbElements, 22.0, 10.0, 13.0);nr = IloNumArray(env, nbRaw, 6.0, 5.0);ns = IloNumArray(env, nbScrap, 7.0, 8.0);ni = IloNumArray(env, nbIngot, 9.0);p = IloNumArray(env, nbElements, 0.05, 0.30, 0.60);P = IloNumArray(env, nbElements, 0.10, 0.40, 0.80);PRaw = IloNumArray2(env, nbElements);PScrap = IloNumArray2(env, nbElements);PIngot = IloNumArray2(env, nbElements);PRaw[0] = IloNumArray(env, nbRaw, 0.20, 0.01);PRaw[1] = IloNumArray(env, nbRaw, 0.05, 0.00);PRaw[2] = IloNumArray(env, nbRaw, 0.05, 0.30);PScrap[0] = IloNumArray(env, nbScrap, 0.00, 0.01);PScrap[1] = IloNumArray(env, nbScrap, 0.60, 0.00);PScrap[2] = IloNumArray(env, nbScrap, 0.40, 0.70);PIngot[0] = IloNumArray(env, nbIngot, 0.10);PIngot[1] = IloNumArray(env, nbIngot, 0.45);PIngot[2] = IloNumArray(env, nbIngot, 0.45);
}int
main(int, char**)
{IloEnv env;try {IloInt j;define_data(env);IloModel model(env);IloNumVarArray m(env, nbElements, 0.0, IloInfinity);IloNumVarArray r(env, nbRaw, 0.0, IloInfinity);IloNumVarArray s(env, nbScrap, 0.0, IloInfinity);IloNumVarArray i(env, nbIngot, 0.0, 100000);IloNumVarArray e(env, nbElements);// Objective Function: Minimize Costmodel.add(IloMinimize(env, IloScalProd(nm, m) + IloScalProd(nr, r) +IloScalProd(ns, s) + IloScalProd(ni, i) ));// Min and max quantity of each element in alloyfor (j = 0; j < nbElements; j++) {e[j] = IloNumVar(env, p[j] * alloy, P[j] * alloy);}// Constraint: produce requested quantity of alloymodel.add(IloSum(e) == alloy);// Constraints: Satisfy element quantity requirements for alloyfor (j = 0; j < nbElements; j++) {model.add(e[j] == m[j] + IloScalProd(PRaw[j], r)+ IloScalProd(PScrap[j], s)+ IloScalProd(PIngot[j], i));}// OptimizeIloCplex cplex(model);cplex.setOut(env.getNullStream());cplex.setWarning(env.getNullStream());cplex.solve();if (cplex.getStatus() == IloAlgorithm::Infeasible)env.out() << "No Solution" << endl;env.out() << "Solution status: " << cplex.getStatus() << endl;// Print resultsenv.out() << "Cost:" << cplex.getObjValue() << endl;env.out() << "Pure metal:" << endl;for(j = 0; j < nbElements; j++)env.out() << j << ") " << cplex.getValue(m[j]) << endl;env.out() << "Raw material:" << endl;for(j = 0; j < nbRaw; j++)env.out() << j << ") " << cplex.getValue(r[j]) << endl;env.out() << "Scrap:" << endl;for(j = 0; j < nbScrap; j++)env.out() << j << ") " << cplex.getValue(s[j]) << endl;env.out() << "Ingots : " << endl;for(j = 0; j < nbIngot; j++)env.out() << j << ") " << cplex.getValue(i[j]) << endl;env.out() << "Elements:" << endl;for(j = 0; j < nbElements; j++)env.out() << j << ") " << cplex.getValue(e[j]) << endl;}catch (IloException& ex) {cerr << "Error: " << ex << endl;}catch (...) {cerr << "Error" << endl;}env.end();return 0;
}
\qquad 最后,给出运行结果以供参考:
2、Windows转Linux
\qquad 本人使用的是Windows最近几年开发出来的WSL工具,可以在不使用虚拟机和双系统的情况下,在windows系统下直接运行Linux环境进行代码测试,对于不熟悉Linux环境或者Linux初学者比较友好。这东西可以直接对接windows的VS code,comfortable [doge].