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

Most is done #133

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions app/controllers/admin/vp_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
include Process

require 'json'
require 'open3'

class Admin::VpController < Admin::AdminController
before_filter :authorize_vp

def index
end

def committees
@candidates = Candidate.approved.current.sort_by {|c| (c.person && c.person.last_name.downcase) || "zzz" }
cand_hash = {}
@committee_out = ""
@err = ""
@candidates.each{|cand| cand_hash[cand.person.id] = cand.committee_preferences}
Open3.popen3("python -i ../script/hungary") do |stdin, stdout, stderr|
input = JSON.dump(cand_hash)
stdin.puts "solve(split(parse('"+ input + "')))"
stdin.puts "exit()"
stdout.each_line { |line|
line.sub(/[0-9]{1,}/) {|num| Person.find_by_id(num.to_i).full_name}
@committee_out += "\n" + line}
stderr.each_line { |line| @err += "\n" + line}
stdin.close
end
end
end
4 changes: 4 additions & 0 deletions app/views/admin/vp/committees.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Committee Assignments
<br>
<%= simple_format(@err) %>
<%= simple_format(@committee_out) %>
1 change: 1 addition & 0 deletions app/views/admin/vp/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
<li><%= link_to "Confirm RSVPs", confirm_rsvps_index_path('candidates') %></li>
<li><a href="/admin/general/super_page/" class="navigation_sublevel_item"> Super Page</a></li>
<li><%= link_to 'Cand Applications', admin_cand_applications_path %></li>
<li><%= link_to 'Committee Assigments', admin_vp_committees_path %></li>
</ul>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@

scope "vp" do
match "/" => "vp#index", :as => :vp, :via => [:get, :post]
get "committees" => "vp#committees", :as => :vp_committees
scope "eligibilities" do
get "/" => "eligibilities#list", :as => :eligibilities
post "update" => "eligibilities#update", :as => :update_eligibilities
Expand Down
45 changes: 45 additions & 0 deletions script/hungary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import json
from munkres import Munkres, print_matrix, make_cost_matrix

def split(info):
committees = ['CompServ','Tutoring','Indrel','Activities','StudRel','Bridge','Service']
for cid in info['data']:
prefs = info['data'][cid]
for committee in committees:
start = prefs.find(committee)
prefs = prefs[:start] + committee + " " + prefs[start + len(committee):]
info['data'][cid] = prefs[:len(prefs)-1]
return info

def parse(jsonInfo):
return json.loads(jsonInfo)

#data format:
#{data: {id: prefs...}, spots: {"compserv":3...}}
def solve(info):
matrix = [] #'profit matrix'
#sorted list of all candidate ids
candidates = sorted([cid for cid in info['data']])
#map candidates to a list of their preferences
c_prefs = {cid:info['data'][cid].split(" ") for cid in info['data']}
length = len(c_prefs[0]) #number of choices
#sorted list of all committee spots
c_spots = sorted([name for name in info['spots']
for _ in range(info['spots'][name])])

#building the matrix, each represents a candidate
for cid in candidates:
prefs_matrix = []
for spot in c_spots:
#first in preference list given highest weight
prefs_matrix.append((length - c_prefs[cid].index(spot)))
matrix.append(prefs_matrix)

#turn 'profit matrix' into 'cost matrix' by subtracting from a
#sufficiently large ceiling, in this case: 100
cost_matrix = make_cost_matrix(matrix, lambda cost: 100 - cost)

m = Munkres()
indexes = m.compute(cost_matrix)
for row, column in indexes:
print('{0} : {1}'.format(row, c_spots[column]))