Is there an efficient algorithm to create this type of schedule? [closed] - r

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am creating a schedule for a sports league with several dozen teams. I already have all of the games in a set order and now I just need to assign one team to be the "home" team and one to be "away" for each game.
The problem has two constraints:
Each pair of teams must play an equal number of home and away
games against each other. For example, if team A and team B play 4
games, then 2 must be hosted by A and 2 by B. Assume that each pair
of teams plays an even number of games against each other.
No team should have more than three consecutive home games or three
consecutive away games at any point in the schedule.
I have been trying to use brute force in R to solve this problem but I can't get any of my code blocks to solve the issue in a timely fashion. Does anyone have any advice on how to deal with either (or both) of the above constraints algorithmically?

You need to do more research on simple scheduling.
There are a lot of references on line for these things.
Here are the basics for your application. Let's assume a league of 6 teams; the process is the same for any number.
Match 1: Simply write down the team numbers in order, in pairs, in a ring. Flatten he ring into two lines. Matches are upper (home) and lower(away).
1 2 3
6 5 4
Matches 2-5: Team 1 stays in place; the others rotate around the ring.
1 6 2
5 4 3
1 5 6
4 3 2
1 4 5
3 2 6
1 3 4
2 6 5
That's one full cycle. To balance the home-away schedule, simply invert the fixtures every other match:
1 2 3 5 4 3 1 5 6 3 2 6 1 3 4
6 5 4 1 6 2 4 3 2 1 4 5 2 6 5
There's your first full round. Simply replicate this, again switching home-away fixtures in alternate rounds. Thus, the second round would be:
6 5 4 1 6 2 4 3 2 1 4 5 2 6 5
1 2 3 5 4 3 1 5 6 3 2 6 1 3 4
Repeat this pair of rounds as many times as needed to get the length of schedule you need.
If you have an odd quantity of teams, simply declare one of the numbers to be the "bye" in the schedule. I find it easiest to follow if I use the non-rotating team -- team 1 in this example.
Note that this home-switching process guarantees that no team has three consecutive matches either home or away: they get two in a row when rounding the end of the row. However, even the two-in-a-row doesn't suffer at the end of the round: both of those teams break the streak in the first match of the next round.
Unfortunately, for an arbitrary existing schedule, you are stuck with a brute-force search with backtracking. You can employ some limits and heuristics, such as balancing partial home-away fixtures as the first option at each juncture. Still, the better approach is to make your original schedule correct by design.
There's also a slight problem that you cannot guarantee that your existing schedule will fulfill the given requirements. For instance, given the 8-team fixtures in this order:
1 2 3 4
5 6 7 8
1 2 5 6
3 4 7 8
1 3 5 7
2 4 6 8
It is not possible to avoid having at least two teams playing three consecutive home or away matches.

Related

Hierarchy chart of 250+ shops and 35,000 employees

are there any tips for make org charts for a 35,000 member organization?
I've attached an org chart for a single shop.
Scenario: We have 250+ shops. Each shop is made up of multiple sections. Each section has a unique section name. Each section is made up of a different amount of managers, technicians, and supervisors. Each shop can be considered a child that reports to a parent. Each parent not only has that particular child shop, but also can have multiple other shops under them as well. That parent can also be a child to a different shop, which is making group_by a challenge. A is a child to parent B, but B is also a child to parent C, who is also a child to parent D, for example.
source doc is an excel doc with 35,000 rows and 50+ columns. Each shop is identified by a shop code and each shop code reports to a parent with it's own shop code.
group_by(parent id, child id might not work because a parent to one shop can be a child to a different parent.
Unit ID Reports To Unit name managers in unit supervisors in unit technicians in unit
10 11 i 2 0 4
9 11 h 2 1 0
8 9 g 4 3 2
6 7 f 2 3 4
5 7 e 1 2 3
4 5 d 2 1 0
3 4 c 4 3 2
2 4 b 2 3 4
1 2 a 1 2 3
You are looking for BALKAN OrgChartJS, it has the functionalities you are asking for:
Code demo with chart and 100k nodes (rows)
Code demo for Import from CSV file and other formats
Also you can read directly from the Excel(CSV) file with http request and load the chart, without any server side code
Disclaimer: I'm a developer in BALKAN App

Get rate of change from messy data

I have a database that looks like this:
Id session q1 q2 q3 ...
1 1 4 5 5
1 2 4 5 6
1 3 5 5 6
2 1 4 4 5
2 2 5 4 5
2 3 5 5 6
Basically, different subjects with 3 different measurements of the same questions. What I want to do is measure the rate of change, and check if every observation improved over time or if there where observations who got worse results in session 3 than in session 1 or 2.
The only thing I have manged to do is get it a bit more tidy with pivot_wider like this:
pivot_wider(id_cols = Id, names_from = session, values_from = c(q1:q4))
The problem is that I have more than 70 questions, and I havenĀ“t figured out a way to automate this instead of doing hundreds of line with mutate in the form of:
mutate(q1change = q1_3 - q1_1)
I was wondering if anyone could come up with a better and simpler solution so I can check this rate of change for each variable.
Ideally I would also like to plot it after I have gotten the rate of change value, so I can show graphically if there where observations that gotten worse.
Thanks

How do I change the order of multiple grouped values in a row dependent on another variable in that row in R?

I need some help conditionally sorting/switching data based on a factor variable.
I'm not sure if it's a typical use case I just can't formulate properly enough for a search engine to show me a solution or if it is that niche but I haven't found anything yet.
I currently have a dataframe like this:
id group a1 a2 a3 a4 b1 b2 b3 b4
1 1 2 6 6 3 4 4 6 4
2 2 5 2 2 2 2 5 2 3
3 1 6 3 3 1 3 6 4 1
4 1 4 8 4 2 7 8 8 9
5 2 3 1 1 4 2 1 1 7
For context this is from a psychological experiment where people went through two variations of a task and the order of those conditions was determined by the experimental group they were assigned to. The columns represent different measurements from different trials and are currently grouped together for the same variable and in chronological order, meaning a1,a2,a3,a4 are essentially the same variable at consecutive time points, same with b1,b2,b3,b4.
I want to split them up for the different conditions so regardless of which group (=which order of tasks) someone went through, data from one condition should come first in the dataframe and columns should still be grouped together for the same variables and in chronological order within that condition. It should essentially look like this:
id group c1a1 c1a2 c2a1 c2a2 c1b1 c1b2 c2b1 c2b2
1 1 2 6 6 3 4 4 6 4
2 2 2 2 5 2 2 3 2 5
3 1 6 3 3 1 3 6 4 1
4 1 4 8 4 2 7 8 8 9
5 2 1 4 3 1 1 7 2 1
So essentially for group 1 everything stays the same since they happened to go through the conditions in the same order that I want to have in the new dataframe while for group 2 values are being switched where the originally second half of values for each variable is put in front of the originally first one.
I hope I formulated the problem in a way, people can understand it.
My real dataset is a bit more complicated it has 180 columns minus id and group so 178.
I have 13 variables some of which were measured over two conditions with 5 trials for each of those and some which have those 5 trials for each of the 2 main condition but which also have 2 adittional measurements for each condition where the order was determined by the same group variable.
(We essentially asked participants to do the task again in two certain ways, which allowed us to see if they were capable of doing them like that if they wanted to under the circumstences of both main conditions).
So there are an adittional 4 columns for some variables which need to be treated seperately. It should look like this when transformed (x and y are the 2 extra tasks where only b was measured once):
id group c1a1 c1a2 c2a1 c2a2 c1b1 c1b2 c1bx c1by c2b1 c2b2 c2bx c2by
1 1 2 6 6 3 4 4 3 7 6 4 4 2
2 2 2 2 5 2 2 3 4 3 2 5 2 2
3 1 6 3 3 1 3 6 2 2 4 1 1 1
4 1 4 8 4 2 7 8 1 1 8 9 5 8
5 2 1 4 3 1 1 7 8 9 2 1 3 4
What I want to say with this is, I need a pretty general solution.
I already tried formulating a function for creation of two seperate datasets for the groups and then merging them by id but got stuck with the automatic creation and naming of columns which I can't seem to wrap my head around. dplyr is currently loaded and used for some other transformations but since I'm not really good with it, I need to ask for your help regarding a solution with or without it. I'm still pretty new to R and this is for my bachelor thesis.
Thanks in advance!
Your question leaves a few things unclear that make this hard to answer, but here is maybe a start that could help, or at least help clarify your problem.
It would really help if you could clarify 2 pieces of info, what types of column rearrangements you need, and how you distinguish what indicates that a row needs to have this transformation.
I'm also wondering if instead of trying to manipulate your data in its current shape, if it not might be more practical to figure out how to change the shape of your data to better represent your data, perhaps using something like pivot_longer(), I don't know how this data will ultimately be used or what the actual values indicate, but it doesn't seem to be very tidy in its current form, and instead having a "longer" table might be more meaningful, but I'll still provide what I think is a solution to your listed problem.
This creates some example data that looks like it reflects yours in the example table.
ID=seq(1:10)
group=sample(1:2,10,replace=T)
Data=matrix(sample(1:10,80,replace=T),nrow=10,ncol=8)
DataFrame=data.frame('ID'=ID,'Group'=group,Data)
You then define the groups of columns that need to be kept together. I can't tell if there is an automated way for you to indicate which columns are grouped, but this might get bulky if done manually. Some more information on what your column names actually are, and how they are distributed in groups would help.
ColumnGroups=list('One'=c('X1','X2'),'Two'=c('X3','X4'),'Three'=c('X5','X6'),'Four'=c('X7','X8'))
You can then figure out which rows need to have rearranged done by using some conditional. Based on your example, I'm assuming when the group variable equals 2, then the rearranging needs to be done, which is what I've used here.
FlipRows=DataFrame$Group==2
You can then have R only apply the rearrangement needed to those rows that need it, and define the rearrangement based on the ordering of the different column groups. I know you ask for a general solution, but is hard to identify the general solution you need without knowing what types of column rearrangements you need. If it is always flipping two sets of consecutive column groups, that would be easier to define without having to type it all out. What I have done here would require you to manually type out the order of the different column groups that you would like the rows to be rearranged as. The SortedDataFrame object seems to be what you are looking for, but might not actually reflect your real data. I removed columns 1 and 2 in this operation since those are ID and group which you don't want overridden.
SortedDataFrame=DataFrame
SortedDataFrame[FlipRows,-c(1,2)]=DataFrame[FlipRows,c(ColumnGroups$Two,ColumnGroups$One,ColumnGroups$Four,ColumnGroups$Three)]
This solution won't work if you need to rearrange each row differently, but it is unclear if that is the case. Try to provide any of the other info requested here, and let me know where this solution doesn't work for you, and that.

Sum variables conditionally with loop in r

I realize this is a topic that's covered somewhat well but I couldn't find anything that approaches this specific concern:
I have a df with 800 columns, 10 iterations of 80 columns (each column represents an item) - Each column is named something like: 1_BL_PRE.1 1_FU_PRE.1 1_BL_PRE.1 1_BL_POST.1
Where the first '1' indicates the item number and the second '1' indicates the iteration number.
What I'm trying to figure out is how to get the sums of specific groups of items from all 10 iterations.
As a short example let's say I want to take the 1st and 3rd item of BL_PRE and get the sum of all 10 iterations for those 2 items - how would I do this?
subject 1_BL_PRE.1 2_BL_PRE.1 3_BL_PRE.1 1_BL_PRE.2 2_BL_PRE.2
1 40002 3 4 3 1 2
2 40004 1 2 3 4 4
3 40006 4 3 3 3 1
4 40008 2 3 1 2 3
5 40009 3 4 1 2 3
Expected output (where A represents the sum of 1_BL_PRE.1, 3_BL_PRE.1, 1_BL_PRE.2 and so on):
subject BL_PRE_A
1 40002 12
2 40004 14
3 40006 15
4 40008 20
5 40009 12
My hunch is the solution is related to a for-loop or lappy (and I'm not familiar at all with either). I'm trying to work with apply(finaldata,1,function(x) {sum(x ...)}) but I haven't been able to figure out the conditional statement for the function of sum.
If there's an implementation with plyr I'd be really curious to see what that looks like. (and if there's a thread that answers this, apologies and just re-direct!)
**Edited to include small example + code I'm trying to get to work
Thanks!

Simple formula to ensure two teams get mixed up?

Say I have a number of users who are evenly split between two teams (if there is an odd count then one team will have one extra player).
I want to make sure everyone gets to be on the same team as everyone else over the course of 3 games with team changes after each game.
What's an easy mechanism for doing this for any number of players?
If it makes it easier for the purpose of explanation, I can give each player a number from 1 to N (where N is the number of players).
TIA
Let's assume we have 6 players, 1 - 6. You want to create different teams for 3 rounds of play.
For the first round, you deal the players.
1 2
3 4
5 6
For the second round, you put the winning team first, then the losing team. Let's assume that the team with player 1 won. Then the list of players would look like this.
1 3 5 2 4 6
And you would deal them like this.
1 3
5 2
4 6
For the third round, you do the same as the second round. This time, let's assume the team with player 3 won.
3 2 6 1 5 4
And you would deal them like this.
3 2
6 1
5 4
With only 3 rounds and many more than 6 players, everybody isn't going to be able to play everybody. But this shuffling algorithm gives a good mixture and is relatively simple to implement.

Resources