Search code examples
databaseroutessimulationanylogic

Anylogic: How to add costs (per route) per time of day?


I have data on different costs per route per time of day for container transport (e.g., from zone A to zone B in the morning; the total costs of transport are 100 euros), for almost 200 zones with 4 times of day. How can I assign these costs in my Anylogic model per route per time of day?

(after this I would like agents (trucks) to decide (based on the costs) which route and time of day to travel)


Solution

  • Given no example data I am using some made-up data to give you an example of how to do this.

    Suppose I have the following route and cost data

    enter image description here

    enter image description here

    You can import this into the AnyLogic DB and then use them to populate a custom class with your data.

    For example here is a custom Java class for your routes

    public class MyRoute {
        String id;
        String from;
        String to;
        
        LinkedHashMap<String, Double> routeCosts = new LinkedHashMap<String, Double>();
        
        /**
         * Default constructor
         */
        public MyRoute(String id, String from, String to) {
            this.id = id;
            this.from = from;
            this.to = to;
        }
        
        public void addCost(String timeOfDay, double cost) {
            routeCosts.put(timeOfDay, cost);
        }
    }
    
    

    And then I have a little function to populate them from the DB

    List<Tuple> rows = selectFrom(routes).list();
    
    for (Tuple row : rows) {
        MyRoute route = new MyRoute(
            row.get( routes.route ),
            row.get( routes.from_db ),
            row.get( routes.to_db )
        );
        
        // Add costs
        List<Tuple> costRows = selectFrom(costs)
        .where(costs.route.eq(route.id))
        .list();
    
        for (Tuple costRow : costRows) {
            route.addCost(
                row.get( costs.time_of_day ), 
                row.get( costs.cost )
            );
        }   
    }
    
    

    Now you can sort the routes based on the costs or time of day and use this to make your decisions

    You can see more on sorting here https://www.baeldung.com/java-hashmap-sort