Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ROI.plugin.symphony causes R session to crash #24

Open
gacolitti opened this issue Oct 26, 2023 · 0 comments
Open

ROI.plugin.symphony causes R session to crash #24

gacolitti opened this issue Oct 26, 2023 · 0 comments

Comments

@gacolitti
Copy link
Owner

ROI.plugin.symphony causes R to crash sometimes.

Here is a reprex:

schematic <- readRDS("schematic.rds")

draft_group <- schematic$draft_group
rules <- schematic$rules
draft_group_id <- schematic$draft_group_id
max_points <- NULL

mod <- ompr::MIPModel() |>
  
  ompr::add_variable(draftable_id[i],
                     i = draft_group$row_number,
                     type = "binary") |>
  
  ompr::add_variable(competition_id[j],
                     j = unique(draft_group$competition_id),
                     type = "binary") |>
  
  ompr::set_objective(
    ompr::sum_over(draft_group$exp_fp[i] * draftable_id[i],
                   i = draft_group$row_number), "max"
  ) |>
  
  # The sum of all player salaries must be less than salary cap max value
  ompr::add_constraint(
    ompr::sum_over(draft_group$salary[i] * draftable_id[i],
                   i = draft_group$row_number) <= rules$salary_cap_max_value
  )

# Optionally set an upper bound on the number of expected fantasy points
# This is used to get multiple lineups
if (!is.null(max_points)) {
  
  mod <- mod |>
    ompr::add_constraint(
      ompr::sum_over(draft_group$exp_fp[i] * draftable_id[i],
                     i = draft_group$row_number) <= max_points
    )
  
}

# Ensure at least two competitions are present
if (!is.na(rules$game_count_min_value)) {
  
  mod <- mod |>
    # force group binary variables to be 1 if item is in group
    ompr::add_constraint(
      ompr::sum_over(
        draftable_id[i],
        i = draft_group$row_number[which(draft_group$competition_id == j)]
      ) - 10000 * competition_id[j] <= 0,
      j = unique(draft_group$competition_id)
    ) |>
    
    # force at least one binary variable for item inclusion to be 1 for all items in group
    # if group binary is 1
    ompr::add_constraint(
      competition_id[j] - 10000 * ompr::sum_over(
        draftable_id[i],
        i = draft_group$row_number[which(draft_group$competition_id == j)]
      ) <= 0,
      j = unique(draft_group$competition_id)
    ) |>
    
    # force at least two competitions
    ompr::add_constraint(
      ompr::sum_over(
        competition_id[j],
        j = unique(draft_group$competition_id)
      ) >= 2
    )
  
}

# Ensure player is selected max one time
if (rules$unique_players) {
  
  mod <- mod |>
    ompr::add_constraint(
      ompr::sum_over(draftable_id[i],
                     i = draft_group$row_number[which(draft_group$player_id == player_id)]) <= 1,
      player_id = draft_group$player_id
    )
  
}

# Set player constraints for NBA classic
if (rules$game_type_id == 70) {
  
  # Ensure at least 1 player in each position
  mod <- mod |>
    ompr::add_constraint(
      ompr::sum_over(
        draftable_id[i],
        i = draft_group$row_number[which(draft_group$position == position)]
      ) >= 1,
      position = unique(draft_group$position)
    )
  
}

ompr::solve_model(mod, ompr.roi::with_ROI(solver = "symphony"))

schematic.rds.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant