class PuLPSolver(Solver): LP_SOLVER = PULP_CBC_CMD(verbose=False, msg=False) def __init__(self): self.prob = LpProblem('Daily Fantasy Sports', LpMaximize) def setup_solver(self): pass def add_variable(self, name, min_value=None, max_value=None): if any([min_value, max_value]): return LpVariable(name, lowBound=min_value, upBound=max_value, cat=LpInteger) return LpVariable(name, cat=LpBinary) def set_objective(self, variables, coefficients): self.prob += lpSum([ variable * coefficient for variable, coefficient in zip(variables, coefficients) ]) def add_constraint(self, variables, coefficients, sign, rhs): if coefficients: lhs = [ variable * coefficient for variable, coefficient in zip(variables, coefficients) ] else: lhs = variables if sign == SolverSign.EQ: self.prob += lpSum(lhs) == rhs elif sign == SolverSign.NOT_EQ: self.prob += lpSum(lhs) != rhs elif sign == SolverSign.GTE: self.prob += lpSum(lhs) >= rhs elif sign == SolverSign.LTE: self.prob += lpSum(lhs) <= rhs else: raise SolverException('Incorrect constraint sign') def copy(self): new_solver = type(self)() new_solver.prob = self.prob.copy() return new_solver def solve(self): self.prob.solve(self.LP_SOLVER) if self.prob.status == LpStatusOptimal: result = [] for variable in self.prob.variables(): val = variable.value() if val is not None and round(val) >= 1.0: result.append(variable) return result else: raise SolverException('Unable to solve')
class PuLPSolver(Solver): LP_SOLVER = PULP_CBC_CMD(threads=multiprocessing.cpu_count()) def __init__(self): self.prob = None def setup_solver(self): self.prob = LpProblem('Daily Fantasy Sports', LpMaximize) def add_variable(self, name): return LpVariable(name, cat=LpBinary) def set_objective(self, variables, coefficients): self.prob += lpSum([ variable * coefficient for variable, coefficient in zip(variables, coefficients) ]) def add_constraint(self, variables, coefficients, sign, rhs): if coefficients: lhs = [ variable * coefficient for variable, coefficient in zip(variables, coefficients) ] else: lhs = variables if sign == SolverSign.EQ: self.prob += lpSum(lhs) == rhs elif sign == SolverSign.NOT_EQ: self.prob += lpSum(lhs) != rhs elif sign == SolverSign.GTE: self.prob += lpSum(lhs) >= rhs elif sign == SolverSign.LTE: self.prob += lpSum(lhs) <= rhs else: raise SolverException('Incorrect constraint sign') def copy(self): new_solver = type(self)() new_solver.prob = self.prob.copy() return new_solver def solve(self): self.prob.solve(self.LP_SOLVER) if self.prob.status == LpStatusOptimal: result = [] for variable in self.prob.variables(): if variable.value() == 1.0: result.append(variable) return result else: raise SolverException('Unable to solve')
class PuLPSolver(Solver): def __init__(self): self.prob = None def setup_solver(self): self.prob = LpProblem('Daily Fantasy Sports', LpMaximize) def add_variable(self, name, low_bound, up_bound): return LpVariable(name, low_bound, up_bound, LpInteger) def set_objective(self, variables, coefficients): self.prob += lpSum([ variable * coefficient for variable, coefficient in zip(variables, coefficients) ]) def add_constraint(self, variables, coefficients, sign, rhs): lhs = [ variable * coefficient for variable, coefficient in zip(variables, coefficients) ] if sign == SolverSign.EQ: self.prob += lpSum(lhs) == rhs elif sign == SolverSign.NOT_EQ: self.prob += lpSum(lhs) != rhs elif sign == SolverSign.GTE: self.prob += lpSum(lhs) >= rhs elif sign == SolverSign.LTE: self.prob += lpSum(lhs) <= rhs else: raise SolverException('Incorrect constraint sign') def copy(self): new_solver = type(self)() new_solver.prob = self.prob.copy() return new_solver def solve(self): self.prob.solve() if self.prob.status == LpStatusOptimal: result = [] for variable in self.prob.variables(): if variable.value() == 1.0: result.append(variable) return result else: raise SolverException('Unable to solve')
class PuLPSolver(Solver): LP_SOLVER = PULP_CBC_CMD(msg=False) def __init__(self): self.prob = LpProblem('pydfs_lineup_optimizer', LpMaximize) def setup_solver(self): pass def add_variable(self, name, min_value=None, max_value=None): name = name.replace(' ', '_') if any([min_value, max_value]): return LpVariable(name, lowBound=min_value, upBound=max_value, cat=LpInteger) return LpVariable(name, cat=LpBinary) def set_objective(self, variables, coefficients): self.prob += lpSum([ variable * coefficient for variable, coefficient in zip(variables, coefficients) ]) def add_constraint(self, variables, coefficients, sign, rhs, name=None): if coefficients: lhs = [ variable * coefficient for variable, coefficient in zip(variables, coefficients) ] else: lhs = variables if sign == SolverSign.EQ: self.prob += lpSum(lhs) == rhs, name elif sign == SolverSign.NOT_EQ: self.prob += lpSum(lhs) != rhs, name elif sign == SolverSign.GTE: self.prob += lpSum(lhs) >= rhs, name elif sign == SolverSign.LTE: self.prob += lpSum(lhs) <= rhs, name else: raise SolverException('Incorrect constraint sign') def copy(self): new_solver = type(self)() new_solver.prob = self.prob.copy() return new_solver def solve(self): self.prob.solve(self.LP_SOLVER) if self.prob.status == LpStatusOptimal: result = [] for variable in self.prob.variables(): val = variable.value() if val is not None and round(val) >= 1.0: result.append(variable) return result else: invalid_constraints = [ (name or str(constraint)) for name, constraint in self.prob.constraints.items() if not constraint.valid() ] raise SolverInfeasibleSolutionException(invalid_constraints)