CPLEX-分支定界算法调用cplex求解VRPTW

前面讲了Cplex直接求解VRPTW的模型,下面我们在分支定界算法中调用Cplex来求解VRPTW
1、分支定界算法
(1)定义:
(2)求解过程:
1)确定一个下界(初始解LB),上界定为无穷大UB
2)把初始问题构建一个节点加入优先队列
3) 判断队列是否为空,如果为空跳转至7,否则取出并弹出队首元素,计算该节点的目标值P
4) 如果P > UB,返回3。否则判断当前节点是否是合法解(对于任意i,j,k,x_ijk均为整数),如果是,跳转5否则跳转6
5) 如果P < UB, 记录UB = P,当前节点为当前最优解BS。返回3
6) 设置两个子节点L, R。L,R的建立方式如上,如果L的目标值L.P <= UB,把L加入队列,如果R的目标值R.P <= UB,把R加入队列。返回3
7) 结束,返回记录的最优节点BS。如果BS为空则无解

2、分支定界求解VRPTW
代码结构如下
在这里插入图片描述
Data类的作用是定义变量、变量初始化、读取数据

package com.chb;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Scanner;

//定义参数
class Data{
	public static final double gap= 1e-6;
	public static final double big_num = 100000;
	public static int pointnum=102;			//所有点集合n(包括配送中心和客户点,首尾(0和n)为配送中心)
	public static double E;	      		//配送中心时间窗开始时间
	public static double L;	     		//配送中心时间窗结束时间
	public static int carnum;    		//车辆数
	public static double cap;     		//车辆载荷
	public static int[][] point=new int[pointnum][2];;		//所有点的坐标x,y
	public static int[] demand=new int[pointnum];			//需求量
	public static int[] car=new int[carnum];			//车辆编号
	public static double[] a=new double[pointnum];				//时间窗开始时间【a[i],b[i]】
	public static double[] b=new double[pointnum];				//时间窗结束时间【a[i],b[i]】
	public static double[] s=new double[pointnum];				//客户点的服务时间
	public static int[][] arcs=new int[pointnum][pointnum];			//arcs[i][j]表示i到j点的弧
	public static double[][] dist=new double[pointnum][pointnum];		//距离矩阵,满足三角关系,暂用距离表示花费 C[i][j]=dist[i][j]
	
	//截断小数3.26434-->3.2
	public double double_truncate(double v){
		int iv = (int) v;
		if(iv+1 - v <= gap)
			return iv+1;
		double dv = (v - iv) * 10;
		int idv = (int) dv;
		double rv = iv + idv / 10.0;
		return rv;
	}	
	public Data() {
		super();
	}
	//函数功能:从txt文件中读取数据并初始化参数
	public void read_data(String path,Data data) throws Exception{
		String line = null;
		String[] substr = null;
		Scanner cin = new Scanner(new BufferedReader(new FileReader(path)));  //读取文件
		//读取1-4行
		for(int i =0; i < 4;i++){
			line = cin.nextLine();  
		}
		//读取5行
		line = cin.nextLine();
		line.trim(); 
		substr = line.split(("\\s+")); 
		carnum = Integer.parseInt(substr[1]); 
		cap = Integer.parseInt(substr[2]);
		//读取6-9行
		for(int i =0; i < 4;i++){
			line = cin.nextLine();
		}
		//读取pointnum-1行数据
		for (int i = 0; i < pointnum - 1; i++) {
			line = cin.nextLine();
			line.trim();
			substr = line.split("\\s+");
			point[i][0] = Integer.parseInt(substr[2]);
			point[i][1] = Integer.parseInt(substr[3]);
			demand[i] = Integer.parseInt(substr[4]);
			a[i] = Integer.parseInt(substr[5]);
			b[i] = Integer.parseInt(substr[6]);
			s[i] = Integer.parseInt(substr[7]);
		}
		cin.close();//关闭流
		//初始化终点参数
		point[pointnum-1] = point[0];
		demand[pointnum-1] = 0;
		a[pointnum-1] = a[0];
		b[pointnum-1] = b[0];
		E = a[0];
		L = b[0];
		s[pointnum-1] = 0;		
		double min1 = 1e15;
		double min2 = 1e15;
		//距离矩阵初始化
		for (int i = 0; i < pointnum; i++) {
			for (int j = 0; j < pointnum; j++) {
				if (i == j) {
					dist[i][j] = 0;
					continue;
				}
				dist[i][j] = 
					Math.sqrt(Math.pow(point[i][0]-point[j][0], 2)+Math.pow(point[i][1]-point[j][1], 2));
				dist[i][j]=double_truncate(dist[i][j]);
			}
		}
		dist[0][pointnum-1] = 0;
		dist[pointnum-1][0] = 0;
		//距离矩阵满足三角关系
		for (int  k = 0; k < pointnum; k++) {
			for (int i = 0; i < pointnum; i++) {
				for (int j = 0; j < pointnum; j++) {
					if (dist[i][j] > dist[i][k] + dist[k][j]) {
						dist[i][j] = dist[i][k] + dist[k][j];
					}
				}
			}
		}
		//初始化为完全图
		for (int i = 0; i < pointnum; i++) {
			for (int j = 0; j < pointnum; j++) {
				if (i != j) {
					arcs[i][j] = 1;
				}
				else {
					arcs[i][j] = 0;
				}
			}
		}
		//除去不符合时间窗和容量约束的边
		for (int i = 0; i < pointnum; i++) {
			for (int j = 0; j < pointnum; j++) {
				if (i == j) {
					continue;
				}
				if (a[i]+s[i]+dist[i][j]>b[j] ||
						demand[i]+demand[j]>cap) {
					arcs[i][j] = 0;
				}
				if (a[0]+s[i]+dist[0][i]+dist[i][pointnum-1]>
				b[pointnum-1]) {
					System.out.println("the calculating example is false");
					
				}
			}
		}
		for (int i = 1; i < pointnum-1; i++) {
			if (b[i] - dist[0][i] < min1) {
				min1 = b[i] - dist[0][i];
			}
			if (a[i] + s[i] + dist[i][pointnum-1] < min2) {
				min2 = a[i] + s[i] + dist[i][pointnum-1];
			}
		}
		if (E > min1 || L < min2) {
			System.out.println("Duration false!");
			System.exit(0);//终止程序
		}
		//初始化配送中心0,n+1两点的参数
		arcs[pointnum-1][0] = 0;
		arcs[0][pointnum-1] = 1;
		for (int i = 1; i < pointnum-1; i++) {
			arcs[pointnum-1][i] = 0;
		}
		for (int i = 1; i < pointnum-1; i++) {
			arcs[i][0] = 0;
		}
	}
}

Node类的主要作用是记录分支节点

package com.chb;

import java.util.ArrayList;

public class Node implements Comparable{
	Data data;
	int d;
	double node_cost;				//目标值object
	double[][][]lp_x;//记录lp解
	int[][][] node_x_map;//node_xij=1时,node_x_mapijk=1表示必须访问,node_x_mapijk=0表示不能访问
	int[][] node_x;//0表示弧可以访问,1表示必须访问,-1表示不能访问
	ArrayList<ArrayList<Integer>> node_routes;		//定义车辆路径链表
	ArrayList<Array
  • 3
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值