Add Solver Constraint For Non-Mutually Exclusive Ranges - python

I use or-tools to optimize my fantasy baseball team. My setup very much resembles the program described here. The only difference in my particular case is that players can actually be eligible for a number of different positions. So, I end up with 1 player in a list for a specific position type, and the same player in another list for another position type. I am trying to avoid having the solver select the same player for multiple positions (which wouldn't be realistic).
Is there any way to modify the aforementioned program to constrain the use of a player to a single position even while they are technically eligible for many? Please let me know if I can clarify any further & thanks for your input.

Related

Filtering sequences of events in django

My django app stores the actions that players do during a game. One of the models is called Event, and it contains a list of all actions by players. It has the following 4 columns: game_id, player_name, action, turn. Turn is the number of the turn in which the action takes place.
Now I want to count how often players behave in certain patterns. For example, I want to know how often a player takes decision A in turn 2 and that same player takes decision B in turn 3. I'm breaking my head over how I can do this. Can someone help?
Note: Ideally the queries should be efficient, bearing in mind that some related queries are following. For example, after the above query I would want to know how often a player takes decision C in turn 4, after doing A and B in turns 2 and 3. (The goal is to predict the likelihood of each action, given the actions in the past.)

How do I create a Leaderboard using a text file in python?

I'm working on a quiz for my exam in computer science. I'm relatively new to the program, in the sense that I know all of the basics, but I am on the point where I want to expand my knowledge. One way I want to do this is by adding a Leaderboard system. The user gets a number of points, and then the program checks in a text file that has other high scores in it, and adds the user to it. It then prints out the leaderboard. This means that I'm going to have to use some sort of operations to determine whether the user's score is higher or lower than another score in the file, and then delete the score it is higher than and replace it. Any idea on how to do this? I'm completely stuck.
Try Pseudeocode and work through steps.
Get Score
Compare Score
Add Score
You have to think like a computer and break all the way down. At each step think about how do I tell the computer to do that. Once you have all that look at what you have done and remember DRY -> Don't Repeat Yourself. Your coding will go much faster.

Create balanced tennis rounds

We are a group of 20 people and we like to go play 2 vs 2 tennis matches. Each of us plays one match each round and we do 5 rounds in total, so everyone plays 5 matches. Matches have two restrictions:
Everyone has a different level (from 1 to 5), so the matches must be balanced: two players with levels 5 and 5 shoulnd't be matched with two levels 1. So between the two teams, the difference in level must be lower or equal to 1.5.
Ej.: level 1.5 and level 2 vs level 2 and level 2.5. The difference in level between teams is 1 so the match is accepted.
If two players play together in one match, they must not play toghether again in the following rounds.
I managed to create a python script that does the specified above, but it takes about 20 minutes to finish depending on the level of the people :/. What I do is shuffle the list with every one in it, break it into 5 lists of 4 people, check if conditions are satisfied and repeat for every round.
I tried modeling the problem to solve it with linear programming (LP) but I don't know which is my function to optimize to begin with... Any ideas on how to do this with or without LP?
Thanks in advance!
You could use a dummy objective or even try to minimize the max of the difference in levels.
My MIP model is not completely trivial, but it solves quite fast (about a second or so using a commercial solver).
The results look ok at first sight:
I assumed two players cannot be in the same team more than once. I.e. not just in the same game. That is in my case you can play against another player more than once.
A more complex example can be found here.

Python: How To Construct A Class With Many Parameters

I am in a bit of a jam in deciding how to structure my class. What I have is a baseball player class and necessary attributes of:
Player ID (a key from a DB)
Last name
First name
Team
Position
Opponent
about 10 or 11 stats (historical)
about 10 or 11 stats (projected)
pitcher matchup
weather
... and a few more
Some things to break this down a little:
1) put stats in dictionaries
2) make a team class that can hold general info common for all players on the team like weather and pitcher match up.
But, I still have 10 attributes after this.
Seeing this (Class with too many parameters: better design strategy?) has given me a couple ideas but don't know if they're ideal.
1) Use a dictionary - But then wouldn't be able to use methods to calculate stats (or would have to use separate functions)
2) Use args/kwargs - But from what I can gather, those seem to be for variable amounts of parameters, and all of my parameters will be required.
3) Breaking up into smaller classes - I have broken it up a bit already, but don't know if I can any further.
Is there a better way to build this rather than having a class with a bunch of parameters listed out?
If you think about this from the perspective of database design, it would probably be odd to have a BaseballPlayer object that has the following parameters:
Team
Position
Opponent
about 10 or 11 stats (historical)
about 10 or 11 stats (projected)
pitcher matchup
weather
Because there are certain things associated with a particular BaseballPlayer which remain relatively fixed, such as name, etc., but these other things are fluid and transitory.
If you were designing this as an application with various database tables, then, it's possible that each of the things listed here would represent a separate table, and the BaseballPlayer's relationship with these other tables amount to current and former Team, etc.
Thus, I would probably break up the problem into more classes, including a StatsClass and a Team class (which is probably what an Opponent really is...).
But it all depends what you would like to do. Usually when you are bending over backwards to cram data into a structure or doing the same to get it back out, the design could be reworked to make your job easier.

Calculating a game's high score table

I need to create a function/method ( in python) which calculates a high score "leaderboard". Each player will have played any number of rounds of the game, recieving a score for each round. I want to know what's the best way to sort the top ranking players (accounting for score AND number of rounds played). The possible scores for each round are F, D-, D, D+, C-, C, C+, B-, B, B+, A-, and A.
Obviously a simple average won't work because it doesn't take into account number of rounds played. Whats the best way to set up a fair sorting function?
EDIT: I've been reading some of the really great answers here and I want to try to clear up my question a bit. I want both the players score AND the number of rounds they've played to count towards their ranking in a way that's fair. Meaning a player with 20 B's should be of a higher rank than a player with 5 A's. Basically the high score should reflect general effort and skill, "the number of rounds played PLUS their score" means the higher their ranking should be.
EDIT 2: After reading the answers, I think the best way to do it is a simple total sum of the players points across all rounds. I'm not sure which answer to assign the green check to because you were all correct.
There are many ways that you could do this. Try this for example, let F-A be 0-11 (you can make your own; however try to take difficulty into account), so each score is one higher than the previous. For every game you play, you receive a score (from 0-11). Create a total score and add the game score every time to the total score. That way, if a person receives 7 A's, that's 77, while a person that receives 7 A-'s gets a score of 70, then simply sort them accordingly. Each function has its drawbacks of course. This function is not the "best", consider getting 20 B's would exceed 7 A's even though, 7 A's is a much better score. if you can give me more details about how you want to rank them, then it will be much easier to get the algorithm down.
What you are asking is essentially how we define "good" players and it's not an easy problem. As you mentioned, a simple average score or picking-the-highest-score will not be an ideal answer depending on your game design.
I'd like to recommend that you read about ELO rating system for Chess and other modified versions of it before you design your own player rating system.
One simple and possible way is you can set a window (like 10 most recent games) and use average score from the window. Players who play less games than this window would be "in placement" state. Again, it's not an easy problem and heavily depends on what your game is. Good Luck!
[UPDATE]
I assumed that your game is player vs. player. If not, this is another story. Most games just keep the highest score no matter how many times you play the game and that's going to be your entry in the leaderboard. Since you don't say anything about your game, I have no idea why it wouldn't be fair. As I mentioned earlier, you could set a window for avg. score or the highest score. You can even reset your leaderboard every month or remove players who haven't played for a week. It all depends on your game and what you want. Please remember that no matter what you do, make it sure that the rules are crystal clear for players otherwise they would be easily upset and frustrated.

Categories

Resources