文章目录提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
- 前言
- 一、基础知识
- 二、问题
- 1.安装与链接问题
- 三、实战经验
- 1.生鲜批发公司选址(师兄提供)
- 总结
前言
此文用来记录从0开始的研究生编程记录,学习c++与CPLEX用来对优化模型进行求解
以下是本篇文章正文内容,下面案例可供参考`
一、基础知识1. 1.1选择结构
1.1.1if语句
1.单行格式if语句
#includeusing namespace std; //选择结构-单行if语句 //格式:if(条件){条件满足执行的语句} //输入一个分数,如果分数大于600分,视为考上一本大学,并在屏幕上打印 int main411() { int score = 0; cout << "请输入一个分数:" << endl; cin >> score; cout << "您输入的分数为:" < = 600) { cout << "我考上了一本大学!!!" << endl; } system("pause"); return 0; }
2.多行格式if语句
#includeusing namespace std; int main412() { int score = 0; cout << "请输入您的分数:" << endl; cin >> score; cout << "您输入的分数为:" << score << endl; if (score >= 600) { cout << "恭喜您考上一本大学!!!" << endl; } else { cout << "遗憾您未考上一本大学!!"< 3.多条件的if语句
#includeusing namespace std; int main413() { int score = 0; cout << "请输入您的分数:" << endl; cin >> score; cout << "您输入的分数为:" < = 600) { cout << "一本的料子" << endl; } else if (score >= 500) { cout << "二本的胚子" << endl; } else if (score >= 400) { cout << "三本的胚子" << endl; } else if (score >= 300) { cout << "上专科吧" << endl; } else { cout << "明年再来!!" << endl; } system("pause"); return 0; } 4.嵌套if语句
#includeusing namespace std; int main414() { int score = 0; cout << "请输入您的分数:" << endl; cin >> score; cout << "您输入的分数为:" << score << endl; if (score >= 600) { cout << "一本大学" << endl; if (score >= 700) { cout << "北京大学" << endl; } else if (score >= 650) { cout << "清华大学" << endl; } else { cout << "人大" << endl; } } else if (score >= 500) { cout << "二本大学" << endl; } else if (score >= 400) { cout << "三本大学" << endl; } else { cout << "未考上本科" << endl; } system("pause"); return 0; } 5.嵌套if练习
#includeusing namespace std; int main415() { int a = 0; int b = 0; int c = 0; cout << "请输入a猪的体重:" << endl; cin >> a; cout << "请输入b猪的体重:" << endl; cin >> b; cout << "请输入c猪的体重:" << endl; cin >> c; if (a >= b) { if (a > c) { cout << "a猪最重" << endl; } else { cout << "c猪最重"< if (b > c) { cout << "b猪最重" << endl; } else { cout << "c猪最重" << endl; }; } system("pause"); return 0; } - List item 6.项目运算符
```cpp #includeusing namespace std; int main421() { int a = 10; int b = 20; int c = 0; c = a > b ? a : b; cout << "c = " << c << endl; //c++从三目运算符返回的是变量,可以继续赋值 (c = a > b ? a : b) = 100; cout << "c = " << c << endl; cout << "b = " << b << endl; cout << "a = " << a << endl; system("pause"); return 0; } 7.switch语句 ```cpp #includeusing namespace std; int main422() { //请给电影评分 //10-9 经典 //8-7 非常好 //6-5 一般 //5分以下 烂片 int score = 0; cout << "请给电影打分:" << endl; cin >> score; switch (score) { case 10: case 9: cout << "经典" << endl; break; case 8: case 7: cout << "非常好" << endl; case 6: case 5: cout << "一般" << endl; default: cout << "烂片" << endl; } system("pause"); return 0; } //注意:switch语句表达式类型中只能是整型或者字符型 //注意:case里如果没有break,那么程序会一直向下执行 //总结:与if语句相比,对于多条件判断时,switch的结构清晰,执行效率高,缺点是switch不可以判断区间 8.while循环语句
#includeusing namespace std; int main501() { int num = 0; while (num < 30) { cout << "num = " << num << endl; num++;//注意:在执行循环语句时候,程序必须提供跳出循环的出口,否则出现死循环 } system("pause"); return 0; } 9.while循环练习案例
#include#include using namespace std; //暂时先这样吧,循环还没搭建起来 int main502() { //添加随机数种子,作用是利用当前系统时间生成随机数,防止每次随机数都一样 srand((unsigned int)time(NULL)); int num = rand()%100 + 1; int answer = 0; cout << "请输入您的答案:" << endl; cin >> answer; while (1)//while(1)其中1代表一个常量表达式,他永远不会等于0。 //所以,循环会一直执行下去。除非你设置break等类似的跳出循环语句循环才会中止 { if (answer == num) { cout << "玩家胜利 游戏结束" << endl; break; } else if (answer > num) { cout << "提示:数字过大,请重新输入" << endl; break; } else { cout << "提示:数字过小,请重新输入" << endl; break; } } system("pause"); return 0; } 10.do…while循环语句
#includeusing namespace std; int main503() { int num = 0; do { cout << "num = " < 11.水仙花数
#includeusing namespace std; int main504(){ int num = 100; do { int a = 0; //个位数字 int b = 0; //十位数字 int c = 0; //百位数字 a = num % 10; //获取各位数字 b = num / 10 % 10; //获取十位数字 c = num / 100; //获取百位数字 if (a*a*a + b*b*b + c*c*c*c == num) { cout << num << endl; } num++; } while (num < 1000); system("pause"); return 0; } 12.for循环语句
#includeusing namespace std; int main505() { for (int i = 0; i < 10; i++) { cout << "i = " << i << endl; } system("pause"); return 0; } //for循环中的表达式,要用分号进行分隔 13.练习案例敲桌子
#includeusing namespace std; int main506() { for (int i = 0;i < 100; i++) { if (i / 7 == 0 || i / 10 == 7 || i % 10 == 7) { cout << "敲桌子" << endl; } else { cout << i << endl; } } system("pause"); return 0; } 14.嵌套循环
#includeusing namespace std; int main507() { //d打印10*10的星号 for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { cout <<" * "<< " "; } cout << "n"< 15.乘法口诀
#includeusing namespace std; int main508() { for (int i = 1; i < 10; i++) { for (int j = 0; j <= i; j++) { cout << i << "*" << j <<"="<< i*j << endl; } cout << endl; } system("pause"); return 0; } 16.跳转break语句
#includeusing namespace std; int main601() { //break在switch中 cout << "请选择游戏难度" << endl; cout << "1.普通" << endl; cout << "2.中等" << endl; cout << "3.困难" << endl; int num = 0; cin >> num; switch (num) { case 1: cout << "您选择的是普通难度!" << endl; break; case 2: cout << "您选择的是中等难度!" << endl; break; case 3: cout << "您选择的是困难难度!" << endl; break; } system("pause"); return 0; } 17.break在循环语句中
#includeusing namespace std; //break语句在循环语句中 int main602() { for (int i = 0; i < 10; i++) { if (i == 5) { break; } cout << i << endl; } system("pause"); return 0; } 18.break在嵌套循环中
#includeusing namespace std; //在嵌套循环语句中使用break语句,推出内层循环 int main603() { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (j == 5) { break; } cout << "*" << " "; } cout << endl; } system("pause"); return 0; } 19.continue语句
#includeusing namespace std; int main611() { for (int i = 0; i < 100; i++) { if (i % 2 == 0) { continue; } cout << i << endl; } system("pause"); return 0; } 20.goto语句
#includeusing namespace std; //在程序中不建议使用goto语句,以免造成程序流程混乱 int main612() { cout << "1" << endl; goto FLAG; cout << "2" << endl; cout << "3" << endl; FLAG: cout << "4" << endl; system("pause"); return 0; } 21.一维数组定义方式
#includeusing namespace std; int main701() { //定义方式一 //数组类型 数组名[数组长度] int score[10]; score[0] = 100; score[1] = 95; score[2] = 90; cout << score[0] << endl; cout << score[1] << endl; cout << score[2] << endl; //第二种定义方式 int score1[10] = { 100,95,90,85,80,80,75,70,60,55 }; cout << "第二种定义输出:" << endl; //利用for循环输出 for (int i = 0; i < 9; i++) { cout << score1[i] << endl; } //第三种定义方式 int score2[] = { 199,55,8,8,3,8,6,4,64,100 }; cout << "第三种定义输出:" << endl; for (int i = 0; i < 9; i++) { cout << score2[i] << endl; } system("pause"); return 0; } //注意:数组名的命名规范与变量名命名规范一致,不要和变量重名 //总结:数组中下标从0开始索引 22.一维数组数组名
#includeusing namespace std; int main702() { int arr[10] = { 1,2,3,4,5,6,7,8,9,10 }; //统计长度 cout << "整个数组所占内存空间为:" << sizeof(arr) << endl; cout << "每个元素所占内存空间为:" << sizeof(arr[5]) << endl; cout << "数组中元素的个数为:" << sizeof(arr) / sizeof(arr[0]) << endl; //获取地址 cout << "数组首地址为:" << (int)arr << endl; cout << "数组中第一个元素地址为:" << (int)&arr[0] << endl; cout << "数组中最后一个元素地址为:" << (int)&arr[9] << endl; //arr = 101 错误,数组名是常量,不可以赋值 system("pause"); return 0; } //总结:直接打印数组名,可以查看数组所占内存的大小 //总结:对数组名进行sizeof,可以获取整个数组所占内存空间大小 23.练习案例5只小猪称体重
#includeusing namespace std; int main703() { int arr[5] = { 300,350,200,250,400 }; int max = 0; for (int i = 0; i < 5;i++) { if (arr[i] > max) { max = arr[i]; cout << arr[i] << endl; } } cout << "最重为:" << max << endl; system("pause"); return 0; } 24练习案例数组元素逆置
#includeusing namespace std; int main704() { int arr[5] = { 1,2,3,4,5 }; int length = sizeof(arr) / sizeof(arr[0]); cout << "length:" < cout << arr[i] << " "; } cout <<"n"<< "数组逆置后:"; int score[5]; for (int i = 0; i < 5; i++) { score[i] = arr[length -1- i]; cout << score[i] << " "; } system("pause"); return 0; } 25.冒泡排序
#includeusing namespace std; int main705() { int arr[9] = { 4,2,8,0,5,7,3,1,9 }; for (int i = 0; i < 9 - 1; i++) { for (int j = 0; j < 9 - 1 - i ; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } for (int i = 0; i < 9; i++) { cout << arr[i] << ","; } system("pause"); return 0; } 26.二维数组定义方式
#includeusing namespace std; int main711() { //第一种定义 int arr[2][3]; arr[0][0] = 1; arr[0][1] = 2; arr[0][2] = 3; arr[1][0] = 4; arr[1][1] = 5; arr[1][2] = 6; cout << "第一种定义:" << endl; for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { cout << arr[i][j] << " "; if (j == 2) { cout << endl; } } } cout << endl; //第二种定义 int arr1[2][3] = { {1,2,3}, {4,5,6} }; cout << "第二种定义:" << endl; for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { cout << arr1[i][j] << " "; if (j == 2) { cout << endl; } } } cout << endl; //第三种定义 int arr2[2][3] = { 1,2,3,4,5,6 }; cout << "第三种定义:" << endl; for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { cout << arr2[i][j] << " "; if (j == 2) { cout << endl; } } } cout << endl; //第四种定义 int arr3[][3] = { 1,2,3,4,5,6 }; cout << "第四种定义:" << endl; for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { cout << arr3[i][j] << " "; if (j == 2) { cout << endl; } } } cout << endl; system("pause"); return 0; } 27.二维数组数组名
#includeusing namespace std; int main712() { int arr[2][3] = { {1,2,3}, {4,5,6} }; cout << "二维数组所占内存大小:" << sizeof(arr) << endl; cout << "二维数组一行所占内存大小:" << sizeof(arr[0]) << endl; //cout << "二维数组一列所占内存大小:" << sizeof[][1] << endl; 疑问,怎么计算一行所占内存大小?? //二维数组应该是按行来存放的 cout << "数组中元素所占内存大小:" < 28.二维数组应用案例
#include#include using namespace std; int main713() { int score[3][3] = { {100,100,100}, {90,50,100}, {60,70,80} }; string name[3] = { "张三","李四","王五" }; for (int i = 0; i < 3; i++) { int sum = 0; for (int j = 0; j < 3; j++) { sum += score[i][j]; } cout << name[i] << ":" ; cout << sum << endl; } system("pause"); return 0; } 29.函数地定义
#includeusing namespace std; //定义两个数的和 int add(int num1, int num2) { int sum = num1 + num2; return sum; } //函数定义里小括号内称为形参,函数调用时传入的参数称为实参 int main801() { int a = 10; int b = 20; int sum = add(a, b); cout << sum << endl; a = 100; b = 200; sum = (a, b); cout << sum << endl; system("pause"); return 0; } 30.值传递
#includeusing namespace std; //如果函数没有返回值,那么应声明为void类型 void swap(int num1, int num2) { cout << "交换前:" << endl; cout << "num1 = " << num1 << endl; cout << "num2 = " << num2 << endl; int temp = num1; num1 = num2; num2 = temp; cout << "交换后:" << endl; cout << "num1 = " << num1 << endl; cout << "num2 =" << num2 << endl; //当函数声明时,不需要返回值,可以不写return } int main802() { int a = 10; int b = 20; swap(a, b); cout << "main中a = " << a << endl; cout << "main中b = " << b << endl; system("pause"); return 0; } 31.函数的常见样式
#includeusing namespace std; //1.无参无返 void test01() { //void a = 0;//无类型不可以创建变量,原因无法分配内存 cout << " this is test01" << endl; //test01()函数调用 } //2.有参无返 void test02(int a) { cout << "this is test02" << endl; cout << a << endl; } //3.有参无返 int test03() { cout << "this is test03" << endl; return 10; } //4.有参有返 int test04(int a, int b) { cout << "this is test04" << endl; int sum = a + b; return sum; } int main803() { system("pause"); return 0; } 32.函数的声明
#includeusing namespace std; int max(int a, int b); int max(int a, int b); int max(int a, int b) { return a > b ? a : b; } int main804() { int a = 10; int b = 20; cout << max(a, b) << endl; system("pause"); return 0; } 33.函数的分文件编写
#include#include"swap.h" using namespace std; void swap1(int a, int b) { int temp = a; a = b; b = temp; cout << "a = " << a << endl; cout << "b = " << b << endl; } int main805() { int a = 10; int b = 20; swap1(a, b); system("pause"); return 0; } 34.指针变量的定义和使用
#includeusing namespace std; int main901() { int a = 10;//整型变量定义 int * p;//指针定义 //指针变量赋值 p = &a;//指针指向变量a的位置 cout << &a << endl;//打印数据a的地址 cout << p << endl;//打印指针变量p //指针的使用 //通过*,操作指针变量指向的内存 cout << "*p = " << *p << endl; system("pause"); return 0; } //指针变量和普通变量的区别 //普通变量存放的是数据,指针变量存放的是地址 //指针变量可以通过“*”操作符,操作指针变量指向的内存空间,这个过程称为解引用 //总结1:我们可以通过 &符号 获取变量的地址 //总结2:利用指针可以记录地址 //总结3:对指针变量解引用,可以操作指针指向的内存 35.指针所占内存空间
#includeusing namespace std; //所有指针在32位操作系统下是4个字节 int main902() { int a = 10; int * p; p = &a; cout << *p << endl; cout << sizeof(p) << endl; cout << sizeof(char *) << endl; cout << sizeof(float *) << endl; cout << sizeof(double *) << endl; system("pause"); return 0; } 36.空指针和野指针
#includeusing namespace std; int main903() { int *p = NULL; //访问空指针报错 //内存编号为0`255为系统占用内存,不允许用户访问 //cout << *p << endl; //指针变量指向内存地址编号为0x1100的空间 int *o = (int *)0x1100; //访问野指针报错 //cout << *o << endl; system("pause"); return 0; } //总结:空指针和野指针都不是我们申请的空间,因此不要访问 37.const修饰指针
#includeusing namespace std; int main904() { int a = 10; int b = 20; //const修饰的是指针,指针指向可以改,指针指向的量不可以更改 const int*p1 = &a; p1 = &b; / int main905() { int arr[] = { 1,2,3,4,5,6,7,8,9,10 }; int *p = arr;//指向数组的指针 cout << "第一个元素;" << arr[0] << endl; cout << "指针访问第一个元素:" << *p << endl; for (int i = 0; i < 10; i++) { //利用指针遍历数组 cout << *p << endl; p++; } system("pause"); return 0; } 39.指针和函数
#include二、问题 1.安装与链接问题using namespace std; //值传递 void swap2(int a, int b) { int temp = a; a = b; b = temp; } //地址传递 void swap1(int *p1, int *p2) { int temp = *p1; *p1 = *p2; *p2 = temp; } int main906() { int a = 10; int b = 20; swap2(a, b); swap1(&a, &b); cout << "a = " << a << endl; cout << "b = " << b << endl; system("pause"); return 0; } //总结:如果不想修改实参,就用值传递,如果想修改实参,就用地址传递
三、实战经验 1.生鲜批发公司选址(师兄提供)1.1问题与模型示例
1.2代码示例
1.2.1数据生成#include总结#include #include #include #include #include #include #include #include #include #include #include #include #include #include #define snprintf _snprintf using namespace std; #define largeNum 1000//定义常数 #define smallNum 0.001 ILOSTLBEGIN//宏命令,可以调用c++的STL typedef IloArray IntMatrix; //IloNumVarArray类,只能定义一维的数组,而这里的模型中需要用到三维和二维的变量。 //Cplex C++ 接口提供了一个lloArray类模版,我们可以利用此类模版定义多维数组。 //比如二维变量数组,在头文件中做如下定义: //typedef IloArray NumVarMatrix; //接下来,便可以用NumVarMatrix定义二维变量。 //三维数组也是同样的方法: //typedef IloArray NumVarMatrix; //typedef IloArray NumVar3Matrix; //接下来,便可以用NumVar3Matrix定义三维变量。 typedef IloArray IntMatrix3d; typedef IloArray IntMatrix4d; typedef IloArray NumMatrix; typedef IloArray NumMatrix3d; typedef IloArray NumMatrix4d; typedef IloArray NumMatrix5d; typedef IloArray IntVarMatrix; typedef IloArray NumVarMatrix; typedef IloArray IntVarMatrix3d; typedef IloArray NumVarMatrix3d; typedef IloArray IntVarMatrix4d; typedef IloArray NumVarMatrix4d; typedef IloArray NumVarMatrix5d; //需要的第一个建模类可能是 IloNumVar。 这个类的对象表示模型中的决策变量。 //它们由变量的下限和上限定义,类型可以是 ILOFLOAT、ILOINT 或 ILOBOOL,分别代表连续变量、整数变量或布尔型变量 IloInt num1, num2, Nbj, Nbl, Nbm, TF, R; IloNum tw; IloInt a, b, i, j, k; int main(int argc, char *argv[]) { srand(time(NULL));//srand函数是随机数发生器的初始化函数。 for (TF = 2; TF <= 2; TF++) for (R = 2; R <= 2; R++) { int instance_num = 0; for (Nbj = 20; Nbj <= 20; Nbj += 5) for (Nbl = 4; Nbl <= 4; Nbl += 2) { Sleep(100); IloEnv env; / for (j = 1; j <= Nbj; j++) for (k = 1; k <= Nbl; k++) { dist[j][k] = floor(sqrt(pow((job_codt[j][1] - loc_codt[k][1]), 2) + pow((job_codt[j][2] - loc_codt[k][2]), 2))); r[j][k] = dist[j][k] / RD; tp[j][k] = TC * dist[j][k]; } IloModel model(env); //Objective function IloExpr temp1(env), temp2(env), temp3(env); for (k = 1; k <= Nbl; k++) temp1 += fcost[k] * w[k]; //开设固定成本 for (j = 1; j <= Nbj; j++) for (k = 1; k <= Nbl; k++) temp2 += 2 * tp[j][k] * v[j][k]; //运输成本 (来回,2倍) for (j = 1; j <= Nbj; j++) temp3 += T[j]; //总延误时间 IloObjective obj(env, temp1 + temp2 + tw * temp3, IloObjective::Minimize); model.add(obj); temp1.end(); temp2.end(); temp3.end(); //Constraints //(2) { IloExpr temp(env); for (k = 1; k <= Nbl; k++) temp += w[k]; model.add(temp <= Nbm); temp.end(); } //(3) for (j = 1; j <= Nbj; j++) { IloExpr temp(env); for (k = 1; k <= Nbl; k++) temp += v[j][k]; model.add(temp == 1); temp.end(); } //(4) for (k = 1; k <= Nbl; k++) for (j = 1; j <= Nbj; j++) model.add(v[j][k] <= w[k]); //(5) for (k = 1; k <= Nbl; k++) { IloExpr temp(env); for (j = 1; j <= Nbj; j++) temp += v[j][k]; model.add(temp <= Nbj * w[k]); temp.end(); } //(6) for (j = 1; j <= Nbj; j++) { IloExpr temp(env); for (k = 1; k <= Nbl; k++) temp += r[j][k] * v[j][k]; model.add(C[j] >= temp + p[j]); temp.end(); } //(7) for (i = 1; i <= Nbj - 1; i++) for (j = i + 1; j <= Nbj; j++) for (k = 1; k <= Nbl; k++) model.add(C[j] >= C[i] + p[j] - largeNum * (3 - x[i][j] - v[j][k] - v[i][k])); //(8) for (i = 1; i <= Nbj - 1; i++) for (j = i + 1; j <= Nbj; j++) for (k = 1; k <= Nbl; k++) model.add(C[i] >= (C[j] + p[i] - largeNum * (2 + x[i][j] - v[j][k] - v[i][k]))); //(9) for (j = 1; j <= Nbj; j++) { IloExpr temp(env); for (k = 1; k <= Nbl; k++) temp += v[j][k] * r[j][k]; model.add(T[j] >= C[j] + temp - d[j]); temp.end(); } IloCplex mycplex(model); mycplex.setParam(IloCplex::Threads, 1); mycplex.setParam(IloCplex::ClockType, 1); mycplex.setParam(IloCplex::EpInt, 0); //mycplex.setParam(IloCplex::EpGap, 0.00); mycplex.setParam(IloCplex::TiLim, timeLim); mycplex.setParam(IloCplex::WorkMem, 10240); mycplex.setParam(IloCplex::NodeFileInd, 3); mycplex.setParam(IloCplex::Param::MIP::Limits::TreeMemory, 5000); //mycplex.setParam(IloCplex::WorkDir, "F:\CplexMemory"); // mycplex.setParam(IloCplex::FPHeur,1); // mycplex.setParam(IloCplex::HeurFreq,20); // mycplex.setParam(IloCplex::LBHeur,1); //mycplex.setParam(IloCplex::MIPEmphasis,1); //mycplex.exportModel("E:\lpModel.lp"); mycplex.solve(); value_LB = mycplex.getBestObjValue(); value_obj = mycplex.getObjValue(); timeCplex = timer.getTime(); double gap = mycplex.getMIPRelativeGap(); Define parameters to store solution values IloNumArray value_w(env, Nbl + 1); IloNumArray value_C(env, Nbj + 1); IloNumArray value_T(env, Nbj + 1); NumMatrix value_v(env, Nbj + 1); for (i = 0; i <= Nbj; i++) value_v[i] = IloNumArray(env, Nbl + 1); //赋值 for (i = 1; i <= Nbl; i++) value_w[i] = mycplex.getValue(w[i]); for (i = 1; i <= Nbj; i++) value_C[i] = mycplex.getValue(C[i]); for (i = 1; i <= Nbj; i++) value_T[i] = mycplex.getValue(T[i]); cout << "w_k: " << endl; for (i = 1; i <= Nbl; i++) cout << value_w[i] << " "; cout << endl; cout << "T_j: " << endl; for (j = 1; j <= Nbj; j++) cout << value_T[j] << " "; cout << endl; for (i = 1; i <= Nbj; i++) for (k = 1; k <= Nbl; k++) { if (mycplex.getValue(v[i][k]) > smallNum) value_v[i][k] = 1; else value_v[i][k] = 0; } IloNum Fixed_cost = 0; IloNum Routing_cost = 0; IloNum Tardiness_cost = 0; for (int k = 1; k <= Nbl; k++) Fixed_cost += value_w[k] * fcost[k]; for (int k = 1; k <= Nbl; k++) for (int j = 1; j <= Nbj; j++) Routing_cost += 2 * tp[j][k] * value_v[j][k]; for (int j = 1; j <= Nbj; j++) Tardiness_cost += value_T[j]; cout << "Fixed cost: " << Fixed_cost << endl; cout << "Routing cost: " << Routing_cost << endl; cout << "Tardiness: " << Tardiness_cost << endl; cout << "Example_" << num1 << "_" << num2 << "_" << num3 << ": " << Nbj << " " << Nbl << " " << Nbm << " " << value_obj << " " << value_LB << " " << 100 * gap << " " << timeCplex << endl; out << "Example_" << num1 << "_" << num2 << "_" << num3 << ": " << Nbj << " " << Nbl << " " << Nbm << " " << value_obj << " " << value_LB << " " << 100 * gap << " " << timeCplex << " " << Fixed_cost << " " << Routing_cost << " " << Tardiness_cost << endl; //Output solution values to file char instance[200]; sprintf(instance, "E:\Projects\Example Data\Data\Example%d_%d_%d.dat", num1, num2, num3); ofstream outfile(instance, ios::app); outfile << "NLocations = " << Nbl << endl; outfile << "NMachines = " << Nbm << endl; outfile << "NJobs= " << Nbj << endl; outfile << "Processing times = "; for (j = 1; j <= Nbj; j++) outfile << p[j] << " "; outfile << endl; outfile << "Release dates r_j_k" << endl; for (j = 1; j <= Nbj; j++) { for (k = 1; k <= Nbl; k++) outfile << r[j][k] << " "; outfile << endl; } outfile << "Fixed cost fcost="; for (k = 1; k <= Nbl; k++) outfile << fcost[k] << " "; outfile << endl; outfile << "Due dates d_j="; for (i = 1; i <= Nbj; i++) outfile << d[i] << " "; outfile << endl; outfile << endl << "Results:" << endl; outfile << "Objective value: " << value_obj << endl << endl; outfile << "C_i: " << endl; for (i = 1; i <= Nbj; i++) { outfile << setprecision(3) << value_C[i] << " "; } outfile << endl << endl; outfile << "w_i: " << endl; for (i = 1; i <= Nbl; i++) outfile << setprecision(3) << value_w[i] << " "; outfile << endl << endl; outfile << "v_jk: " << endl; for (k = 1; k <= Nbl; k++) { outfile << "[" << k << "]: "; for (j = 1; j <= Nbj; j++) { if (value_v[j][k] >= smallNum) outfile << setprecision(3) << value_v[j][k] << " "; } outfile << endl; } outfile << endl; outfile << "T_j: " << endl; for (j = 1; j <= Nbj; j++) outfile << setprecision(3) << value_T[j] << " "; outfile << endl; model.end(); env.end(); } catch (IloException& e) { cerr << "Concert exception caught: " << e << endl; } catch (...) { cerr << "Unknown exception caught" << endl; } } } void readParameter(char *fname, IloInt *Nbj, IloInt *Nbl, IloInt *Nbm, IloNum *tw) { ifstream fin(fname); if (!fin) { cerr << "ERROR: could not open file '" << fname << "' for reading" << endl; throw(-1); } fin.ignore(256, '='); fin >> *Nbj; fin.ignore(256, '='); fin >> *Nbl; fin.ignore(256, '='); fin >> *Nbm; fin.ignore(256, '='); fin >> *tw; fin.ignore(256, '['); } void readData(char *fname, IloIntArray p, NumMatrix job_codt, NumMatrix loc_codt, IloIntArray fcost, IloIntArray d) { ifstream fin(fname); if (!fin) { cerr << "ERROR: could not open file '" << fname << "' for reading" << endl; throw(-1); } for (i = 0; i<4; i++) fin.ignore(256, 'n'); //忽略一行 fin.ignore(256, '='); //忽略到等于号 char temp; fin >> temp; for (j = 1; j <= Nbj; j++) fin >> p[j] >> temp; fin.ignore(256, '='); fin >> temp; for (j = 1; j <= Nbj; j++) { fin >> temp; for (k = 1; k < 2; k++) fin >> job_codt[j][k] >> temp; fin >> job_codt[j][k]; fin >> temp >> temp; } fin.ignore(256, '='); fin >> temp; for (j = 1; j <= Nbl; j++) { fin >> temp; for (k = 1; k < 2; k++) fin >> loc_codt[j][k] >> temp; fin >> loc_codt[j][k]; fin >> temp >> temp; } fin.ignore(256, '='); fin >> temp; for (k = 1; k <= Nbl; k++) fin >> fcost[k] >> temp; fin.ignore(256, '='); fin >> temp; for (j = 1; j <= Nbj; j++) fin >> d[j] >> temp; } 提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方



