Advanced Programming Lab 2 Object-Oriented Programming Read the - - PowerPoint PPT Presentation
Advanced Programming Lab 2 Object-Oriented Programming Read the - - PowerPoint PPT Presentation
Advanced Programming Lab 2 Object-Oriented Programming Read the definition of the problem (expressed in natural language) Identify the candidates for the classes . An instance of MDVSP consists of depots , vehicles and clients
Object-Oriented Programming
- Read the definition of the problem (expressed
in natural language)
- Identify the candidates for the classes.
“An instance of MDVSP consists of depots, vehicles and clients (trips).”
- Determine the relationship between classes
- Define the class members:
– constructors, variables, methods – properties
- Draw the UML class diagram
The Client Class
public class Client { private String name; private int order; public Client() { } public Client(String name, int order) { this.name = name; this.order = order; } // … getters and setters public String toString() { return name; } }
Refactor → Encapsulate fields (Generate getter / setter) Why not make everything public? Refactor → Encapsulate fields (Generate getter / setter)
Using the Client Class
public class Main { public static void main(String args[]) { Client c1 = new Client(); c1.setName("Client 1"); c1.setOrder(1); System.out.println(c1.getName()); Client c2 = new Client("Client 2", 1); System.out.println(c2); System.out.println( new Client("Client 3") ); } //where is the bug }
The Vehicle Class
public class Vehicle { private String name; private Depot depot; private VehicleType type; public Vehicle(String name) { this.name = name; } // … getters and setters // … toString @Override public boolean equals(Object obj) { if (obj == null || !(obj instanceof Vehicle)) { return false; } Vehicle other = (Vehicle) obj; return name.equals(other.name); } }
The method setDepot should not be public. Why?
The Depot Class
public class Depot { private String name; private Vehicle[] vehicles; // … constructor(s) // … getter and setter for name public void setVehicles(Vehicle ... vehicles) { this.vehicles = vehicles; for(Vehicles v : vehicles) { v.setDepot(this); } } // … getVehicles // … toString // … equals }
Variable number of arguments
Depot d1 = new Depot("Depot 1"); d1.setVehicles(v1, v2); Depot d2 = new Depot("Depot 2"); d2.setVehicles(v3);
The Tour, Problem, Main Classes
public class Problem { private Depot[] depots; private Client[] clients; … } public class Tour { private Vehicle vehicle; private Client[] clients; … } public class Main { public static void main(String args[]) { Problem pb = new Problem(); //...create vehicles, clients, depots System.out.println(pb); } }
Evolving the Model
public abstract class Vehicle { protected String name; … } public class Car extends Vehicle { … } public class Truck extends Vehicle { … } Vehicle[] vehicles = new Vehicle[2]; vehicles[0]= new Car(); vehicles[1]= new Truck();
The Algorithms
- A greedy allocation of trips to vehicles is ok for
the optional part. Some vehicles may remain unused, some trips may remain uncovered.
- For the bonus, observe that the cost matrix
represents the adjacency matrix of a directed acyclic graph (dag). Implement the algorithm for determining a shortest path in a dag (or Dijkstra).
The Research Project
- Vehicle Routing Problems (VRS)
- Vehicle Scheduling Problems (VSP)
- Both have many variants (multi depot, time
windows, preferences, online, etc)
- Many applications in public transportation
- Approached with flows, circulations, integer