Query to work out possible destinations on a set of routes using SQL Server 2008 - tsql

I'm writing an application that models train routes, which are stored in the database table [TrainStop] as follows:
TrainServiceId
StationCode
StopIndex
IsEnabled
So a given train follows a route and consists of several rows with the StopIndex indicating the order. The problem I am trying to solve is to say which stations a user can get to from a given starting station based on the train services that are available. This would be relatively straightforward BUT it is also possible to disable stops which means that a user cannot get to any destinations after that stop for that train service. It is also possible that multiple trains on different routes can share stations e.g.:
Train Service 1: A, B, C, D, E
Train Service 2: P, Q, B, C, D, R
So if a user is at B they can go to C, D, E and R but if station D is disabled on either service they can get to C only. If station E is disabled on service 1 the user can get to C, D and R.
Solving this problem is fairly straightforward within C# but I am wondering whether it can be solved elegantly and efficiently within SQL? I'm struggling to find a way, for each route, to rule out stations past a row that is not enabled.

I think I might set it up this way. Three tables:
ROUTE
List of routes
STATION
List of stations
ROUTEPATH (or route plan?)
Many-to-many table between route and station, including the StopIndex attribute
Whether a station is enabled or disabled is both an attribute of an individual station (station is closed) AND an attribute of the route path (station before "this" station is closed, so "this" is also closed). That means that IsEnabled should appear in bot the STATION and ROUTEPATH tables.
When a station is to be disabled:
Flag it as disabled in STATION
Flag it as disabled in ROUTEPATH
For each route containing that station, flag all stations with StopIndex greater than that station's as disabled (these last two can be done as one update query)
When a station is to be enabled, reverse the process.
There will of course be problems when more than one station becomes disabled--or rather, when some but not all disabled stations become enabled again. I think it would be better to make ROUTEPATH's attribute "IsDisabled", and make it an integer:
Zero means not disabled
Non-zero means disabled
Increment it by one for each "station disabled" action
Decrement it by one for each "station enabled" action
A system like this should lend itself to relatively simple queries.

Related

Access primary address of ESP-BLE-MESH publishing node in ESP-IDF

I have a simple BLE mesh (using Espressif's ESP-BLE-MESH) consisting of two nodes; one containing 3 generic onoff server elements publishing to a group, and one containing a single generic onoff client subscribed to that group. Each node is a little ESP32 dev board with an RGB LED built in. I'm programming in C, using the ESP-IDF extension for Visual Studio Code.
I want the 3 onoff servers to control the R, G and B of the LED, respectively, but all I have to distinguish a server from the others is the address of its element, which is the primary address of the node + some offset. I want to get that offset to determine which of R, G and B are being switched, but I can't figure out how to access the primary address of the publishing node to work that out. I've trawled the documentation and tried every member of the esp_ble_mesh_generic_client_cb_param_t struct passed to the example_ble_mesh_generic_client_cb function, going down such rabbit holes as param->params->model->pub->publish_addr, but have been unsuccessful. Is what I'm trying to achieve even possible?
Server and client code used: ble mesh node

How can I take the closest resources to specific stations from ResourcePool?

I'm simulating a manufacturing process. The stations need a variable number of ResourcePool "workers". Let's say there are 5 workers for 4 stations, and I want to simulate that those workers can work indifferently in any of the stations.
The problem is that they are not working in the closest station. Oppositely, even when a worker is next to the station, maybe the furthest worker goes until this station which needs the resource, and then the process starts. Obviously, this is inefficient and doesn't represent the reality.
I already have the workers defined, their shifts, their timetables, etc. I have tried to use the function getNeartestAgent() in 'Customize request choice', but it doesn't work. I also tried to calculate the distance of each agent with getDistance(), but I don't know how to manage it and I'm sure it must be a simpler solution even when I'm not working on GIS.
[31/08/22]
Recently I tried to develop a similar model, and I still have the same problem. The model consists of 3 stations which need 1 unit of ResourcePool. In this pool, there are 2 units. This units are agent type Operario. So, I need to choose the closest unit to the station. I tried several things, the following are just some...:
Service blocks -> Resource selection -> "Nearest to the agent". The agent is the material item, so the unit should go to the closest agent being treated in the station. However, it didn't work (units where to any station indeferently).
Service blocks -> Resource selection -> "Unit with top rating". The Unit rating field was 1/distanceTo(node1.getXYZ()). It didn't work either.
Stations (instead of Service blocks) -> Customize resource choice -> Resource choice condition. I can't stablish a boolean variable related to the unit position, as I can't compare among them.
Function. I can't compare units from ResourcePool, as they are detected as Agent and not as Operario.
ResourcePool. I tried to stablish the priority based on the distance to a certain node. But then I can't call these agents (Operario) from the station. It seems here I can only refer to material items, eventhough I write "unit.(...)".
Does anyone know how can I choose the closest resource to the station in which the task is performed? Should be through Customize resource choice -> Resource choice condition, but I don't know how!
when you use a service or seize block, in the advanced properties, there's an option called "Resource Selection"
You have to choose "Nearest to the agent"
If this doesn't work, it might mean that a resource that is close by and not selected, might be actually busy with something else... you have debug your model in order to be sure that it is not the case.

In Anylogic How can I model truck that deliver orders to multiple clients

I'm creating a model based on the product delivery example provided by AnyLogic. In my own model, I want a truck to deliver multiple orders in one trip instead of one. My process diagram is shown below. Here, an order enters via the enter block and several orders are accumulated in the batch block. Every order has a specified destination. How do I model the truck such that it combines two orders and move to the nearest delivery location first and then the second etc?
The main problem is that I don't know the code that accesses the parameter "Delivery location" in each order.
enter image description here
enter image description here
Additional information:
The orders agents are generated and the delivery location is stored in a parameter called "client"
The batch block combines (lets say 2) orders into a batch of the type Order ( advanced settings set to population of agents)
The service block pulls a truck from the resource poule and send the batch of orders to the truck agent using send(batch.unit)
The truck agent stores the order/orders(?) in a variable called "order"
Then, a moveTo function should deliver the order to the first destination
What would be the code to move to the first, second etc., destination?
Here is the conceptual piece you are probably not aware of, and that should help you move in the right direction:
you can have "for loops" as part of your process flows. Below, you see an example where an agent keeps driving to places until it has no more parcels.
Obviously, the details of the blocks depend on your model but in each, you can access the truck's orders if you have them in your Truck agent type (which is needed, obviously).

How to create orders for picking process at warehouse operations using Anylogic

During the creation of a generalized warehouse model, I ran into a problem when trying to create an order event that can be used by order pickers to retrieve the products from the storages racks. Currently, I am using a source block which creates "orders" of a single type of pallet (1 to 5). The pickers each travel to pick up a pallet from the rack and transports them to the next location.
Question: How can I create an order consisting out of several pallets consisting of different types.
Question: How can I use a single picker (resource) to pick multiple pallets in a single run through the warehouse before transporting all the pallets to the next location (process).
Kind regards,
Stefan
Question 1
normally i would create a class for the higher level order (say Pickwave class).
And when instantiating it you need to save a list of actual picking orders(pallets) into it (say collection of type ArrayList<Order>).
And to process your pickwave you can use a loop which will steer your resource/transporter/picker to the next Order location until all orders are picked. If everything is picked you exit your loop and move to your next location
Question 2
you don't really pick the orders as you would normally pick them using RackPick block in 1-to-1 scenario. But as you have a refence to all your orders inside you pickwave object you can still control their location/animation programmatically.

Reconciling schedule data (Solve a logic problem)

I have a problem I need to solve using Node. The question I have is surrounding the best logical way to solve it. Any advise is appreciated.
Summary
You will build a tool that imports train schedules from an external data source and is stored in an internal database. 
Eternal Data Source
There is a service that provides a list of trains:
[{"id":1,"name":"A EXPRESS"},{"id":2,"name":"B EXPRESS"},{"id":3,"name":"C EXPRESS"},{"id":4,"name":"D EXPRESS"},{"id":5,"name":"E EXPRESS"}]
And a list of train stations:
Example for train "A EXPRESS":
[{"arrival":"2019-04-30T11:48:00.000Z","departure":"2019-05-01T05:42:00.000Z","service":"Loop 5","station":{"id":"ST1","name":"Waterloo"}},{"arrival":"2019-05-13T18:00:00.000Z","departure":"2019-05-14T05:00:00.000Z","service":"Loop 5","station":{"id":"ST2","name":"Paddington"}},{"arrival":"2019-05-15T04:00:00.000Z","departure":"2019-05-15T11:00:00.000Z","service":"Loop 5","station":{"id":"ST3","name":"Heathrow"}},{"arrival":"2019-05-16T20:00:00.000Z","departure":"2019-05-17T10:00:00.000Z","service":"Loop 5","station":{"id":"ST4","name":"Wimbledon"}},{"arrival":"2019-05-18T15:00:00.000Z","departure":"2019-05-19T21:00:00.000Z","service":"Loop 5","station":{"id":"ST5","name":"Reading"}},{"arrival":"2019-05-21T04:00:00.000Z","departure":"2019-05-21T21:00:00.000Z","service":"Loop 5","station":{"id":"ST6","name":"Algate"}},{"arrival":"2019-05-31T03:00:00.000Z","departure":"2019-05-31T15:00:00.000Z","service":"Loop 5","station":{"id":"ST1","name":"Waterloo"}}]
Note: this train stops at station "ST1" ("Waterloo") twice.
To do
For each imported station call, we want to maintain the latest information as well as the history of the station call: 
-What station is the train calling? 
-What are the latest arrival and departure dates? 
-When was the station call first imported? 
-When was the station call last updated? 
-How did the station call change as time went? (evolution of arrival and departure dates with time) This kind of information is useful for us to understand how often trains are delayed, when do schedule changes happen and if there are patterns to these changes. 
How it works
-The external data source is a simulation of train schedules forecasts 
-The data covers a time range from January 1st 2019 to May 31st 2019 
-This 5 months time window is compressed and simulated over a 24 hours cycle 
-This 24 hours cycle restarts every day at 00:00 UTC 
-The data source provides endpoints to request train schedules
-A train list endpoint provides a dynamic list of trains that you can import (see Data above)
-A train schedules endpoint provides a dynamic list of stations calls for a specific train (see Data above)
-A train schedule consists of a list of station calls with a varying amount of past station calls and future station calls
-This external data source does not provide a unique identifier for each station call
-This means that merging station calls is not straightforward. This is the crux of my question: reconciling external station calls with the existing ones in the database. 
-Station calls arrival and departure dates routinely change, sometimes by multiple days. Sometimes they swap, get deleted or new ones appear
-Station calls can sometimes be deleted (the train will not stop in that specific station
-New station calls can sometimes be created (the train will make an unscheduled stop)
-The train schedules endpoint changes the data returned every 15 minutes. 
Specific requirements
You need to capture 24 hours of all train schedules starting at 00:00:00 UTC on one day and ending at 23:59:59 UTC on the same day.   
Question
As you can see from the task above, there is a need to reconcile the new imported data with existing data as the new data changes.
There is no ID that can be used to match station visits, but these visits need to be updated when the external data changes.
We do have a train ID and a station ID as well as the date of visits.
What is the logic I can apply to keep the database data accurate and up to date?
Thank you
Answer?
My initial thoughts are to do the following, however I am not sure if this is the best solution. If you have a better solution, or see a problem with mine, please let me know.
The list of station calls retrieved from the external service will always be saved to the internal database under its retrieval timestamp. This will always be displayed as the current information in the UI (latest database entry).
Each 15 min a call is made to the external service and a new latest entry is added to the internal database, and reflected on the UI.
The status of the latest entry needs to reflect the difference between the the latest entry and the previous entry. E.g. delayed by X min, cancelled, etc. This is the tricky part, because the current station visit needs to be matched with a previous station visit.
My thinking is for a specific train is to just find the matching station ID.
If there is no previous matching station ID, then the status is "new".
if there is previous station ID, and no matching current station ID, the status is "cancelled".
if there is one matching station ID, then there times are compared, and the status is updated to "early" or "delayed".
If there is more than one matching station ID, the station previous and current station IDs with the closest timestamps are matched, and their status updates accordingly to "early" or "delayed".
Is my logic correct?