sigma 0.0.0 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/generators/sigma/migration_generator.rb +23 -4
- data/lib/generators/sigma/model_updater.rb +15 -0
- data/lib/generators/sigma/sigma_generator.rb +23 -0
- data/lib/generators/templates/resource_model.rb +114 -0
- data/lib/generators/templates/sigma.rb +1 -0
- data/lib/sigma.rb +0 -1
- metadata +7 -5
- data/lib/generators/sigma/setup_generator.rb +0 -22
- data/lib/sigma/core.rb +0 -5
@@ -5,6 +5,7 @@ module MigrationGenerator
|
|
5
5
|
generate("migration", "add_wins_to_#{@model_name.pluralize} wins:integer")
|
6
6
|
generate("migration", "add_losses_to_#{@model_name.pluralize} losses:integer")
|
7
7
|
generate("migration", "add_draws_to_#{@model_name.pluralize} draws:integer")
|
8
|
+
generate("migration", "add_expectations_to_#{@model_name.pluralize} expectations:string")
|
8
9
|
end
|
9
10
|
|
10
11
|
def set_default_values
|
@@ -12,11 +13,29 @@ module MigrationGenerator
|
|
12
13
|
migrations.each do |m|
|
13
14
|
name = m.split(/^[0-9]+_/)[1]
|
14
15
|
if name == "add_skill_to_#{@model_name.pluralize}.rb"
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
16
|
+
update_migration(m, @scale/2, ":float")
|
17
|
+
elsif name == "add_doubt_to_#{@model_name.pluralize}.rb"
|
18
|
+
update_migration(m, @scale/6, ":float")
|
19
|
+
elsif name == "add_wins_to_#{@model_name.pluralize}.rb" || name == "add_losses_to_#{@model_name.pluralize}.rb" || name == "add_draws_to_#{@model_name.pluralize}.rb"
|
20
|
+
update_migration(m, 0, ":integer")
|
19
21
|
end
|
20
22
|
end
|
21
23
|
end
|
24
|
+
|
25
|
+
def migrate
|
26
|
+
puts <<-EOS
|
27
|
+
|
28
|
+
=======================================
|
29
|
+
> Running rake db:migrate
|
30
|
+
=======================================
|
31
|
+
|
32
|
+
EOS
|
33
|
+
rake("db:migrate")
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def update_migration (file, default, after)
|
39
|
+
inject_into_file "db/migrate/#{file}", ", :default => #{default}", :after => "#{after}"
|
40
|
+
end
|
22
41
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module ModelUpdater
|
2
|
+
def add_methods
|
3
|
+
resource = File.read find_in_source_paths("resource_model.rb")
|
4
|
+
inject_into_file "app/models/#{@model_name}.rb",
|
5
|
+
":skill, :doubt, :wins, :losses, :draws, :expectations, ",
|
6
|
+
:after => "attr_accessible "
|
7
|
+
inject_into_file "app/models/#{@model_name}.rb",
|
8
|
+
"\n#{resource}\n",
|
9
|
+
:after => /attr_accessible [a-z:_, ]+$/
|
10
|
+
end
|
11
|
+
|
12
|
+
def creating_templates
|
13
|
+
template "sigma.rb", "config/initializers/sigma.rb"
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require "generators/sigma/model_updater"
|
2
|
+
require "generators/sigma/migration_generator"
|
3
|
+
require "generators/sigma/generator_instructions"
|
4
|
+
|
5
|
+
class SigmaGenerator < Rails::Generators::Base
|
6
|
+
include MigrationGenerator, GeneratorInstructions, ModelUpdater
|
7
|
+
|
8
|
+
source_root File.expand_path("../../templates", __FILE__)
|
9
|
+
|
10
|
+
desc "Setup Sigma for some resource"
|
11
|
+
|
12
|
+
def execute
|
13
|
+
@model_name = ask("What is your resource model? (eg. user)")
|
14
|
+
@scale = ask("What will be the scale? (default is 50)").to_f
|
15
|
+
@scale = 50.0 if 0
|
16
|
+
generate_migrations
|
17
|
+
set_default_values
|
18
|
+
creating_templates
|
19
|
+
add_methods
|
20
|
+
migrate
|
21
|
+
instructions
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
before_create "self.expectations = {
|
2
|
+
'win_expectation'=>{'wins'=>0, 'losses'=>0, 'draws'=>0},
|
3
|
+
'lost_expectation'=>{'wins'=>0, 'losses'=>0, 'draws'=>0},
|
4
|
+
'draw_expectation'=>{'wins'=>0, 'losses'=>0, 'draws'=>0}
|
5
|
+
}"
|
6
|
+
|
7
|
+
serialize :expectations
|
8
|
+
|
9
|
+
scope :ranking, select("#{self.name.pluralize.downcase}.*")
|
10
|
+
.select("(#{self.name.pluralize.downcase}.skill - 3 * #{self.name.pluralize.downcase}.doubt) as rating")
|
11
|
+
.order("rating DESC")
|
12
|
+
|
13
|
+
def rating
|
14
|
+
self.skill - 3 * self.doubt
|
15
|
+
end
|
16
|
+
|
17
|
+
def position
|
18
|
+
self.class.ranking.index(self) + 1
|
19
|
+
end
|
20
|
+
|
21
|
+
def matches
|
22
|
+
self.wins + self.losses + self.draws
|
23
|
+
end
|
24
|
+
|
25
|
+
def won(match_difficult)
|
26
|
+
define_match_data(match_difficult)
|
27
|
+
|
28
|
+
if @expectation == true
|
29
|
+
skill_won = self.skill/@alpha/Sigma::SCALE/(Sigma::SCALE+@difficult/@alpha)
|
30
|
+
else
|
31
|
+
skill_won = self.skill * @alpha
|
32
|
+
end
|
33
|
+
|
34
|
+
self.skill = self.skill + skill_won
|
35
|
+
update_sigma(true)
|
36
|
+
self.increment :wins
|
37
|
+
self.save
|
38
|
+
end
|
39
|
+
|
40
|
+
def lost(match_difficult)
|
41
|
+
define_match_data(match_difficult)
|
42
|
+
|
43
|
+
if !@expectation
|
44
|
+
skill_lost = self.skill/@alpha/Sigma::SCALE/(Sigma::SCALE+@difficult.abs/@alpha)
|
45
|
+
else
|
46
|
+
skill_lost = self.skill * @alpha
|
47
|
+
end
|
48
|
+
self.skill = self.skill - skill_lost
|
49
|
+
update_sigma(false)
|
50
|
+
self.increment :losses
|
51
|
+
self.save
|
52
|
+
end
|
53
|
+
|
54
|
+
def define_match_data(difficult)
|
55
|
+
@expectation = (difficult == 0) ? 0 : difficult > 0
|
56
|
+
@resource_probability = probability(@expectation)
|
57
|
+
|
58
|
+
if difficult == 0 || difficult < 2.5*Sigma::SCALE/100 && difficult > 0
|
59
|
+
@difficult = 2.5*Sigma::SCALE/100
|
60
|
+
elsif difficult > -2.5*Sigma::SCALE/100 && difficult < 0
|
61
|
+
@difficult = -2.5*Sigma::SCALE/100
|
62
|
+
else
|
63
|
+
@difficult = difficult.abs
|
64
|
+
end
|
65
|
+
@alpha = (@difficult / Sigma::SCALE).abs
|
66
|
+
end
|
67
|
+
|
68
|
+
def update_sigma(exp)
|
69
|
+
exp_result = (@expectation == true) ? 'win_expectation' : nil
|
70
|
+
exp_result ||= (@expectation == false) ? 'lost_expectation' : 'draw_expectation'
|
71
|
+
result = (exp == true) ? 'wins' : nil
|
72
|
+
result ||= (exp == false) ? 'losses' : 'draws'
|
73
|
+
expectations = self.expectations[exp_result][result]
|
74
|
+
|
75
|
+
self.expectations[exp_result][result] = expectations + 1
|
76
|
+
if @expectation == exp
|
77
|
+
salpha = (1 - @resource_probability) * @alpha
|
78
|
+
self.doubt = self.doubt - self.doubt * salpha
|
79
|
+
else
|
80
|
+
salpha = @resource_probability * @alpha
|
81
|
+
self.doubt = self.doubt + self.doubt * salpha
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def probability(expectation)
|
86
|
+
result = (expectation == true) ? 'wins' : nil
|
87
|
+
result ||= (expectation == false) ? 'losses' : 'draws'
|
88
|
+
|
89
|
+
exp_result = (expectation == true) ? :we : nil
|
90
|
+
exp_result ||= (expectation == false) ? :le : :de
|
91
|
+
|
92
|
+
w = self.wins * 100.0 / ((matches == 0) ? 1 : matches)
|
93
|
+
l = self.losses * 100.0 / ((matches == 0) ? 1 : matches)
|
94
|
+
d = self.draws * 100.0 / ((matches == 0) ? 1 : matches)
|
95
|
+
|
96
|
+
expectations = {
|
97
|
+
'wins' => { we:0, le:0, de:0 },
|
98
|
+
'losses' => { we:0, le:0, de:0 },
|
99
|
+
'draws' => { we:0, le:0, de:0 }
|
100
|
+
}
|
101
|
+
|
102
|
+
expectations.each do |k, v|
|
103
|
+
mult = (k == 'wins') ? w : nil
|
104
|
+
mult ||= (k == 'losses') ? l : d
|
105
|
+
|
106
|
+
v[:we] = mult*(self.expectations['win_expectation'][k]*100.0/((self[k] == 0) ? 1 : self[k]))
|
107
|
+
v[:le] = mult*(self.expectations['lost_expectation'][k]*100.0/((self[k] == 0) ? 1 : self[k]))
|
108
|
+
v[:de] = mult*(self.expectations['draw_expectation'][k]*100.0/((self[k] == 0) ? 1 : self[k]))
|
109
|
+
end
|
110
|
+
|
111
|
+
all_probabilities = expectations[result][:we]+expectations[result][:le]+expectations[result][:de]
|
112
|
+
probability = expectations[result][exp_result] / ((all_probabilities == 0) ? 1 : all_probabilities)
|
113
|
+
(probability == 0) ? 0.25 : probability
|
114
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
Sigma::SCALE = <%= @scale %>
|
data/lib/sigma.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sigma
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-05-06 00:00:00.
|
12
|
+
date: 2013-05-06 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: ''
|
15
15
|
email: joaomdmoura@gmail.com
|
@@ -20,8 +20,10 @@ files:
|
|
20
20
|
- lib/sigma.rb
|
21
21
|
- lib/generators/sigma/generator_instructions.rb
|
22
22
|
- lib/generators/sigma/migration_generator.rb
|
23
|
-
- lib/generators/sigma/
|
24
|
-
- lib/sigma/
|
23
|
+
- lib/generators/sigma/model_updater.rb
|
24
|
+
- lib/generators/sigma/sigma_generator.rb
|
25
|
+
- lib/generators/templates/resource_model.rb
|
26
|
+
- lib/generators/templates/sigma.rb
|
25
27
|
- init.rb
|
26
28
|
homepage: http://joaomdmoura.github.com/sigma/
|
27
29
|
licenses: []
|
@@ -43,7 +45,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
43
45
|
version: '0'
|
44
46
|
requirements: []
|
45
47
|
rubyforge_project:
|
46
|
-
rubygems_version: 1.8.
|
48
|
+
rubygems_version: 1.8.15
|
47
49
|
signing_key:
|
48
50
|
specification_version: 3
|
49
51
|
summary: A ranking algorithm implementation for Ruby on Rails applications.
|
@@ -1,22 +0,0 @@
|
|
1
|
-
require "generators/sigma/migration_generator"
|
2
|
-
require "generators/sigma/generator_instructions"
|
3
|
-
|
4
|
-
class Sigma
|
5
|
-
class SetupGenerator < Rails::Generators::Base
|
6
|
-
include MigrationGenerator
|
7
|
-
include GeneratorInstructions
|
8
|
-
|
9
|
-
source_root File.expand_path("../../templates", __FILE__)
|
10
|
-
|
11
|
-
desc "Setup Sigma for some resource"
|
12
|
-
|
13
|
-
def execute
|
14
|
-
@model_name = ask("What is your resource model? (eg. user)")
|
15
|
-
@scale = ask("What will be the scale? (eg. 50)").to_i
|
16
|
-
generate_migrations
|
17
|
-
set_default_values
|
18
|
-
instructions
|
19
|
-
end
|
20
|
-
|
21
|
-
end
|
22
|
-
end
|