Skip to content

Commit

Permalink
added copeland aggregating method
Browse files Browse the repository at this point in the history
  • Loading branch information
py4 committed Oct 3, 2014
1 parent 97fa364 commit 8ebb922
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 41 deletions.
42 changes: 14 additions & 28 deletions lib/inits/config.yml
Original file line number Diff line number Diff line change
@@ -1,31 +1,17 @@
---
method: kemeny
method: copeland
out: /home/pooya/Projects/UTR/samples/output.dat
estimators:
pooya:
path: /home/pooya/Projects/UTR/samples/pooya.dat
weight: 1
hamed:
path: /home/pooya/Projects/UTR/samples/hamed.dat
listnet:
path: /home/pooya/Projects/UTR/samples/listnet.dat
weight: 2
javid:
path: /home/pooya/Projects/UTR/samples/javid.dat
weight: 5

# method: kemeny
# out: /home/pooya/Projects/UTR/samples/output.dat
# estimators:
# listnet:
# path: /home/pooya/Projects/UTR/samples/listnet.dat
# weight: 2
# ranknet:
# path: /home/pooya/Projects/UTR/samples/ranknet.dat
# weight: 3
# listmle:
# path: /home/pooya/Projects/UTR/samples/listmle.dat
# weight: 3
# svm:
# path: /home/pooya/Projects/UTR/samples/svm.dat
# weight: 1
# evaluation:
# solution_file: /home/pooya/Projects/UTR/samples/solution.dat
ranknet:
path: /home/pooya/Projects/UTR/samples/ranknet.dat
weight: 3
listmle:
path: /home/pooya/Projects/UTR/samples/listmle.dat
weight: 3
svm:
path: /home/pooya/Projects/UTR/samples/svm.dat
weight: 1
evaluation:
solution_file: /home/pooya/Projects/UTR/samples/solution.dat
9 changes: 5 additions & 4 deletions lib/inits/messages.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
---
init: -> initializing and loading configs
loading_data: -> loading data from input files
init: -> Unitializing and loading configs
loading_data: -> Loading data from input files
dumping: -> Writing aggregation result to file
evaluating: -> evaluating base rankings and the aggregation
evaluating: -> Evaluating base rankings and the aggregation
kemeny: -> Applying kemeny aggregating method
borda: -> Applying borda aggregating method
done: -> done :)
copeland: -> Applying copeland aggregating method
done: -> Done :)

44 changes: 44 additions & 0 deletions lib/methods/copeland.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require './lib/inits/config.rb'
require './lib/common/quick.rb'

module CopelandModule

def self.included(base)
base.extend(ClassMethods)
end

module ClassMethods

def run data
puts $MESSAGES[:copeland]
scores = Hash.new { |h,k| h[k] = Hash.new {|hh,kk| hh[kk] = 0}}
data.each do |estimator, hash|
hash.each do |group_id, hash2|
hash2.keys[0..-2].each_with_index do |instance_id,i|
(i+1..hash2.keys.size-1).each do |j|
#puts "comparing #{instance_id}:#{hash2[instance_id]} with #{hash2.keys[j]}:#{hash2[hash2.keys[j]]}"
if hash2[instance_id] > hash2[hash2.keys[j]]
scores[group_id][instance_id] += 1
scores[group_id][hash2.keys[j]] -= 1
elsif hash2[instance_id] < hash2[hash2.keys[j]]
scores[group_id][instance_id] -= 1
scores[group_id][hash2.keys[j]] += 1
end
end
end
end
end

result = {}
scores.each do |group_id, hash|
result[group_id] = hash.keys.sort_by { |instance_id| [-1*hash[instance_id], -1*instance_id] }
end

puts result
return result

end

end

end
7 changes: 7 additions & 0 deletions lib/utr.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require './lib/inits/config.rb'
require './lib/methods/kemeny'
require './lib/methods/borda'
require './lib/methods/copeland'
require './lib/evaluation/evaluate'

class UTR
Expand All @@ -17,6 +18,10 @@ class Kemeny
class Borda
include BordaModule
end

class Copeland
include CopelandModule
end
end

class Evaluatation
Expand Down Expand Up @@ -60,6 +65,8 @@ def run
dump_result Methods::Kemeny.run data
when "borda"
dump_result Methods::Borda.run data
when "copeland"
dump_result Methods::Copeland.run data
end

puts $MESSAGES[:done]
Expand Down
19 changes: 10 additions & 9 deletions samples/output.dat
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
1,8,9
1,6,8
1,1,7
1,4,6
1,9,5
1,7,4
1,5,3
1,2,2
1,3,1
1,222,5
1,333,4
1,444,3
1,111,2
1,555,1
2,236,5
2,234,4
2,233,3
2,231,2
2,232,1

0 comments on commit 8ebb922

Please sign in to comment.