Cplex的数据类型结构及基本语法功能

        本序列将会重开一门新的序列----数学求解器cplex,文章不做简单介绍,不灌水,直接给大家进行讲述如何上手实操,并有针对性的给出相应案例分析。

OPL编程

        OPL是ILOG团队为运筹学专家量身定制的一种优化建模语言,语法相对简单,约束定义接近运筹学专家习惯的属性模型表达。表达更为简洁,描述同样约束,OPL更为简单明了,易于检查排错。有助于开发者专注于模型的开发,而不必花很大精力将数学模型转换为复杂的程序语言

        在学习任何一门新的语言也好,工具也好,我们都需要先熟悉其基本的结构与功能,本文将讲述cplex的基本数据结构与基本的语法功能。

1、基础数据类型

cplex的数据结构有点类似C,其常用的数据结构类型包含有:  

1. 整数型
2. 浮点数型
3. 字符串型
4. 布尔型

 1.  整数型: 范围 −2^31+1, 2^31−1;关键词 int
     int truckQty = 10;
     int n = 20;
     int size = n * n;

2. 浮点型: 双精度;  关键词 float
      float unitCost = 2.0;
      float totalCost = n * unitCost;

execute {
   writeln("truckQty = ", truckQty);
   writeln("size = ", size);
   writeln(" unitCost = ", unitCost);
   writeln("totalCost = ", totalCost);
 }

 3.  字符串型:关键词 string
     1) string company = “大华技术”;
     2)string retailerName = “沃尔玛”;
     3) 字符串中的特殊字符: \t  tab
      \n  newline   \”  双引号   \\   反斜杠
     4)  OPL中字符串换行
       string address = “上海市  \             临港新城”;

 4. 布尔型: 关键词 boolean
     dvar boolean x;
     dvar boolean isUsed;

2、基础的数据结构

cplex中包含的数据结构包含有

1. 范围:range
2. 数组: 给出元素的数据类型
3. 元组:tuple
4. 集合:给出元素的数据类型

1. Range:给定最小值和最大值
   1) range  rows = 1..10;
   2) int n = 8;  
      range cols = n+1..2*n+1;
   3)用于数组定义
      int A[rows] = …;
   4) 用于变量取值范围定义
      dvar int x in rows;
   5) 用于循环
       forall( r in rows){  语句1}

 2. 数组 :
  一维数组
  int a[1..4] =[10,20,30,40];
  float  f[1..4] = [2.0,3.0,4.0,5.0];
  string days[1..2] = [“Mon”,”Tue”]
 
execute{
   writeln("a",a);
   writeln("f",f);
   writeln("days",days);
}

 2. 数组 :
  二维数组
  int b[1..2][1..4] =[[10,20,30,40],[2,3,4,5]];
  float  tranCost[1..2][1..4] = …;
execute{
   writeln("b", b);
}

  3. tuple (元组 ):
    //卡车元组
   tuple TTruck {
      key int id;
      float fixedCost;
      float capacity;}
 //客户元组
 tuple TCustomer {
 key int id;
 float demand;
 float serviceTime; }

 4. 集合:
    可以用{T} 或 setof{T}的方式来定义集合
     {string}  days = {“Mon”, “Tue”, “Wen”};
      setof(int)  s1={1,2,3};
     //配送中心所有的卡车集合
    {TTruck}  trucks = ...;
     //配送中心服务的客户的集合
    {TCustomer} customers =...;

3、基本语法功能

addadd constraints to the problem
baroptsolve using barrier algorithm
displaydisplay problem, solution, or parameter settings
enterenter a new problem
mipoptsolve a mixed integer program
netoptsolve the problem using network method
optimizesolve the problem
populateget additional solutions for a mixed integer program
primoptsolve using the primal method
readread problem or advanced start information from a file
setset parameters
toolstools for analvsis and debugging of models
writewrite problem or solution information to a file

4、项目文件

在一个典型的OPL项目中,需要包含的文件如下:
主要包含模型文件、数据文件、设置文件、运行文件,具体关系如下

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

且行且安~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值