I would like to maximize the probability of winning a raffle by buying a certain number of tickets. For this, I wrote the following code
import numpy as np
import math as mt
from scipy.optimize import minimize
from pulp import *
def objective(tickets, winners = 500, losers = 2500, cost_of_ticket = 40, winning_amt = 1000):
Pwin = 1 - mt.factorial(losers)//mt.factorial(losers - tickets)*mt.factorial(winners+losers-tickets)/mt.factorial(winners+losers)
Ewin = Pwin*(winning_amt - cost_of_ticket*tickets)
return Ewin
# declare your variables
tickets = LpVariable("tickets", range(0, 10)) # 0<= tickets <= 10
prob = LpProblem("problem", LpMaximize)
#define the objective
prob += objective(tickets)
# solve the problem
status = prob.solve(GLPK(msg=0))
LpStatus[status]
# print the results
value(tickets)
The issue seems to be that the number of tickets that get passed into the objective function is not an integer (and the factorial function then fails). Can anyone suggest how I should ensure that the ticket is restricted to positive integer values?
The answer, for checking, should be 8. I can do this by manually calling the objective function and checking.
int()on the value. Is there a reason you can't do that?ticketsis an object of typeLpVariablehow can you do something like:losers - ticketswhat's the meaning of an integer minus an object ?