Requirements for (self-made, non-actual project):
A distribution center for the canvassing, the target number of customers is 50 customers, distribution Center's current capacity resources are as follows:
- Existing vehicles 5 units
- Maximum travel distance of single capacity 200-kilometer
- Maximum capacity kg 1 ton per load
Q: How can capacity be carried out in order to complete the canvassing behavior for these 50 customers at the lowest cost
is an optimization problem (Operations Research), we only consider the simplified model, regardless of road traffic, time window of these complex calculations, using ant colony optimization algorithm to achieve near optimal solution calculation.
The theory of ant colony optimization algorithm See this article: https://www.cnblogs.com/asxinyu/p/Path_Optimization_Tsp_Problem_Ant_System_CSharp.html
Inside the basic algorithm has been written, there are demo, this article is for how to adapt to the specific business introduction (the ant core code used in this article is also modified above)
The main steps of the ant colony are:
- Initialize (such as pheromone)
- Start Iteration
- Constructs each ant, and the path that the ant walks (core is select for subsequent nodes)
- Calculation of Fitness
- Join the excellent ant to the tracking list
- Update pheromone (depending on fitness)
- End Iteration
- Give a report
The original article uses the TSP to do the demo, the more ugly clear how to apply to the actual business logic
Similarly, the core of the most confused core, similar to genetic algorithm, is also the calculation of fitness value, some places are step by step to increase the vlaue, such as the increase of the simple distance, but the complexity of the point can not do so, but to see the overall path indicators (including penalties, etc.)
Since the Ant colony optimization algorithm and the code can be downloaded, only the calculation of fitness value is introduced.
Download
classFitnessvaluecalculator {Private Static intNumber of vehicles with capacity =5; Private Static intMaximum travel distance of single capacity = $; Private Static intMaximum capacity kg for single units +; Private Static DoublePenalty weight = -; Public Static DoubleCalculator (shortestdeliverant ant) {varpaths =Newlist<string>(); vardistances =Newlist<Double>(); varweights =Newlist<Double>(); DoubleCurrent Travel distance =0; DoubleCurrent Capacity load =0; stringCurrent Drive Path =""; intCurrent number of capacity required =1; //calculate the hub to the first customer delivery distanceCurrent Travel Path + ="hub-->"+Ant. Pathnodes.first (); Current travel distance+=Ant. DistanceHelper.hub.DistanceTo (Ant. Distancehelper.customers[ant. Pathnodes.first ()]); Current capacity load+=Ant. Distancehelper.customers[ant. Pathnodes.first ()]. Demand _ kg; foreach(varPathinchAnt. Edges) {varFromnodeid =path. Key; varTonodeid =path. Value; varFromnode =Ant. Distancehelper.customers[fromnodeid]; varTonode =Ant. Distancehelper.customers[tonodeid]; DoubleNewaddeddistance2customer =0; DoubleNewaddeddistance2hub =0; DoubleNewaddedweight =0; Newaddeddistance2customer=Fromnode.distanceto (Tonode); Newaddeddistance2hub=Tonode.distanceto (Ant. Distancehelper.hub); Newaddedweight=Tonode. Demand _ kg; if(Current travel distance + Newaddeddistance2customer + Newaddeddistance2hub <=maximum travel distance for single-station capacity&&Current capacity load<=maximum capacity kg for single units) {Current travel distance+=Newaddeddistance2customer; Current capacity load+=Newaddedweight; Current Travel Path+=" -"+Tonodeid; } Else { //Add current customer distance, and distance to hubCurrent Travel distance + =Fromnode.distanceto (Ant. Distancehelper.hub); Distances. ADD (current travel distance); Weights. ADD (current capacity load); Current Travel Path+="-->hub"; Paths. ADD (current drive path); //RESETCurrent Travel distance =0; Current travel distance+=Ant. DistanceHelper.hub.DistanceTo (Tonode); Current capacity load=0; Current capacity load+=Tonode. Demand _ kg; Current Travel Path=""; Current Travel Path+="hub-->"+Tonodeid; Current number of capacity required++; } } //back to the hubCurrent Travel distance + =Ant. Distancehelper.customers[ant. Pathnodes.last ()]. Distanceto (Ant. Distancehelper.hub); Distances. ADD (current travel distance); Current Travel Path+="-->hub"; Paths. ADD (current drive path); int penalty factor = 0; if (current required capacity > number of vehicles with capacity) penalty factor = Current required number of capacity- number of vehicles with capacity; Ant. Transport distance Sequence=distances; Ant. Transport path=paths; Ant. Total Travel distance=distances. Sum (); Ant. Total Capacity=the current number of capacity required; returnAnt. Total travel distance+ penalty factor * penalty weights ; } }
Ant. Distancehelper.hub: is the distribution center info, has address information
Ant. Distancehelper.customers: is 50 customer info, also has address information
At present, in order to simplify, the distance is calculated by the street distance.
At present, the code is only a single objective optimization algorithm, non-multi-objective optimization, follow-up research and research.
The code is actually the first car from the distribution center to the first customer location, and then add customer demand (range of cargo weight)
Then determine whether to open to the next customer there canvassing, if the mileage, weight are only in the constraints, the past, do not meet the conditions to open back to the hub; and then continue to judge the second car, it is such a logic
The number of final vehicles is the number of capacity required to complete the 50 customer canvassing.
In the event that the required capacity exceeds the limit (5 cars in the code), it is necessary to punish, because the final function return is double, and the smaller the representation of the better, so encountered the need to punish the situation, is actually a large increase in the return value (fitness value)
The red part is the penalty variable part.
The core of the various optimization algorithms after the framework of the basic will not change, the most variable is actually the fitness function calculation, if the fitness calculation used in the prediction technology, but also in the above function to adjust the machine learning code, the feeling of reinforcement learning in the feedback value given after the action is also such a value
Code download
Canvassing shortest Path Solution algorithm-C # ant colony optimization Algorithm implementation