/* * set finished tag for all ta in this route * according to the given ctime. * The running job is also set to finished (even) */ public void setFinishedTA(Calendar ctime) { if (nw.periodStartTimes[period].after(ctime)) { return; } int size = taskSet.size(); for (int i = 0; i < size; i++) { Task t = taskSet.get(i); Calendar startTime; if (i == 0) startTime = nw.periodStartTimes[period]; else startTime = taskSet.get(i - 1).startT; if (ctime.after(startTime)) t.finished = true; if (ctime.before(t.finishT)) break; // if ctime > startTime // set this to finished // if ctime < finishTime // break; } }
public String toString() { String statistics = new String(), ports = "Ports: ", tasks = "Tasks: ", times = "Times: "; if (check() <= 0) { statistics += "*"; nw.updateObjectiveValues(this); } for (int i = 0; i < taskSet.size(); i++) { Task t = taskSet.elementAt(i); if (i == 0) { statistics += "Route: " + this.id + " driver: " + t.driver + " vehicle: " + t.vehicleID; statistics += " ED: " + this.emptyDistance + " LD: " + this.loadedDistance; } ports += "[" + t.cmdt.src.id + " " + t.cmdt.dest.id + "] "; tasks += t.cmdt.id + "("; if (t.size == 1) tasks += "s"; else // large tasks += "b"; if (t.gT != null) tasks += "s"; if (t.finished) tasks += "f"; tasks += ") "; times += "<"; if (t.startT != null && t.finishT != null) times += t.startT.getTime() + " " + t.finishT.getTime(); else times += "Infeasible"; times += " " + t.getLateness() + " "; times += "> "; } return statistics + "\n" + ports + "\n" + tasks + "\n" + times + "\n"; }
/** * get all the routes that has been assigned from the database save into Vector<Route> to be used * by Change.java controller * * @return */ public Vector<Route> getRoutes() { DBManager m = new DBManager(); ResultSet rs = m.getRs( "SELECT r.id AS route_id,s.sessionID,session_driverName," + "session_vehicleID,startTime,expireTime,status,shift_normal_start_time " + "FROM Route r,Session s WHERE s.sessionID = r.sessionID"); Vector<Route> routes = new Vector<Route>(); try { while (rs.next()) { int route_id = rs.getInt("route_id"); int sessionID = rs.getInt("sessionID"); String driverName = rs.getString("session_driverName"); String vehicleID = rs.getString("session_vehicleID"); long startTime = Long.parseLong(rs.getString("startTime")); long expireTime = Long.parseLong(rs.getString("expireTime")); int status = rs.getInt( "status"); // status, 0:UNASSIGNED, 1:ASSIGNED£¬2£ºSESSION_SUSPENDED£¬ // 3£ºSESSION_ENDED Calendar shift_normal_start_time = StringToCalendar(rs.getString("shift_normal_start_time")); // TODO:What is Network and period Route route = new Route(null, route_id, 0); XMLSession session = new XMLSession(driverName, vehicleID, startTime, expireTime); route.setSession(session); route.setStatus(intToStatus(status)); ; // TODO:HOW to use the shift_normal_start_time? Vector<Task> taskSet = new Vector<Task>(); ResultSet rss = m.getRs( "SELECT t.id AS task_id, t.commodity_id,sequence_num," + "t.small AS task_small,t.large AS task_large,planned_start,planned_finish," + "actual_start,actual_finish,source,destination,available_t,deadline," + "c.small AS com_small, c.large AS com_large,finished_small,finished_large " + "FROM Route r, Task t, Commodity c " + "WHERE r.id = t.route_id AND t.commodity_id = c.id AND r.id =" + route_id + " " + "ORDER BY sequence_num ASC"); while (rss.next()) { int task_id = rss.getInt("task_id"); String commodity_id = rss.getString("commodity_id"); // int sequenceNo = rss.getInt("sequence_num"); int task_small = rss.getInt("task_small"); int task_large = rss.getInt("task_large"); Calendar planned_start = StringToCalendar(rss.getString("planned_start")); Calendar planned_finish = StringToCalendar(rss.getString("planned_finish")); Calendar actual_start = StringToCalendar(rss.getString("actual_start")); Calendar actual_finish = StringToCalendar(rss.getString("actual_finish")); String source = rss.getString("source"); String destination = rss.getString("destination"); Calendar available_t = StringToCalendar(rss.getString("available_t")); Calendar deadline = StringToCalendar(rss.getString("deadline")); int com_small = rss.getInt("com_small"); int com_large = rss.getInt("com_large"); int finished_small = rss.getInt("com_small"); int finished_large = rss.getInt("com_large"); Task t = new Task(); // TODO:WHAT IS loadTime and unloadTime Node src = new Node(source, 0, 0); Node des = new Node(destination, 0, 0); Commodity c = new Commodity(commodity_id, available_t, deadline, src, des, com_small, com_large); t.cmdt = c; t.taskID = task_id; // TODO:Not sure if its right.. if (task_small > 0) { t.size = 1; } else { t.size = 2; } t.startT = planned_start; t.finishT = planned_finish; t.actualStartT = actual_start; t.actualFinishT = actual_finish; t.driver = driverName; t.vehicleID = vehicleID; taskSet.add(t); } route.taskSet = taskSet; routes.add(route); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return routes; }
/** * update the new route and sequenceNo of task send to phone * * @param taskId * @param newRouteID * @param newSequenceId */ public void updatePhoneTask(int taskId, int newRouteID, int newSequenceId) { DBManager m = new DBManager(); ResultSet rs = m.getRs( "SELECT s.*,t.* FROM Session s, Task t, Route r WHERE t.id = " + taskId + " AND t.route_id = r.id AND s.sessionID = r.sessionID"); try { while (rs.next()) { String driverName = rs.getString("session_driverName"); String vehicleID = rs.getString("session_vehicleID"); long startTime = Long.parseLong(rs.getString("startTime")); long expireTime = Long.parseLong(rs.getString("expireTime")); int sequenceNo = rs.getInt("sequence_num"); // save the action into updateTasks // remove task first XMLSession s = new XMLSession(driverName, vehicleID, startTime, expireTime); ArrayList<UpdateManager> ml = Global.getUpdateTasks(); int l = ml.size(); int index = -1; for (int i = 0; i < l; i++) { if (ml.get(i).getSession().equals(s)) { index = i; break; } } UpdateManager um = new UpdateManager(s); ArrayList<SingleUpdateManager> sl = new ArrayList<SingleUpdateManager>(); if (index != -1) { um = ml.get(index); sl = um.getTasks(); } Task t = new Task(); SingleUpdateManager a = new SingleUpdateManager(Action.delete, t); a.setSequenceNo(sequenceNo); sl.add(a); um.setTask(sl); if (index != -1) { ml.set(index, um); Global.setUpdateTasks(ml); } else { Global.addUpdateTask(um); } // then add new task ResultSet rss = m.getRs( "SELECT s.* FROM Route r, Session s WHERE r.sessionID = s.sessionID" + " AND r.id = " + newRouteID); while (rss.next()) { String driverName1 = rss.getString("session_driverName"); String vehicleID1 = rss.getString("session_vehicleID"); long startTime1 = Long.parseLong(rss.getString("startTime")); long expireTime1 = Long.parseLong(rss.getString("expireTime")); ml = Global.getUpdateTasks(); l = ml.size(); int index1 = -1; XMLSession s1 = new XMLSession(driverName1, vehicleID1, startTime1, expireTime1); for (int i = 0; i < l; i++) { if (ml.get(i).getSession().equals(s1)) { index1 = i; break; } } UpdateManager um1 = new UpdateManager(s1); ArrayList<SingleUpdateManager> sl1 = new ArrayList<SingleUpdateManager>(); if (index1 != -1) { um1 = ml.get(index1); sl1 = um1.getTasks(); } String commodityID = rs.getString("commodity_id"); ResultSet rsss = m.getRs("SELECT * FROM Commodity WHERE id = '" + commodityID + "'"); if (rsss.next()) { String source = rsss.getString("source"); String destination = rsss.getString("destination"); String available_t = rsss.getString("available_t"); String deadline = rsss.getString("deadline"); int small = rsss.getInt("small"); int large = rsss.getInt("large"); Task t1 = new Task(); Node src = new Node(source, 0, 0); Node dest = new Node(destination, 0, 0); Commodity c = new Commodity( commodityID, StringToCalendar(available_t), StringToCalendar(deadline), src, dest, small, large); t1.cmdt = c; SingleUpdateManager b = new SingleUpdateManager(Action.add, t1); b.setSequenceNo(newSequenceId); sl1.add(b); um1.setTask(sl1); if (index1 != -1) { ml.set(index1, um1); Global.setUpdateTasks(ml); } else { Global.addUpdateTask(um1); } } } } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } this.updateDBSTask(taskId, newRouteID, newSequenceId); m.destory(); }
/** * get All the task by sessionID used in case of first-login and re-login * * @param sessionID */ public void getTasks(int sessionID) { DBManager m = new DBManager(); ResultSet rs = m.getRs("SELECT * FROM Session WHERE sessionID = " + sessionID); try { if (rs.next()) { String driverName = rs.getString("session_driverName"); String vehicleID = rs.getString("session_vehicleID"); long startTime = Long.parseLong(rs.getString("startTime")); long expireTime = Long.parseLong(rs.getString("expireTime")); XMLSession s = new XMLSession(driverName, vehicleID, startTime, expireTime); ArrayList<UpdateManager> ml = Global.getUpdateTasks(); int l = ml.size(); int index = -1; for (int i = 0; i < l; i++) { if (ml.get(i).getSession().equals(s)) { index = i; break; } } UpdateManager um = new UpdateManager(s); ArrayList<SingleUpdateManager> sl = new ArrayList<SingleUpdateManager>(); if (index != -1) { um = ml.get(index); // Clear the tasks of this session // sl = um.getTasks(); } ResultSet rss = m.getRs( "SELECT commodity_id,sequence_num FROM Route r, Commodity c, Task t WHERE r.sessionID = " + sessionID + " AND route_id = r.id AND c.id = t.commodity_id ORDER BY sequence_num ASC"); while (rss.next()) { String commodityID = rss.getString("commodity_id"); int sequenceNo = rss.getInt("sequence_num"); ResultSet rsss = m.getRs("SELECT * FROM Commodity WHERE id = '" + commodityID + "'"); if (rsss.next()) { String source = rsss.getString("source"); String destination = rsss.getString("destination"); String available_t = rsss.getString("available_t"); String deadline = rsss.getString("deadline"); int small = rsss.getInt("small"); int large = rsss.getInt("large"); Task t = new Task(); t.driver = s.getDriverName(); Node src = new Node(source, 0, 0); Node dest = new Node(destination, 0, 0); Commodity c = new Commodity( commodityID, StringToCalendar(available_t), StringToCalendar(deadline), src, dest, small, large); t.cmdt = c; SingleUpdateManager a = new SingleUpdateManager(Action.add, t); a.setSequenceNo(sequenceNo); sl.add(a); um.setTask(sl); // SAVE THE UPDATE TASK INTO GLOBAL VARIABLE if (index != -1) { ml.set(index, um); Global.setUpdateTasks(ml); } else { Global.addUpdateTask(um); } // System.out.println(Global.getUpdateTasks().size()); } } } } catch (NumberFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } m.destory(); }
/* * Checks Deadline, total Time * return 3 => succeed (last task emergent) * return 2 => succeed (soft time window) * return 1 => succeed (strict time window) * return 0 => exceed return deadline * return -1 => time window conflict * * after check, the startT and finishT is updated correctly. */ public int check() { Calendar currentTime = (Calendar) nw.periodStartTimes[this.period].clone(); Calendar endTime = nw.periodEndTimes[this.period]; loadedDistance = emptyDistance = waitTime = softTimeWindowPenalty = 0; int taskSetSize = taskSet.size(); for (int i = 0; i < taskSetSize; i++) { Task currentTa = taskSet.elementAt(i); currentTa.emergent = false; Commodity c = currentTa.cmdt; int srcI = c.src.index, destI = c.dest.index, depotI = nw.depot.index; // dead heading to source node. if (i == 0) { // depot to src currentTime.add(Calendar.MINUTE, (int) nw.getTravelingTime(depotI, srcI)); emptyDistance += nw.getTravelingDistance(depotI, srcI); } else { // dest to src Commodity c2 = this.taskSet.elementAt(i - 1).cmdt; if (!c.src.equals(c2.dest)) { currentTime.add(Calendar.MINUTE, (int) nw.getTravelingTime(c2.dest.index, srcI)); emptyDistance += nw.getTravelingDistance(c2.dest.index, srcI); } } // Carrier just arrived at source node. if (currentTime.compareTo(c.availTime) < 0) { // arrive too early this.waitTime += (c.availTime.getTimeInMillis() - currentTime.getTimeInMillis()) / 1000 / 60; currentTime = (Calendar) c.availTime.clone(); } currentTa.startT = (Calendar) currentTime.clone(); // load time of commodity c currentTa.emergent = (int) ((c.deadline.getTimeInMillis() - currentTime.getTimeInMillis()) / 1000 / 60 - 120) // should arrive earlier, not just in time!! < nw.getTravelingTimeWithQueue(srcI, destI); // if (!currentTa.emergent) { currentTime.add(Calendar.MINUTE, c.src.loadTime); // } // src -> dest currentTime.add(Calendar.MINUTE, (int) nw.getTravelingTime(srcI, destI)); loadedDistance += nw.getTravelingDistance(srcI, destI); // unload ta1, task(s) finished // if (!currentTa.emergent) { currentTime.add(Calendar.MINUTE, c.dest.unloadTime); // } if (currentTime.compareTo(c.deadline) > 0) { // System.out.println("Too late to unload!"); return -1; } currentTa.finishT = (Calendar) currentTime.clone(); // if last task, go back to depot if (i == taskSetSize - 1) { // dest to depot currentTime.add(Calendar.MINUTE, (int) nw.getTravelingTime(destI, depotI)); this.emptyDistance += nw.getTravelingDistance(destI, depotI); } if (currentTime.compareTo(endTime) > 0) { if (i == taskSetSize - 1) { // emergent and it is last task if (currentTa.emergent) return 3; // Soft time window // double lateness = currentTime.getTimeInMillis() - endTime.getTimeInMillis(); // lateness = lateness / 1000 / 60; // // this.softTimeWindowPenalty = lateness / 2; // return 2; } else { return 0; } } // The delay is calculated, but not included in finish time; // This is put here because once a job is found to be delayed, // Only later jobs should be reconsidered, it is already to late // to change it if period deadline is missed. currentTime.add(Calendar.MINUTE, currentTa.getLateness()); } return 1; }