I'm trying to make a minesweeper game through text in python. This error comes up when I try to draw the little numbers. Maybe the way I'm doing this is inefficient, but I don't understand why it throws this error. I've been trying to fiddle with the code, but nothing seems to work. Does anyone have any idea for why it's not working?
import random
minenum = 12
gridsize = 10
grid = [[0] * gridsize for _ in range(gridsize)]
def setup():
global grid
global minecount
for i in range(minenum):
x = random.randrange(0,10)
y = random.randrange(0,10)
grid[y][x] = "m"
xpos = 0
ypos = 0
for n in range(10):
for z in range(10):
count = 0
try:
if grid[ypos + 1][xpos] == "m":
count += 1
except:
pass
try:
if grid[ypos + 1][xpos + 1] == "m":
count += 1
except:
pass
try:
if grid[ypos + 1][xpos - 1] == "m":
count += 1
except:
pass
try:
if grid[ypos - 1][xpos + 1] == "m":
count += 1
except:
pass
try:
if grid[ypos - 1][xpos - 1] == "m":
count += 1
except:
pass
try:
if grid[ypos - 1][xpos] == "m":
count += 1
except:
pass
try:
if grid[ypos][xpos + 1] == "m":
count += 1
except:
pass
try:
if grid[ypos][xpos - 1] == "m":
count += 1
except:
pass
grid[ypos][xpos] = count
xpos += 1
ypos += 1
def printBoard():
for i in range(10):
print(' '.join(str(v) for v in grid[i]))
setup()
printBoard()
[Edit]
here is the error:
Traceback (most recent call last):
File "main.py", line 74, in <module>
setup()
File "main.py", line 63, in setup
grid[ypos][xpos] = count
IndexError: list assignment index out of range
If you add a print(count) before the grid[ypos][xpos] = count you will see that you have 11 instances of count but grid is only 10 so that's why.
You add to the ypos and xpos even when it's at max, a quick fix below but can be better:
print(count)
grid[ypos][xpos] = count
if xpos < gridsize - 1:
xpos += 1
if ypos < gridsize - 1:
ypos += 1
Your code was not working because you never reset xpos when you incremented your ypos so your indexes looked like this (for gridsize = 4):
0 0
1 0
2 0
3 0
4 1
5 1
6 1
7 1
8 2
Instead of what you inteded, i.e.
0 0
1 0
2 0
3 0
0 1
1 1
2 1
3 1
0 2
You should have added xpos = 0 whenever you do ypos += 1
xpos += 1
ypos += 1
xpos = 0
Your code could also use a bit of cleanup:
import random
def setup(minecount, gridsize):
grid = [[0] * gridsize for _ in range(gridsize)]
for _ in range(minecount):
x = random.randrange(0,gridsize)
y = random.randrange(0,gridsize)
grid[y][x] = "m"
for xpos in range(gridsize):
for ypos in range(gridsize):
count = 0
if ypos + 1 < 10 and grid[ypos + 1][xpos] == "m":
count += 1
if ypos + 1 < 10 and xpos +1 < 10 and grid[ypos + 1][xpos + 1] == "m":
count += 1
if ypos + 1 < 10 and xpos - 1 >= 0 and grid[ypos + 1][xpos - 1] == "m":
count += 1
if ypos - 1 >= 0 and xpos + 1 < 10 and grid[ypos - 1][xpos + 1] == "m":
count += 1
if ypos - 1 >= 0 and xpos -1 >= 10 and grid[ypos - 1][xpos - 1] == "m":
count += 1
if ypos - 1 >= 0 and grid[ypos - 1][xpos] == "m":
count += 1
if xpos + 1 < 10 and grid[ypos][xpos + 1] == "m":
count += 1
if xpos - 1 >= 0 and grid[ypos][xpos - 1] == "m":
count += 1
grid[ypos][xpos] = count
return grid
def printBoard(grid):
for i in range(10):
print(' '.join(str(v) for v in grid[i]))
minecount = 12
gridsize = 10
grid = setup(minecount, gridsize)
printBoard(grid)
Didn't even have to change the logic, just switched magic numbers to proper parameters. You are also overwriting all your "m" cells when counting neighbouring bombs, you might want to avoid that.
You have to assign zero to xpos in the end of setup() function:
ypos += 1
xpos = 0
So I have a 2d list, named tour:
tour = [ [3, 2, 1,],
[4, 7, 9,],
[5, 6, 8,], ]
I want my code to check through this list and see if a king (chess) can go through it normally.
The code I made can be applicable on a N×N 2d list. What it does is that it starts on number 1, and checks all 9 spaces around it if there is a number 2, repeating the process till N×N. The problem is that when the number is at a side of the list the code gives an IndexError, to overcome this I made a try: except: IndexError but now when it gets to i = 6 it keeps on going infinitely
def in_list(number, listname):
"""
this function detects where a certain number is in a 2d list and returns the index of
it
"""
for i, sublist in enumerate(listname):
if (number in sublist):
return [i, sublist.index(number)]
return -1
def is_king_tour_3by3(tour):
"""
this function checks for all 9 areas around this number and if it's an IndexError
i.e. it's at the end of the list it ignores that case
"""
start = in_list(1, tour)
i = 2
counter = len(tour) * len(tour)
while True:
counter -= 1
print(i)
print(start)
try:
if(tour[start[0]][start[1] - 1] == i): #starting from checking the number on the left and going counter-clockwise
i += 1
start[1] -= 1
elif(tour[start[0] + 1][start[1] - 1] == i):
i += 1
start[0] += 1
start[1] -= 1
elif(tour[start[0] + 1][start[1]] == i):
i += 1
start[0] += 1
elif(tour[start[0] + 1][start[1] + 1] == i):
i += 1
start[0] += 1
start[1] += 1
elif(tour[start[0]][start[1] + 1] == i):
i += 1
start[1] += 1
elif(tour[start[0] - 1][start[1] + 1] == i):
i += 1
start[0] -= 1
start[1] += 1
elif(tour[start[0] - 1][start[1]] == i):
i += 1
start[0] -= 1
elif(tour[start[0] - 1][start[1] - 1] == i):
i += 1
start[0] -= 1
start[1] -= 1
except IndexError:
pass
if(i == (len(tour) * len(tour))):
return True
break
elif(counter == 0):
return False
break
By adding start[0] < len(tour)-1 and start[1] > 0 to the rows you can check that you are not outside the 2d list instead of using a try: except:. This code will solve it as long as start is > 0 and < len(tour) (if start[0] or start[1] is -1 or len(tour) it might fail):
def in_list(number, listname):
"""
this function detects where a certain number is in a 2d list and returns the index of
it
"""
for i, sublist in enumerate(listname):
if (number in sublist):
return [i, sublist.index(number)]
return -1
def is_king_tour_3by3(tour):
"""
this function checks for all 9 areas around this number and if it's an IndexError
i.e. it's at the end of the list it ignores that case
"""
start = in_list(1, tour)
i = 2
counter = len(tour) * len(tour)
while True:
counter -= 1
print(i)
print(start)
if(start[1] > 0 and tour[start[0]][start[1] - 1] == i): #starting from checking the number on the left and going counter-clockwise
i += 1
start[1] -= 1
elif(start[0] < len(tour)-1 and start[1] > 0 and tour[start[0] + 1][start[1] - 1] == i):
i += 1
start[0] += 1
start[1] -= 1
elif(start[0] < len(tour)-1 and tour[start[0] + 1][start[1]] == i):
i += 1
start[0] += 1
elif(start[0] < len(tour)-1 and start[1] < len(tour)-1 and tour[start[0] + 1][start[1] + 1] == i):
i += 1
start[0] += 1
start[1] += 1
elif(start[1] < len(tour)-1 and tour[start[0]][start[1] + 1] == i):
i += 1
start[1] += 1
elif(start[0] > 0 and start[1] < len(tour)-1 and tour[start[0] - 1][start[1] + 1] == i):
i += 1
start[0] -= 1
start[1] += 1
elif(start[0] > 0 and tour[start[0] - 1][start[1]] == i):
i += 1
start[0] -= 1
elif(start[0] > 0 and start[1] > 0 and tour[start[0] - 1][start[1] - 1] == i):
i += 1
start[0] -= 1
start[1] -= 1
if(i == (len(tour) * len(tour))):
return True
break
elif(counter == 0):
return False
break
tour = [ [3, 2, 1,],
[4, 7, 9,],
[5, 6, 8,], ]
is_king_tour_3by3(tour)
I am new to Pulp and therefore have been encountering a problem when trying to make a conditional constraint. I have made a fantasy football optimizer that picks the optimal selection of 9 players, my solver fully works currently with position constraints, salary constraints, and more.
The last thing I need to add is a constraint that makes it so out of the 9 players it picks, there need to be 8 unique team names of the players. For example: there is a Quarterback and a WR/TE going to be on the same team given this constraint in my code ###Stack QB with 2 teammates. and therefore everyone else should be on a different team than each other to have 8 unique team names.
Below is the the code i have tried to use to make this constraint, the head of the excel file being optimized and my code that works so far without the constraint I want to add of 8 unique team names in the 9 players selected.
I have currently tried this but it doesn't work! Would really appreciate any help!
list_of_teams = raw_data['Team'].unique()
team_vars = pulp.LpVariable.dicts('team', list_of_teams, cat = 'Binary')
for team in list_of_teams:
prob += pulp.lpSum([player_vars[i] for i in player_ids if raw_data['Team'][i] == team] + [-9*team_vars[team]]) <= 0
prob += pulp.lpSum([team_vars[t] for t in list_of_teams]) >= 8
file_name = 'C:/Users/Michael Arena/Desktop/Football/Simulation.csv'
raw_data = pd.read_csv(file_name,engine="python",index_col=False, header=0, delimiter=",", quoting = 3)
player_ids = raw_data.index
player_vars = pulp.LpVariable.dicts('player', player_ids, cat='Binary')
prob = pulp.LpProblem("DFS Optimizer", pulp.LpMaximize)
prob += pulp.lpSum([raw_data['Projection'][i]*player_vars[i] for i in player_ids])
##Total Salary upper:
prob += pulp.lpSum([raw_data['Salary'][i]*player_vars[i] for i in player_ids]) <= 50000
##Total Salary lower:
prob += pulp.lpSum([raw_data['Salary'][i]*player_vars[i] for i in player_ids]) >= 49900
##Exactly 9 players:
prob += pulp.lpSum([player_vars[i] for i in player_ids]) == 9
##2-3 RBs:
prob += pulp.lpSum([player_vars[i] for i in player_ids if raw_data['Position'][i] == 'RB']) >= 2
prob += pulp.lpSum([player_vars[i] for i in player_ids if raw_data['Position'][i] == 'RB']) <= 3
##1 QB:
prob += pulp.lpSum([player_vars[i] for i in player_ids if raw_data['Position'][i] == 'QB']) == 1
##3-4 WRs:
prob += pulp.lpSum([player_vars[i] for i in player_ids if raw_data['Position'][i] == 'WR']) >= 3
prob += pulp.lpSum([player_vars[i] for i in player_ids if raw_data['Position'][i] == 'WR']) <= 4
##1-2 TE's:
prob += pulp.lpSum([player_vars[i] for i in player_ids if raw_data['Position'][i] == 'TE']) >= 1
# prob += pulp.lpSum([player_vars[i] for i in player_ids if raw_data['Position'][i] == 'TE']) <= 2
##1 DST:
prob += pulp.lpSum([player_vars[i] for i in player_ids if raw_data['Position'][i] == 'DST']) == 1
###Stack QB with 2 teammates
for qbid in player_ids:
if raw_data['Position'][qbid] == 'QB':
prob += pulp.lpSum([player_vars[i] for i in player_ids if
(raw_data['Team'][i] == raw_data['Team'][qbid] and
raw_data['Position'][i] in ('WR', 'TE'))] +
[-1*player_vars[qbid]]) >= 0
###Don't stack with opposing DST:
for dstid in player_ids:
if raw_data['Position'][dstid] == 'DST':
prob += pulp.lpSum([player_vars[i] for i in player_ids if
raw_data['Team'][i] == raw_data['Opponent'][dstid]] +
[8*player_vars[dstid]]) <= 8
###Stack QB with 1 opposing player:
for qbid in player_ids:
if raw_data['Position'][qbid] == 'QB':
prob += pulp.lpSum([player_vars[i] for i in player_ids if
(raw_data['Team'][i] == raw_data['Opponent'][qbid] and
raw_data['Position'][i] in ('WR', 'TE'))]+
[-1*player_vars[qbid]]) >= 0
prob.solve()
In Linear Programming terms
Let x_i = 1 if the i^th player is chosen, and 0 otherwise, i = 1....I.
Let t_i be the team of the i^th player, which is a constant.
Let t_j be the j^th unique team, also a constant, j = 1....T.
And let t_{ij} = 1 if t_i == t_j, and 0 otherwise. This is also a constant.
Then you can say that the total number of players selected from team t_j is (t_{1j}*x_1 + t_{1j}*x_2 + ... + t_{Ij}*x_I), which takes a value between 0 and I, logically.
Now, you can let the binary variable y_j = 1 if any selected players come from team t_j, and 0 otherwise, like this:
(t_{1j}*x_1 + t_{1j}*x_2 + ... + t_{Ij}*x_I) >= y_j
This gives you the following situation:
If (t_{1j}*x_1 + t_{1j}*x_2 + ... + t_{Ij}*x_I) = 0, then y_j is 0;
If (t_{1j}*x_1 + t_{1j}*x_2 + ... + t_{Ij}*x_I) > 0, then y_j can be 0 or 1.
And now, if you add a constraint (y_1 + y_2 + ... + y_T) >= 8, that implies that (t_{1j}*x_1 + t_{1j}*x_2 + ... + t_{Ij}*x_I) > 0 for at least 8 different teams t_j.
In PULP terms (something like this, wasn't able tot test it)
If player_vars is a binary variable equivalent to x_i
teams = raw_data['Team'] # t_i
unique_teams = teams.unique() # t_j
player_in_team = teams.str.get_dummies() # t_{ij}
# Example output for `teams = pd.Series(['A', 'B', 'C', 'D', 'E', 'F', 'A', 'C', 'E'])`:
# A B C D E F
# 0 1 0 0 0 0 0
# 1 0 1 0 0 0 0
# 2 0 0 1 0 0 0
# 3 0 0 0 1 0 0
# 4 0 0 0 0 1 0
# 5 0 0 0 0 0 1
# 6 1 0 0 0 0 0
# 7 0 0 1 0 0 0
# 8 0 0 0 0 1 0
team_vars = pulp.LpVariable.dicts('team', unique_teams, cat='Binary') # y_j
for team in unique_teams:
prob += pulp.lpSum(
[player_in_team[team][i] * player_vars[i] for i in player_ids]
) >= team_vars[team]
prob += pulp.lpSum([team_vars[t] for t in unique_teams]) >= 8
I am programming a vehicle routing problem in Python with PuLP. I got all my code in it, but for some reason I get a negative value for one of my decision variables, even though I restricted all of them to be nonnegative.
My code is as follows (Traveltimes is a two dimensional np array, with travel times between each pair of customers (i,j), where c(i,j) = c(j,i) and c(i,i) = 0.):
My code:
numVehicles = 2
numCustomers = 2
prob = LpProblem("DSP", LpMinimize)
var = [[[0 for k in range(numVehicles)] for j in range(numCustomers+1)] for i in range(numCustomers+1)]
for i in range(numCustomers+1):
for j in range(numCustomers+1):
for k in range(numVehicles):
var[i][j][k] = LpVariable("x"+str(i)+","+str(j)+","+str(k), 0,1, cat='Binary')
# ADD OBJECTIVE
obj = ""
for i in range(numCustomers+1):
for j in range(numCustomers+1):
for k in range(numVehicles):
obj += traveltimes[i][j]*var[i][j][k]
prob += obj
# ADD CONSTRAINTS
# All customers visited
for j in range(numCustomers+1):
for k in range(numVehicles):
nr = ""
for i in range(numCustomers+1):
nr += var[i][j][k]
prob += nr == 1
# Enter each customer exactly once
for i in range(numCustomers+1):
nr = ""
for k in range(numVehicles):
for j in range(1, numCustomers+1):
nr += var[i][j][k]
prob += nr == 1
# Leave each customer exactly once
for j in range(numCustomers+1):
nr = ""
for k in range(numVehicles):
for i in range(1, numCustomers+1):
nr += var[i][j][k]
prob += nr == 1
# Per vehicle only one customer can be visited as first
nrFirst = ""
for k in range(numVehicles):
for j in range(numCustomers+1):
nrFirst += var[0][j][k]
prob += nrFirst <= 1
# Max num vehicles
nrOut = ""
for k in range(numVehicles):
for j in range(numCustomers+1):
nrOut += var[0][j][k]
prob += nrOut <= numVehicles
# Restrict x(0,j,k) to be nonpositive
for j in range(numCustomers+1):
for k in range(numVehicles):
prob += var[0][j][k] >= 0
print(prob)
# Solve LP
prob.solve()
for v in prob.variables():
print(v.name, "=", v.varValue)
print("objective=", value(prob.objective))
The first output is the formulation printed
MINIMIZE
1.731*x0,1,0 + 1.731*x0,1,1 + 2.983*x0,2,0 + 2.983*x0,2,1 + 1.731*x1,0,0 + 1.731*x1,0,1 + 9.375*x1,2,0 + 9.375*x1,2,1 + 2.983*x2,0,0 + 2.983*x2,0,1 + 9.375*x2,1,0 + 9.375*x2,1,1 + 0.0
SUBJECT TO
_C1: x0,0,0 + x1,0,0 + x2,0,0 = 1
_C2: x0,0,1 + x1,0,1 + x2,0,1 = 1
_C3: x0,1,0 + x1,1,0 + x2,1,0 = 1
_C4: x0,1,1 + x1,1,1 + x2,1,1 = 1
_C5: x0,2,0 + x1,2,0 + x2,2,0 = 1
_C6: x0,2,1 + x1,2,1 + x2,2,1 = 1
_C7: x0,1,0 + x0,1,1 + x0,2,0 + x0,2,1 <= 1
_C8: x1,1,0 + x1,1,1 + x1,2,0 + x1,2,1 <= 1
_C9: x2,1,0 + x2,1,1 + x2,2,0 + x2,2,1 <= 1
_C10: x0,0,0 + x0,1,0 + x0,2,0 <= 1
_C11: x0,0,0 + x0,0,1 + x0,1,0 + x0,1,1 + x0,2,0 + x0,2,1 <= 1
VARIABLES
0 <= x0,0,0 <= 1 Integer
0 <= x0,0,1 <= 1 Integer
0 <= x0,1,0 <= 1 Integer
0 <= x0,1,1 <= 1 Integer
0 <= x0,2,0 <= 1 Integer
0 <= x0,2,1 <= 1 Integer
0 <= x1,0,0 <= 1 Integer
0 <= x1,0,1 <= 1 Integer
0 <= x1,1,0 <= 1 Integer
0 <= x1,1,1 <= 1 Integer
0 <= x1,2,0 <= 1 Integer
0 <= x1,2,1 <= 1 Integer
0 <= x2,0,0 <= 1 Integer
0 <= x2,0,1 <= 1 Integer
0 <= x2,1,0 <= 1 Integer
0 <= x2,1,1 <= 1 Integer
0 <= x2,2,0 <= 1 Integer
0 <= x2,2,1 <= 1 Integer
It can clearly be observed that all variables are restricted to be an integer between 0 and 1 (thus binary). However, for some reason, I do get negative values for some variable(s), as can be seen below
x0,0,0 = 0.0
x0,0,1 = -1.0
x0,1,0 = 0.0
x0,1,1 = 1.0
x0,2,0 = 0.0
x0,2,1 = 1.0
x1,0,0 = 1.0
x1,0,1 = 1.0
x1,1,0 = 1.0
x1,1,1 = 0.0
x1,2,0 = 0.0
x1,2,1 = 0.0
x2,0,0 = 0.0
x2,0,1 = 1.0
x2,1,0 = 0.0
x2,1,1 = 0.0
x2,2,0 = 1.0
x2,2,1 = 0.0
objective= 11.159
Really looking forward to any suggestions on how to solve this problem, since I clearly do not want negative values!
As a few others have suggested you should write a Minimum Complete and Verifiable Example.
That said, if you are getting constraints violated, and you are sure you've implemented them correctly, I reckon you have an infeasible problem (i.e. if you looked at your constraints carefully you would find there is a combination which makes solving impossible).
To check this add:
print (("Status:"), LpStatus[prob.status])
Just after you do prob.solve(). I reckon you'll find it's infeasible.
prob += nr == 1
"+=" is for assignment
"==" is checking for equivalence, and belongs in an "if" statement or a "while".
For instance:
if prob + nr == 1: #execute what follows if prob + nr is equal to 1
When I was testing a counter, I discovered that it only seems to display the last item to go through it. For example, if something was excellent, it showed up as counted so it would be "1". However regardless of other data, the rest would be 0.
def mealrating(score, review):
for x in range(0,len(score)):
mp = 0
mg = 0
me = 0
if score[x] >= 1 and score[x] <= 3:
review.append("poor")
mp = mp + 1
if score[x] >= 4 and score[x] <= 6:
review.append("good")
mg = mg + 1
if score[x] >= 7 and score[x] <= 10:
review.append("excellent")
me = me + 1
print("The customer rated tonight's meal as:")
print('Poor:' + str(mp))
print('Good:' + str(mg))
print('Excellent:' + str(me))
print("\n")
You are resetting mp, mg, and me in each iteration.
def mealrating(score, review):
mp = 0
mg = 0
me = 0
for x in range(0,len(score)):
if score[x] >= 1 and score[x] <= 3:
review.append("poor")
mp = mp + 1
if score[x] >= 4 and score[x] <= 6:
review.append("good")
mg = mg + 1
if score[x] >= 7 and score[x] <= 10:
review.append("excellent")
me = me + 1
print("The customer rated tonight's meal as:")
print('Poor:' + str(mp))
print('Good:' + str(mg))
print('Excellent:' + str(me))
print("\n")
You must initialize the counters outside the loop:
mp = 0
mg = 0
me = 0
for x in range(0, len(score)):
# same as before
Otherwise they'll get reset at each iteration! To make your code more Pythonic, take the following tips into consideration:
A condition of the form x >= i and x <= j can be written more concisely as i <= x <= j
The idiomatic way to traverse a list is using iterators, without explicitly using indexes
The conditions are mutually exclusive, so you should use elif
Use += for incrementing a variable
This is what I mean:
mp = mg = me = 0
for s in score:
if 1 <= s <= 3:
review.append("poor")
mp += 1
elif 4 <= s <= 6:
# and so on