Linear Optimization with Categorical Constraints - python-3.x

I am trying to optimize a lineup of players according to the level of competition to play based on maximizing a single statistic(Points in my example below). For a given lineup, 2 players are matched up against a competition level. Here is an example of my DataFrame(This is just an example, my actual DataFrame has a lot more players than this):
Comp Level Points Minutes Played
Player A Elite 10.1 22
Player A Middle -5 22
Player A Low 7.2 22
Player B Elite 0.8 20
Player B Middle 5.6 20
Player B Low 2.2 20
Player C Elite -7.2 18
Player C Middle 3.3 21
Player C Low 6.6 23
Player D Elite -7.2 18
Player D Middle 3.3 21
Player D Low 6.6 23
Player E Elite -7.2 18
Player E Middle 3.3 21
Player E Low 6.6 23
Player F Elite -7.2 18
Player F Middle 3.3 21
Player F Low 6.6 23
The optimized lineup will maximize the total points of the entire lineup (A sum of points from each player). The constraints are:
1. You can't use the same player for more than one competition level (For example, if player A is paired against the elite level, they can't be played against the middle or low levels).
2. Each Lineup Pair has to have a minutes played value that is within 10% of each other (For Example, Player A Could be Paired with Player B at the Elite Level but not Player C).
I'm hoping my output will look something like this:
Line up:
Elite: Player A and B
Middle: Player C and Player D
Low: Player E and Player F
Team Total Points: X
I've started by creating my DataFrame and figured on using PuLP for this optimization. The part where I am struggling with is adding my objective function, and appropriate constraints to the problem.
This is my code thus far:
prob = LpProblem('LineUp Optimization', LpMaximize)
players = list(data['Name'])
# print('The Players under consideration are \n' + '-'*100)
# for x in players:
# print(x, end=',\n ')
costs = dict(zip(players, data['Points']))
comp_levels = dict(zip(players, data['Competition Level']))
Minutes = dict(zip(players, data['Minutes Played']))
ozs = dict(zip(players, data['OZS%']))
player_var = LpVariable.dicts('Players', players, 0, cat='Integer')
Any help/guidance on what my constraints would look like within this PuLP optimization would be greatly appreciated!

If I understood you're problem correctly you need to choose a pair of players for each of the Elite, Middle and lower competition levels.
There are a number of ways you could formulate this problem. The most obvious to me would be to have sets of binary variables which indicate wheter (1) or not (0) each player is chosen for each level of competition:
player_var = LpVariable.dicts('player_var', (players, comp_levels), cat='Binary')
Your objective is to maximise total points of the included players:
prob += lpSum([player_var[p, l]*points[p, l] for p in players for l in comp_levels])
You then need to put in constraints that two players should be chosen for each level:
for l in comp_levels:
prob += lpSum([player_var[p, l] for p in players]) == 2
And that a player can play in at most one competition level:
for p in players:
prob += lpSum([player_var[p, l] for l in comp_levels]) <= 1
And that your 10% rule on minutes played at the chosen level for the pairing is satisfied. This last one is kind of interesting. I'm sure there is a more efficient way of doing this but what I've done below should work... I think.
for l in comp_levels:
for p in players:
prob += minutes[p, l]*player_var[p,l] <= 1.1*lpSum([minutes[p,l]*player_ver[other_p,l] for other_p in players if other_p != p])
This creates a constraints for each competition level, and for each player, however if the player is not chosen to play at the level (player_var[p,l] == 0) then the constraints should have no effect - term on left-hand-side will be 0.
If the player is chosen (player_var[p,l] == 1) then the constaint ensures that the chosen player has no more than 10% more minutes than their oponent - note the right hand sand takes a sum over all other players but only 1 of these will be non-zero.
No check is carried out to ensure the chosen player has at least 90% of the minutes of their oponent - but that case is covered when this constraint is applied to their oponent.
I'm sure someone out there can suggest a much more efficient/elegant way of doing this...

Related

soccer 1X2 odds to Asian Handicap conversion

Can anyone help .
I now know how to convert Home,Draw,Away probabilities to Asian handicap lines usin excel e.g.:
At first let’s take a look at the simpler situations where the handicap is half a goal or zero
goals (=no advatage for either team).
Handicaps marked as 0:0 are identical to moneyline bets (which is a term used in
America for this type of bet). In moneyline you bet on which team will win the game.
And if the game ends with a draw the stakes will (usually) be fully refunded. And
because the result from refunded stake is exactly the same as if the bet was never placed,
the possibility of draw can be excluded from the set.
The true odds (in decimal presentation) for moneyline can be derived from the
probabilities of home win, draw and away win in the following way:
Home Odds = (1 - p0) / p1
Away Odds = (1 - p0) / p2,
where p1 is the probability for home win (a value between 0 and 1), p0 the probability for
draw (0..1) and p2 the probability for away win (0..1).
Numerical example:
If 1X2-probabilities for a game are 45% (= 0.45), 30% (= 0.30) and 25% (=0.25),
moneyline odds would be: 4
Home Odds = (1 - 0.30) / 0.45 = 1.56
Away Odds = (1 - 0.30) / 0.25 = 2.80
Now I'm stuck tryin to do the same for the over and under Asian goal lines.
what I am tryin to achieve is how this website does it
https://www.totalcorner.com/page/fulltime-asian-handicap-calculator
remember I know 1x2 Asian handicap formula and what I'm asking is help with the over/under goal lines.
Thanks in advance

Vehicle Routing Problem where distance constraint will depend on first visited node

I am trying to solve the vehicle routing problem for cabs to pick-up employees and drop them off at the office using OR-Tools.
One of the requirement is that no employee should spend more them 'x' km off the route, i.e. if x = 10 km and employee A lives 10 km away from the office, then the distance traveled by cab after picking up employee A should not be more than 20 km(10km of x + 10km of distance from A to office).
So, if for the best route, the cab needs to pick employee A then B, and then C, but the total distance is turning out to be 25km, then it is not allowed, if it is up to 20 it is fine.
So the max distance allowed for any cab to travel will depend on who the first pickup is. Is there any way to implement such a scenario using OR-tools??
In python, it should something like this:
x_pickup = manager.NodeToIndex(x_index)
x_drop = manager.NodeToIndex(y_index)
routing.solver().Add(
distance_dimension.CumulVar(x_pickup) + 10 <=
distance_dimension.CumulVar(y_drop))

Minimum pumping length of L= {0^i1^j | i >= j} to prove L is non regular

Both Mr. Mithlesh Upadhyay and You play a game:
1. Mr. Mithlesh Upadhyay gives you a constant n.
2. You choose a word w in the language of length at least n.
3. Mr. Mithlesh Upadhyay gives you x, y, and z with xyz = w, |xy|≤n, and y not empty.
4. Now you pick r.
5. Mr. Mithlesh Upadhyay asserts that xyrz is also in the language.
6. If Mr. Mithlesh Upadhyay is wrong, you win.
In case, if you win the game, what is the minimum possible value of
your r for the language {0^i1^j | i >= j} ?
A. 0
B. 2
C. 3
D. Mr. Mithlesh Upadhyay won the game for given language.
Explanation
Given, above game is known as Pumping Lemma for regular languages.
You won the game for the minimum value of r is 0.
Option (A) is correct.
I could not understand the solution. How is it possible to get 0 length min pumping length.If we take
w = 01 and pump 1 , then it does not belong to L.
So, why is 2 not the minimum length ?
According to point number 5 and 6, if you are the winner, it means that Mr. Mithlesh Upadhyay was wrong and xyrz does not belong to L. In your example you have shown that 011 does not belong to L. If you are going to make the minimum possible length of r to be 2, then your example will be wrong. Because you have taken r to be of length 1. Correct me if I'm wrong.

Nesting Excel Score

For a project we are making an Excel file for the WK in Russia.
So I need the following things (if its possible only nesting):
TOTO: the home team wins = 1, away team wins = 2, its equal = 3
This is what I have right now for these:
IF(F5<H5;"2";IF(F5>H5;"1";IF(F5;"3";IF(H5;"3";""))))
This works but if I set the score 0-0 then I get 0 back in stead of 3.
Then the next one:
If they gambled that the home team score is correct they get 2 points,
If they gambled that the away team score is correct they get 2 points
If they gambled that the TOTO is correct: 5 points
If all is correct: 1 bonus point
What i mean with the second thing is if somebody says that the score is 2-1 and the game ends on 0-1 then i get 2 points (one for the TOTO and 1 point for the team that scored 0-1).
E.g. Belgium-Tunis (player1) on 2-1 with TOTO = 1. Game ends on 0-1. Player1 gets 2 points in total because he predict the goals of Tunis correct.
Last but not least:
IF the teams are going to the quarter finals,... are coming a few lines on it:
Country good: 10 points
European champ correct: 25 points
Total yellow: 20 points
TOtal red: 20 points.
I believe the formula you want for the first of your questions (the home team wins = 1, away team wins = 2, its equal = 3) is:
=IF(F5<H5,"2",IF(F5>H5,"1",IF(F5=H5,"3","")))
Not sure why you have the numbers in speech marks (have left as is in the above) you could have:
=IF(F5<H5,2,IF(F5>H5,1,IF(F5=H5,3,"")))
Which would probably be easier if you want to do any maths with the values.
It's not entirely clear what you're asking with the rest of the question(s).

Highscores in MongoDB

I've made a small HTML5 game with a highscores feature. At the end of every game, my node server stores name, score and userId (a number unique for each player) in a Mongo database, and then orders it by score and sends back the top 3.
So far it works, but I want to find a way so that each player can appear on the highscore table only once.
So if the scores would be:
Player A, 100
Player A, 99
Player A, 98
Player B, 97
Player B, 91
Player C, 96
then I would like to display:
Player A, 100
Player B, 97
Player C , 96
Is there a way to do this in Mongo? It's the first time I'm using it, and don't even now what this function would be called...

Resources