Round Robin Seeding simple logic iteration psudocode required - asp.net-mvc-4

I'm going bananas at something that should be simple. A teamfixtures table for a league database. Imagine we have 12 teams with primary key 1,2,3... until 12 in team table
Each team plays each other twice home and away. Imagine that team fixtures is empty but I want to loop 22 (total weeks of league with 12 teams) times with an inner loop of 6 times (since 6 days games a week)
The idea is to make it so that (as much as possible) each team gets home game then and away game
So the table we want to populate is team fixtures with a hometeamId and an AwayTeamId (remembering 1-12 for each team))
How can we loop in c sharp and have it seed 1st half of season everyone plays everyone once. Then the 2nd half repeating but reversed
I keep getting into a mess with the logic and the examples online explain the pricinples of round robin seeding but no good simple examples
Trying now to rotate them clockwise I think thats the way to go, should be easy but its tricky
REgards
J

Related

SQL problem: Selecting subset of weekly fixtures so that at end of season all selected teams have played same amount of home games

Please note that this is not the usual question about generating a fixture list for a league.
This problem is really doing my head in as I can't come up with a solution that wouldn't involve generating stupidly large results tables that couldn't be processed in my lifetime.
There are 19 teams in a league, 9 matches each week (1 team gets a rest each week). Over the season they play each other home and away. Now the meaty bit. 6 of the 9 matches are selected for a predictions competition. At the end of the season all of the teams in the league must have been selected for the prediction comp and all must have played the same number of home games in the predictions. The problem I have is how can I use SQL to generate each week's 6 fixtures to use in the predictions comp?
The numbers get silly because the are C(9,6)=84 possible combinations of matches that can be selected each week, so over just a few weeks the numbers get impossibly large. I'm sure there must be a solution for this but I'm well and truly stumped. Many thanks in advance.
PS: I don't need all possible combinations of prediction fixtures as that too would be impossibly large, just one result that satisfies the criteria.

Customer Schedulling & route optimization

I'm new to ORTools and I need some guidance on which algorithm to choose for this problem:
(I think it is an assignment problem, but also a mix with route optimization and VRP..)
A salesman has N shops to visit
Every shop has a service time T (ex: 30 minutes)
Shops are categorized this way:
a) Category 1 = must be visited twice a week
b) Category 2 = must be visited once a week
c) Category 3 = must be visited biweekly (every two weeks)
d) Category 4 = must be visited once a month
A shop cant be visited 2 days in a row
The salesman should work up to 9 hours (1 hour for lunch)
The salesman should visit 8 to 12 shops a day (soft constraint)
The salesman should work up to 176 hours a month
Some shops can be discarded from the solution. (prefer to keep the ones with lower category)
We have the geolocation and a time distance matrix for the shops
The objectives are:
Maximize the number of shop visited
Minimize the KMs for the salesman
This is an example of shops distribution
First of all, it seems like you're trying to solve the problem globally, i.e. to find an optimal monthly or yearly schedule for the entire problem. I suspect that might be difficult, and suggest to try relaxing the problem, say: to a daily one.
For instance, maybe for a given day, it's possible to compile a list of stores which should be visited, perhaps by order of scores. So maybe a store that was visited yesterday will not be in the list; and one that has to be visited once a week, and was visited 6 days ago would be on the top of the list.
Then, you might be able to solve using a variant of the VRP solver on that list; you can use simulation (maybe with some random element) to generate a weekly or monthly schedule based on that. It's not guaranteed to be optimal, but might be good enough.
If your problem is more complex then what the OR-Tools VRP solver can handle, you can try using the more general CP-SAT solver, which is very generic, and can handle VRP-like problems with additional constraints, e.g. Scheduling problems or other constraints.
You can also consider using other approaches here, e.g. Tabu search.

vb.net calculating booked appointment times from a list of available times?

I'm trying to find an efficient way to calculate the booked times for a user(object), given a list of free/available times for the same user\object.
I have an object that will return the "available" times for a given specific day. The duration/end time is fixed to 10 minutes.
Example Starting data:
12/23/2020 8:00 AM
12/23/2020 9:00 AM
12/23/2020 1:00 PM
In this case I need to generate the "unavailable" times and insert them into a database with a fairly simple schema:
start_date | end_date | start_time | end_time
The inserting is fairly trivial, i'm having a hard time determining the best way to calculate the unavailable timespans.
Using the example above i would need to generate the following timespans:
12/23/2020 12:00 AM - 7:59 AM
12/23/2020 08:11 AM - 8:59 AM
12/23/2020 09:11 AM - 12:59 PM
12/23/2020 1:11 PM - 11:59 PM
Any frameworks or libraries that can do the heavy lifting on this for me? Is it possible to solve this problem without looping through the results and calculating all of the offsets?
To anyone asking "why" - hooking together two legacy systems, one system returns the available appointments for a given date this needs to be plumbed into a system that needs the un-available appointments for a given date.
Well, first I written more tour booking systems then I can shake a stick at.
The one Rosetta stone that holds true?
You don't want to generate or have booking slots that are NOT being used in the system PERIOD!!!
Thus you ONLY ever enter into the system a valid booking (starttime, and end time). And that startTime should be a datetime column - this will VAST reduce the complexity of your queries. Given you have date and separate time? Well, then your queries will be more complex - I'll leave that to you.
Given the above? The simple logic to find a booking collision in ALL cases is this:
A collision occurs when:
RequestStartDate <= EndDate
and
RequestEndDate >= StartDate
Now in above, I assume date values, or datetime values.
So if I want a list of any booking for today?
RequestDDTStart = 2020-12-23 9 AM
RequestDTEnd = 2020-12-23 5 PM
And thus any collision can be found with this:
strWhere= dtRequestStartDate <= BookingEndDate" & _
" and dtRequestEndDate >= BookingStartDate"
Now, assumging .net, then above would be something like this as parameters
strWhere= #dtRequestStart <= BookingEndDate" & _
" and #dtRequestEnd >= BookingStartDate"
So, above would return all bookings for today 9 am to 5 pm
A REMARKABLE simple query! Now of course the above query could/would include the exam room, or hotel room or whatever as an additional criteria. But in ALL cases the above simple query returns ANY collision for that 9 am to 5 pm.
And the beauty of this system? As long as you never allow a over-lap into the booking system, then you can book a 10 minute or a 20 minute or a 30 minute session as ONE entry into the database. I would thus not need to create 3x 10 minute slots.
So, this means you NEVER have to create booking slots. The whole system will and can be driver with a simple start + end booking record. And as noted, then you can book 1 hour, or 40 minutes. Your input (UI) can simple limit the time span to increments of 10 minutes - but that's the UI part.
Now I suppose to display things in 10 minute increments on a screen? Well, then you would have to submit 6 requests per hour to "display" the time slots. For a whole day, that suggest for 9 am to 5 pm, you would have to run 8 x 6 = 48 requests to get a list of 10 minute increments. But then again, you COULD just show the existing bookings for a day, and allow new bookings to be added - but don't allow if there is a over lap.
So, as noted, the concept here is you don't really need "slots" in the database. I suppose you could try slots, but it makes the code a HUGE mess to deal with. if you ONLY ever store the start + end? Then I can say move the booking to another day by JUST changing the date. Or I can extend a booking from 10 minutes to say 20 or 40 minutes - and ONLY have to change the end time. As long as no overlap occurs with the above simple "test", then I can simple change the booking to be 40 minutes in length - and ZERO code to update multiple slots is required. And same goes for reducing a booking from 40 minutes to 10 minutes. Again ONLY the end time need be reduced - a ONE row update into the database.
So if at all possible, I would dump the concept of having "slots" in the database. I might consider such a design if a booking was only ever 10 minutes. But if 10 or 20 or 30 is allowed, then you don't need to store ANY un-used slots in the database, but ONLY ever store a valid booked slot. Empty un-used time can thus ALSO be found with the above query. (if the query returns records - then you can't book).
So display of free time in some UI becomes more of a challenge, but showing bookings that span 10 or 20 or whatever minutes is far more easy, and as noted, you can even change a whole booking to a different room by a ONE row update of the room ID. If no collision occurs, then you allow this booking - and you achieve this result by ONLY updating one simple booking record that represents that start + end time.
and this means you also NEVER store the booking totals in the database - you query them!
I also found that if I say store any booking totals in the database? Well, with complex code, we always found that the totals often don't match perfect. So then we wind up writing a routine to go though the data, sum up the totals and write those out.
But, if you never store any booked totals (say people on a bus, or people in a given hotel), then while the query for such display is somewhat more difficult, it becomes dead simple to remove a person from say a tour by simple null out of the tourID.
So, this display shows the above concepts in Action. And the available rooms in the hotel, people booked on bus, and even totals for "group tours" are ALL values NOT stored in the database:
So in above, people booked on bus, booked in rooms, and rooms used? All those values are NOT stored in the database. And no slots exist either. So if we have a bus, then we set the capacity of 46, but we do NOT create 46 slots to book into. So be it a bus, a hotel, a medical exam room? You don't create booking slots ahead of time, but simply insert bookings with a start + end, and then query against that concept.
So, to find a total on a bus (or say in a exam room), I query to find the total for that day. And if I want to move a group booking of 4 people from one bus to another? Then one FK update to the given bus they are on allows the whole system to "cascade" the existing values in the system. And same goes for moving a person from exam room #1 to #5. You only have to update the FK value of the exam room. If no collisions occur, then this again is a one row update. If you have multiple exam rooms, and multiple slots, then what should be a simple one row update in the database becomes a whole hodge podge of now having to update multiple booking slots with whacks of code.
So you book "use" of resources "into" a "day" a "bus" a room, but it is the act of that booking that consumes the time slots - not that you pre-create records or timeslots for each "range". This thus allows you to leverage the relatonal database model, and reduce huge amounts of code - since you not coding against "slots", but only that a exam room is open from 10 am to 4 pm. That available room for that day is thus ONLY ONE record you create in the system, and then you are now free to book into that one day given room range. The bookings into that one room for the day can be 10 minutes, or 40 minutes - but it ONLY one record being added into the database to achieve this goal (booking).
Regardless of the above, that simple collision query works for any collision (including a whole overlap, inside a existing span, or even the end or start overlaps any booking. And that query is dead simple - and it works for all collisions. So I don't have a library to share, but that simple booking collision finder query can thus drive the whole system based on that kind of simple query.

tour start as an additional planning variable in Optaplanner VRPTW - a good idea?

Hi and happy new year to all Optaplanner users,
we have a requirement to plan tours. These tours contain chained and time-windowed activities (deliveries) executed by a weekly changing number of trucks.
The start time of a single tour can vary and is dependent on several conditions (i.e. the goods to be delivered must be produced, before the tour can start; only a limited number of trucks can be served at the plants gates at the same time; truck must be back before starting a new tour). Means: also the order of tours can vary and time gaps between the tours of a truck can occur.
My design plan is, to annotate TourStartTime as a second planning variable in Optaplanners VRPTW-example and to assign TourStartTime to 2-hours time grains (planning horizon is 1 week and tours normally do not start during night times; so the mentioned time grains reflect a simplified calendar for possible tour starts).
The number of available trucks (from external logistic companies) can vary from week to week. Regarding this I wanted to plan with a 'unlimited' number of trucks. But the number of trucks per logistic company, that can be actually assigned with deliveries, should be controlled by a constraint (i.e. 'trucks_to_be_used_in_parallel').
Can anybody tell me, if this is a feasable design approach, or where do I have to avoid traps (ca. 1000 deliveries/week, 40-80 trucks per day) ?
Thank you
Michael
A second planning variable is possible (and might be even the best design depending on your requirements), but it will blow up the search space and maybe even custom course grained moves might become needed to get great results.
Instead, I 'd first investigate if the Truck's TourStartTime can be made a shadow variable. For example, give all trucks a unique priority number. Then make a Truck's TourStartTime a shadow variable: the soonest time a truck can leave. If there are only 3 lanes and 4 trucks want to leave, the 3 trucks with the highest priority number leave first (so get the original TourStartTime, the 4th truck gets a later one).

Data scheme for a litle football league [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
In one year, there are two seasons: Season Spring and Season Fall
On one season we play on each monday night
On every monday nights we divide ourselves to a two 5 player teams,
randomly selected by picking a red og black card.
In one game, we play untill either team has won by scoring 5 goals.
In one monday night we play somewhere betweeen 2 and 4 games,
depending on how much we score.
Every player in the winning team gets a one point for winning a game,
so on each monday the player can get several point, depending on how
many games he wins.
There are same players in red og black teams the whole night, so you
will get same amount of points like other 4 players if you were in
the same team as them at the same night.
the scoring board holds the name of the player and how many games he
has won over the season.
I have a headache, how can a create a datascheme on sql database that carries this kind of data.
It should witholds a datarow with every game we play on the season and the result of the game.
It should be linked with some other table that holds in what team each and every player was playing with (red or black team)
Should i have one row, that carries one monday night and this row say something like:
Monday - 21.9.2011 - 3 - 2
that would say that that the match was on monday with the date and the result were 3 games won by red team and 2 games won by black.
I´m not asking for a complete solution, just some tips on how i should have the data scheme lined up.
thank you
I would start with the Player table (player_id, player_name, etc...)
I would also have a Weekly_Match_Results table (weekly_match_id, match_date, games_won)
You will put 2 records in here each week, one for Red, one for Black.
Then create the Player_Match_Reference table (player_match_id, player_id, weekly_match_id)
You'll put a record in here each week for every player.
You should then be able to join the 3 tables to get each player's points.
Break it down this way:
First, identify the entities. An entity is a person, place, thing, event etc that is stored in the database. Each entity will typically have attributes. An attribute is a detail about the entity. For example, if you had a table of cars, you may store several attributes about the car (year, make, model, color, miles, condition)
Look at the question and make a list of the entities listed (e.g. player, game etc)
The next step is to think about the relationships between the entities (e.g. many players will belong to many teams. One teams will play many games.) You'll want to read about many to many relationships to best implement that.
The alternative approach is to start with sample data in one big table. Then follow steps to normalize the data. http://databases.about.com/od/specificproducts/a/normalization.htm