-
Notifications
You must be signed in to change notification settings - Fork 0
Max cov example #7
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f683b49
WIP: maximum coverage game
brunompacheco 940a813
refactor expression internalization outwards of payoff setting
brunompacheco bf77a08
WIP: working for 2p games
brunompacheco 8f7c6f4
fix: bilateral payoff with affexpr was not auto casted to quadexpr
brunompacheco fe59eb5
fix: computing bilateral payoff when x_other has no influence
brunompacheco b62a1c0
maxcov works for larger problems
brunompacheco 07f4dd4
explain why v_bar_p is ignored in compute_bilateral_payoff with AffExpr
brunompacheco e63da71
fix: setting payoff in internalize_expr
brunompacheco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| using CSV | ||
| using DataFrames | ||
| using PyCall | ||
|
|
||
| py""" | ||
| import pickle | ||
|
|
||
| def load_pickle(fpath): | ||
| with open(fpath, "rb") as f: | ||
| data = pickle.load(f) | ||
| return data | ||
| """ | ||
|
|
||
| load_pickle = py"load_pickle" | ||
|
|
||
| # these parameters must match those in the csv file name, and the folder from which they are downloaded | ||
| type_dataset = "multi" | ||
| county_size = 5 | ||
| num_lakes_per_county = 50 | ||
| budget_ratio = 0.5 | ||
|
|
||
| dirname = "EBMC_generated/$(type_dataset)_dataset/" | ||
| fname = "$(county_size)_$(num_lakes_per_county)_$(budget_ratio).csv" | ||
| df_edge = DataFrame(CSV.File(dirname * fname)) | ||
|
|
||
| info_data = load_pickle(dirname * "info_data.pickle") | ||
|
|
||
| # === Unpack Experiment Settings === # | ||
| # extract the value list for the (county_size, num_lakes_per_county, budget_ratio) key | ||
| vals = info_data[(county_size, num_lakes_per_county, budget_ratio)] | ||
|
|
||
| # values come from Python (0-based originally) so use 1-based Julia indices | ||
| counties = vals[1] # likely a sequence of county ids | ||
| num_lakes_per_county = Int(vals[2]) # ensure it's an Int | ||
| infestation_status = vals[3] # likely a Python dict | ||
| county_budget = vals[4] | ||
|
|
||
| # determine infested lakes (any value > 0 in the nested dict) | ||
| infested_lakes = String[] | ||
| for (key, infestation_vals) in infestation_status | ||
| if any(v -> v > 0, values(infestation_vals)) | ||
| push!(infested_lakes, string(key)) | ||
| end | ||
| end | ||
|
|
||
| # === lakes and lake->county mapping === # | ||
| lakes = unique(vcat(df_edge[:, :dow_origin], df_edge[:, :dow_destination])) | ||
| lake_county = Dict(lake => lake[1:2] for lake in lakes) # first two chars like Python's [:2] | ||
|
|
||
| # === Compute Lake Weights === # | ||
| w = Dict{String, Float64}(lake => 0.0 for lake in lakes) | ||
| for row in eachrow(df_edge) | ||
| if row[:bij] != 0 | ||
| ori = string(row[:dow_origin]) | ||
| dst = string(row[:dow_destination]) | ||
| w[ori] += row[:bij] * row[:weight] | ||
| w[dst] += row[:bij] * row[:weight] | ||
| end | ||
| end | ||
|
|
||
| # === Set Model Parameters === # | ||
| I = lakes | ||
|
|
||
| I_c = Dict(county => [i for i in I if i[1:2] == county] for county in counties) | ||
| I_c_complement = Dict(county => [i for i in I if !(i in I_c[county])] for county in counties) | ||
|
|
||
| # build arc dictionaries n (weight) and t (bij) | ||
| n = Dict{Tuple{Any,Any}, Float64}() | ||
| t = Dict{Tuple{Any,Any}, Float64}() | ||
| for row in eachrow(df_edge) | ||
| arc = (row[:dow_origin], row[:dow_destination]) | ||
| n[arc] = row[:weight] | ||
| t[arc] = row[:bij] | ||
| end | ||
|
|
||
| arcs = collect(keys(n)) | ||
|
|
||
| # arcs within, incoming to, and outgoing from each county | ||
| arcs_c = Dict{Any, Vector{Tuple{Any,Any}}}() | ||
| arcs_plus_c = Dict{Any, Vector{Tuple{Any,Any}}}() | ||
| arcs_minus_c = Dict{Any, Vector{Tuple{Any,Any}}}() | ||
|
|
||
| for county in counties | ||
| arcs_c[county] = [arc for arc in arcs if (arc[1][1:2] == county) && (arc[2][1:2] == county)] | ||
| arcs_plus_c[county] = [arc for arc in arcs if (arc[2][1:2] == county) && (arc[1][1:2] != county)] | ||
| arcs_minus_c[county] = [arc for arc in arcs if (arc[1][1:2] == county) && (arc[2][1:2] != county)] | ||
| end | ||
|
|
||
| # === Define and Solve SELFISH Game using IPG.jl === # | ||
|
|
||
| using IPG, SCIP | ||
| using IPG.JuMP: Containers | ||
|
|
||
| # define players | ||
| players = [Player(name=county) for county in counties] | ||
|
|
||
| # add variables | ||
| x_c = Dict(p => @variable(p.X, [I_c[p.name]], Bin, base_name="x_$(p.name)_") for p in players) | ||
| y_c = Dict(p => @variable(p.X, [arcs_minus_c[p.name]], Bin, base_name="y_$(p.name)_") for p in players) | ||
|
|
||
| # concatenate x variables | ||
| x = Containers.DenseAxisArray(vcat([x_c[p].data for p in players]...), vcat([x_c[p].axes[1] for p in players]...)) | ||
|
|
||
| for p in players | ||
| ### add constraints | ||
| # TODO: x[arc[i]] may be a variable from another player; need to translate the index | ||
| # before using in @constraint. This is a limitation of our implementation, that I am | ||
| # currently handling with the internalize_expr method. Ideally, we would have the macro | ||
| # overwritten so that the internalization is automatic. | ||
| @constraint(p.X, [arc in arcs_minus_c[p.name]], y_c[p][arc] <= IPG.internalize_expr(p, x[arc[1]] + x[arc[2]])) | ||
| @constraint(p.X, sum(x_c[p]) <= county_budget[p.name]) | ||
|
|
||
| ### set payoff | ||
| set_payoff!(p, sum(t[arc] * n[arc] * y_c[p][arc] for arc in arcs_minus_c[p.name])) | ||
| end | ||
|
|
||
| Σ, payoff_improvements = SGM(players, SCIP.Optimizer, max_iter=10, verbose=true) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.