数据结构
- 节约里程表
/**
* 包含节约里程,起点、终点(节约里程表的数据结构)
*
*/
public class Path {
public double save_distance;//节约里程
public int head; //起点
public int tail; //终点
public Path(int head,int tail,double save_distance){
this.head = head;
this.tail = tail;
this.save_distance = save_distance;
}
public Path(){}
}
- 路线表
public class Route {
/**
* 剩余容量
*/
public double residual_load;
/**
* 路线上地点的数组
*/
public ArrayList route;
/**
* 路线总路程
*/
public double distance_count;
}
具体算法实现
import java.util.ArrayList;
import com.tool.GetDate;
import com.bean.Path;
import com.bean.Route;
/**
* @instruction 节约里程算法
*/
public class Distance {
static double[][] minPath;//最短路线矩阵
static double[] demand; //每个点的需求量
static double whole_capacity; //最大载货量
static int num = 5; // 配送点的数量
static ArrayList<Route> route_result = new ArrayList<Route>(); //路线数组
static ArrayList<Path> save_distance;//节约里程
public static void main(String[] args) {
// 从文件中读取最短路径矩阵和每个点的需求量(文件在data目录下,使用了com.tool中的工具类)
minPath = GetDate.readCSV_distance();
demand = GetDate.readCSV_demand();
whole_capacity = 4; // 最大载货量
num = 5; //配送点数量
//计算节约里程
save_distance = Distance.process(demand, minPath);
//计算节约路线
find_path();
//组织输出,更符合阅读习惯(标号从0更改到1),增加起点终点
System.out.println("-----------------------------------------");
System.out.println("最终输出");
int count = 0;
for (Route r : route_result) {
ArrayList routes = r.route;
System.out.print("P0");
for (int i = 0; i < routes.size(); i++) {
int n = (int)routes.get(i) + 1;
System.out.print("-P" + n + "-");
}
System.out.print("P0");
count += r.distance_