activecube 0.1.25 → 0.1.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 94f1e4575a041108004194789f85b9c030c6f7f94083677dee253de3cc878966
4
- data.tar.gz: b182640a584166cefc17b7b26e7add652600b0c937f08b83689a02f819bb2bb9
3
+ metadata.gz: d0c91dca78a440d0db5b8b0a28531d4c922a9dc81062aad663052afd671a500d
4
+ data.tar.gz: da104891e98f8cfcb14f91dcc74db0a2e418209e1380baf93a4a03d9653454d0
5
5
  SHA512:
6
- metadata.gz: ab68fd59fdbffe5e0b3d4b8f0523d351b5a6c2840090328af0e78ed10c7fc23e402335f99f05d114da175c57d0b12f0d4fa02195a89ab52108af451b2dae0445
7
- data.tar.gz: ace9d8f215cb59e930cf66249211405408c2bce905166fbe59370708dea17502d40d2416611297a1d6c06d0f6930ee3e73460775cfe1ad32895030db22a49c65
6
+ metadata.gz: d381b939daec89b03e758a595959fa903ace82f958402c6b7a595b00e28fa1a16593201dc6daec6af99d79faa4e82ab4f76a81c153780eaf890cdc853edbd11b
7
+ data.tar.gz: d9e9c8d31921dbb6fa43843a35da245594de76182bf85ee207d1bfd19d92ad099ffbe74391ddd0b08d4f3c34aaf443d21a5cbc8b31321a1ae7aa030e88fe1b35
@@ -9,7 +9,7 @@ GIT
9
9
  PATH
10
10
  remote: .
11
11
  specs:
12
- activecube (0.1.24)
12
+ activecube (0.1.25)
13
13
  activerecord (>= 5.2)
14
14
 
15
15
  GEM
@@ -18,7 +18,7 @@ module Activecube::Processor
18
18
  @tables_count = cost_matrix.map(&:count).max
19
19
  @metrics_count = cost_matrix.count
20
20
 
21
- tables_count==1 ? [0]*metrics_count : do_optimize
21
+ (tables_count==1 || metrics_count==0) ? [0]*metrics_count : do_optimize
22
22
 
23
23
  end
24
24
 
@@ -27,95 +27,41 @@ module Activecube::Processor
27
27
 
28
28
  private
29
29
 
30
- def do_optimize
30
+ def generate_variants vs, metric_i
31
31
 
32
- @tables_by_metrics = []
32
+ return vs if metric_i==metrics_count
33
33
 
34
- # sort metrics from low min cost to higher min costs ( by all applicable tables )
35
- sort_metrics
34
+ metric_tables = cost_matrix[metric_i].map.with_index do |c, index|
35
+ [index] if c
36
+ end.compact
36
37
 
37
- # fill initial @tables_by_metrics by selecting tables with minimum cost for metrics.
38
- # If there are more than one table with this minimum cost, then select already selected table with maximum cost
39
- select_min_cost_by_metric
38
+ vsnew = if metric_i==0
39
+ metric_tables
40
+ else
41
+ arry = []
42
+ vs.each do |v|
43
+ metric_tables.each{|newv|
44
+ arry << (v + newv)
45
+ }
46
+ end
47
+ arry
48
+ end
40
49
 
41
- # make iterations over @tables_by_metrics ( max MAX_ITERATIONS)
42
- iterates
50
+ generate_variants vsnew, metric_i+1
43
51
 
44
- @tables_by_metrics
45
52
  end
46
53
 
47
- def sort_metrics
48
- @metrics_index_sorted = (0...metrics_count).sort_by{|m_i| cost_matrix[m_i].compact.min || UNLIM_COST }
54
+ def cost_for variant
55
+ variant.each_with_index.group_by(&:first).collect do |table_index, arry|
56
+ arry.map(&:second).map{|metric_index| cost_matrix[metric_index][table_index] }.max
57
+ end.sum
49
58
  end
50
59
 
51
- def select_min_cost_by_metric
52
-
53
- @metrics_index_sorted.collect do |m_i|
54
-
55
- table_index_cost = (0...tables_count).map{|c_i| [c_i,
56
- cost_matrix[m_i][c_i] || UNLIM_COST,
57
- (@tables_by_metrics.include?(c_i) ? -cost_matrix[@tables_by_metrics.index(c_i)][c_i] : 0)
58
- ]}.sort_by(&:third).sort_by(&:second)
59
-
60
- @tables_by_metrics[m_i] = table_index_cost.first.first
61
-
62
- end
63
- end
64
-
65
- def iterates
66
-
67
- steps = [@tables_by_metrics]
68
-
69
- (1..MAX_ITERATIONS).each do |iteration|
70
-
71
- step = []
72
- prev_step = steps.last
73
-
74
- prev_step.each_with_index {|c_i, m_i|
75
-
76
- table_included_times = prev_step.select{|c| c==c_i }.count
77
- old_cost = cost_matrix[m_i][c_i]
78
- new_c_i = (0...tables_count).detect{|c_n|
79
- new_cost = cost_matrix[m_i][c_n]
80
- next if c_i==c_n || new_cost.nil?
81
- new_table_included_times = prev_step.select{|c| c==c_n }.count
82
-
83
- if old_cost.nil?
84
- # if we have non indexed table now
85
- true
86
- elsif table_included_times>1
87
- if new_table_included_times>0
88
- # table to used table if
89
- # cost now > new cost
90
- old_cost > new_cost
91
- else
92
- # table to unused table if
93
- # cost now > new cost + max other cost in table now
94
- old_cost > new_cost + ( prev_step.select.with_index{|c,i| c==c_i && i!=m_i }.map{|c| cost_matrix[m_i][c]}.max || UNLIM_COST )
95
- end
96
- else
97
- if new_table_included_times>0
98
- # unused table to table if
99
- # new cost < cost now + max other cost in new table
100
- old_cost > new_cost - ( prev_step.select{|c| c==c_n }.map{|c| cost_matrix[m_i][c]}.max || UNLIM_COST )
101
- else
102
- # unused to unused
103
- # cost now > new cost
104
- old_cost > new_cost
105
- end
106
- end
107
-
108
- }
109
-
110
- step << (new_c_i || c_i)
111
-
112
- }
113
-
114
- break if steps.include? step
115
- steps << step
116
- end
60
+ def do_optimize
117
61
 
118
- @tables_by_metrics = steps.last
62
+ variants = generate_variants [], 0
63
+ variant_costs = variants.map{|v| cost_for v}
64
+ variants[variant_costs.each_with_index.min.second]
119
65
 
120
66
  end
121
67
 
@@ -1,3 +1,3 @@
1
1
  module Activecube
2
- VERSION = "0.1.25"
2
+ VERSION = "0.1.26"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activecube
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.25
4
+ version: 0.1.26
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aleksey Studnev