有能力约束的车辆路径调度(Capacitated Vehicle Routing Problem,CVRP)

有能力约束的车辆路径调度(Capacitated Vehicle Routing Problem,CVRP)

车辆路径问题是指为服务于需求已知的一组客户的车队,设计从中心仓库出发并返回的最小费用路径,约束条件包括客户只能被服务一次,且车辆的载重能力有限,或具有时间窗限制等,并随约束条件的拓展可以衍生出许多新的问题,VRP自从1959年由DANTZIG和 RAMSER提出以来,迅速成为运筹学\物流管理\交通规划管理\地理区位等领域的研究热点VRP的研究属于组合优化中的NP难问题,即随着问题的规模增加,计算机求解所耗费的时间迅速增加。针对 VRP的特点,国内外学者引入各种优化算法对其进行求解,其中应用较多的是各种智能算法,如遗传算法、进化计算等。
遗传算法(Genetic Algorithm,GA)是一种以自然选择和遗传理论为基础,将生物进化过程中适者生存规则与染色体的随机信息变换机制相结合的全局搜索算法,它对优化对象既不要求连续,也不要求可微,并具有极强的鲁棒性。但遗传算法本身存在一些问题,如容易出现“早熟”、收敛速度慢等,因此,在解决车辆路径问题时需要加以改进。
本文用遗传算法求解有能力约束的VRP,遗传算法的交叉算子采用顺序交叉算子,变异算子采用随机变异算子。

参考文献:
[1] 梁勤.欧基于改进免疫算法的有能力约束车辆路径问题[J].武汉理工大学学报
[2] 梁勤欧,周晓艳.基于免疫遗传算法的设备布局问题研究[J]. 武汉理工大学学报

部分代码:
% Author: 怡宝2号
% Use: 基于遗传算法求解CVRP问题,交叉采用的是顺序交叉方法;变异采用交换变异:随机选择串中的两点,交换其基因值。
% 输入变量(可修改量): demand.mat:每个客户的需求量
% distance.mat:距离矩阵
% const:客户的个数
% MAXGEN:遗传算法的遗传代数,视具体情况改变
%
%
% 输出: trace:每一代的最优个体
% Remark: 代写程序qq:778961303,如有疑问请咨询
clc
clear all
close all

NIND=20; %种群大小
const=29; %客户个数
carload=25; %车的载重限制
MAXGEN=100; %遗传代数
GGAP=0.9; %代沟
PC=0.8; %交叉概率
PM=0.1; %变异概率
trace=[]; %结果存储数组

% demand=ceil(rand(29,1)*9);
% save demand
load demand %每个客户点的需求
% distance=xlsread(‘distance.xlsx’);
% save distance
load distance %距离矩阵

%—————–初始化种群——————-%
for i=1:NIND
chrom(i,:)=randperm(const);
end

%—————–计算目标函数——————-%
visited=[];
route=ones(NIND,64)*Inf;
% route=[];
for i=1:NIND
temp=0;
j=1;
while j<=const

    temp=temp+demand(chrom(i,j));
    visited=[visited chrom(i,j)];               %没有超重,则按种群运货
    if temp>carload
        temp=0;
        visited(1,size(visited,2))=0;               %如果超重则回到原点
        j=j-1;
    end   
    j=j+1;

    if j==const+1
        visited(1,size(visited,2)+1)=0;             %使最后一点回到原点
    end
end

route(i,2:size(visited,2)+1)=visited;               %每个种群的行驶路线
visited=[];

end
route(:,1)=0; %使所有路线都出原点出发

%—————-计算目标函数的值———————%
length=zeros(NIND,1); %保存路线的长度变量
for i=1:NIND
temp=0;
updimension=find(route(i,:)

  • 2
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Here is a basic implementation of CVRP in Python using the Google OR-Tools library: ```python from ortools.constraint_solver import routing_enums_pb2 from ortools.constraint_solver import pywrapcp def create_data_model(): """Stores the data for the problem.""" data = {} data['distance_matrix'] = [ [0, 548, 776, 696, 582, 274, 502, 194, 308, 194, 536, 502], [548, 0, 684, 308, 194, 502, 730, 354, 696, 742, 1084, 594], [776, 684, 0, 992, 878, 502, 274, 810, 468, 742, 400, 1278], [696, 308, 992, 0, 114, 650, 878, 502, 844, 890, 1232, 514], [582, 194, 878, 114, 0, 536, 764, 388, 730, 776, 1118, 400], [274, 502, 502, 650, 536, 0, 228, 308, 194, 240, 582, 810], [502, 730, 274, 878, 764, 228, 0, 536, 194, 468, 354, 1016], [194, 354, 810, 502, 388, 308, 536, 0, 342, 388, 730, 468], [308, 696, 468, 844, 730, 194, 194, 342, 0, 274, 388, 810], [194, 742, 742, 890, 776, 240, 468, 388, 274, 0, 342, 650], [536, 1084, 400, 1232, 1118, 582, 354, 730, 388, 342, 0, 878], [502, 594, 1278, 514, 400, 810, 1016, 468, 810, 650, 878, 0] ] data['num_vehicles'] = 3 data['vehicle_capacities'] = [100, 100, 100] data['depot'] = 0 return data def print_solution(data, manager, routing, solution): """Prints solution on console.""" total_distance = 0 total_load = 0 for vehicle_id in range(data['num_vehicles']): index = routing.Start(vehicle_id) plan_output = 'Route for vehicle {}:\n'.format(vehicle_id) route_distance = 0 route_load = 0 while not routing.IsEnd(index): node_index = manager.IndexToNode(index) route_load += data['demands'][node_index] plan_output += ' {} Load({}) -> '.format(node_index, route_load) previous_index = index index = solution.Value(routing.NextVar(index)) route_distance += routing.GetArcCostForVehicle( previous_index, index, vehicle_id) plan_output += ' {} Load({})\n'.format(manager.IndexToNode(index), route_load) plan_output += 'Distance of the route: {}m\n'.format(route_distance) plan_output += 'Load of the route: {}\n'.format(route_load) print(plan_output) total_distance += route_distance total_load += route_load print('Total distance of all routes: {}m'.format(total_distance)) print('Total load of all routes: {}'.format(total_load)) def main(): """Entry point of the program.""" data = create_data_model() manager = pywrapcp.RoutingIndexManager(len(data['distance_matrix']), data['num_vehicles'], data['depot']) routing = pywrapcp.RoutingModel(manager) def distance_callback(from_index, to_index): """Returns the distance between the two nodes.""" from_node = manager.IndexToNode(from_index) to_node = manager.IndexToNode(to_index) return data['distance_matrix'][from_node][to_node] transit_callback_index = routing.RegisterTransitCallback(distance_callback) routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index) dimension_name = 'Capacity' routing.AddDimension( transit_callback_index, 0, # no slack 100, # vehicle maximum capacities True, # start cumul to zero dimension_name) capacity_dimension = routing.GetDimensionOrDie(dimension_name) for i, demand in enumerate(data['demands']): index = manager.NodeToIndex(i) capacity_dimension.SetDemand(index, demand) for vehicle_id in range(data['num_vehicles']): index = routing.Start(vehicle_id) capacity_dimension.CumulVar(index).SetRange(data['vehicle_capacities'][vehicle_id], data['vehicle_capacities'][vehicle_id]) search_parameters = pywrapcp.DefaultRoutingSearchParameters() search_parameters.first_solution_strategy = ( routing_enums_pb2.FirstSolutionStrategy.PARALLEL_CHEAPEST_INSERTION) solution = routing.SolveWithParameters(search_parameters) if solution: print_solution(data, manager, routing, solution) if __name__ == '__main__': main() ``` Note that this is just a basic implementation and can be modified to suit specific requirements and constraints of individual problem instances.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值