ravensat 0.3.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f369c1036cde3ea2ff754109809d5b4ce62f826a2ab409e9ec19fdd07ff341dc
4
- data.tar.gz: 76df72fb98e5140fcd1b2b394643f23de5218ea47f4ec03f450dc0a05074f801
3
+ metadata.gz: bd6bb73a8f6668c36675461d24272ae8e02632c5b2d3f768b5a81347ad29956a
4
+ data.tar.gz: 48e4f44fa76bd1716b32ed98ddaa82d198cb83064e944269ecfd57ab21645860
5
5
  SHA512:
6
- metadata.gz: b59a47f2b7a899e49d506f839c892178523ea3920e611278e0f31399dc1c0e5c868bb8f8a74adab5a848f26f2d2efb8779f6391a20ec566ec177a1fdacfb4565
7
- data.tar.gz: 9ed85ceb8abc589197c9241679bb5c5486ce525e427d91f84568dcc4b4ab7a58b0d110688cba999d78dacc9fd0e067d6e4f3908ef87d2d3851ed7c30331d22ca
6
+ metadata.gz: 5365bfdf37eeb55b5a7ccab53f0ecca8cf8c73ecdfcac2eb9c92ce843aa6fa2e77f711f14a9b00f37cb9a37bfade19c65ea38cbd3f7478398b0d1ef8a5cf416a
7
+ data.tar.gz: abf648c3097425241ae106e7ed42876f9c9a33e55bf4615cefc16c77943aedf62b0bd99fc6b9c2062147dbaf6c9b690b13f5a28204771e03934e4c59bdf98a9e
@@ -1,4 +1,12 @@
1
1
  module Ravensat
2
2
  class AndNode < OprNode
3
+ def &(object)
4
+ @children.append object
5
+ self
6
+ end
7
+
8
+ def to_dimacs
9
+ " 0\n"
10
+ end
3
11
  end
4
12
  end
@@ -14,17 +14,84 @@ module Ravensat
14
14
  # end
15
15
  # end
16
16
 
17
+ # def each
18
+ # case self
19
+ # when AndNode, OrNode
20
+ # @children.first.each{|c| yield(c)}
21
+ # yield(self)
22
+ # @children.last.each{|c| yield(c)}
23
+ # when NotNode
24
+ # yield(self)
25
+ # @children.first.each{|c| yield(c)}
26
+ # when VarNode
27
+ # yield(self)
28
+ # end
29
+ # end
30
+
31
+ def each_DP
32
+ node_stack = [[self, self.children.clone]] #[[parent, children], ...]
33
+
34
+ until node_stack.empty?
35
+ current_parent, current_children = node_stack.pop
36
+ current_node = current_children.shift
37
+
38
+ # puts 'loop'
39
+ # puts "node_stack.size:#{node_stack.size}"
40
+ # puts "current_parent:#{current_parent.class}"
41
+ # puts "current_children.size:#{current_children.size}"
42
+ # puts "current_node:#{current_node.class}"
43
+
44
+ case current_node
45
+ when AndNode
46
+ node_stack.push [current_parent, current_children.clone]
47
+ node_stack.push [current_node, current_node.children.clone]
48
+ when OrNode
49
+ node_stack.push [current_parent, current_children.clone]
50
+ node_stack.push [current_node, current_node.children.clone]
51
+ when NotNode
52
+ yield(current_node)
53
+ yield(current_node.children.first)
54
+
55
+ if current_children.empty?
56
+ yield(node_stack.last.first)
57
+ else
58
+ yield(current_parent)
59
+ node_stack.push [current_parent, current_children.clone]
60
+ end
61
+ when VarNode, Extension::BooleanVariable
62
+ yield(current_node)
63
+
64
+ if current_children.empty?
65
+ yield(node_stack.last.first)
66
+ else
67
+ yield(current_parent)
68
+ node_stack.push [current_parent, current_children.clone]
69
+ end
70
+ end
71
+
72
+ end
73
+ end
74
+
17
75
  def each
18
- case self
19
- when AndNode, OrNode
20
- @children.first.each{|c| yield(c)}
21
- yield(self)
22
- @children.last.each{|c| yield(c)}
23
- when NotNode
24
- yield(self)
25
- @children.first.each{|c| yield(c)}
26
- when VarNode
27
- yield(self)
76
+ return to_enum unless block_given?
77
+ node_stack = [self]
78
+
79
+ until node_stack.empty?
80
+ current_node = node_stack.shift
81
+ next unless current_node
82
+
83
+ yield current_node
84
+
85
+ node_stack = node_stack.concat(current_node.children)
86
+ end
87
+
88
+ self if block_given?
89
+ end
90
+
91
+ def each_naive
92
+ yield(self)
93
+ @children.each do |child|
94
+ child.each {|c| yield(c)}
28
95
  end
29
96
  end
30
97
 
@@ -40,6 +107,9 @@ module Ravensat
40
107
  self.class.name
41
108
  end
42
109
 
110
+ def to_dimacs
111
+ end
112
+
43
113
  def cnf?
44
114
  @children.map(&:cnf?).reduce(:&)
45
115
  end
@@ -3,5 +3,9 @@ module Ravensat
3
3
  def ~@
4
4
  @children.first
5
5
  end
6
+
7
+ def to_dimacs
8
+ "-"
9
+ end
6
10
  end
7
11
  end
@@ -1,8 +1,17 @@
1
1
  module Ravensat
2
2
  class OrNode < OprNode
3
+ def |(object)
4
+ @children.append object
5
+ self
6
+ end
7
+
3
8
  def cnf?
4
9
  return false if @children.any?{|node| node.is_a? AndNode}
5
10
  @children.map(&:cnf?).reduce(:&)
6
11
  end
12
+
13
+ def to_dimacs
14
+ " "
15
+ end
7
16
  end
8
17
  end
@@ -1,9 +1,10 @@
1
1
  module Ravensat
2
2
  class VarNode < Node
3
- attr_accessor :value
3
+ attr_accessor :value, :dimacs_name
4
4
  def initialize
5
5
  @value
6
6
  @children = []
7
+ @dimacs_name
7
8
  end
8
9
 
9
10
  def ~@
@@ -17,5 +18,9 @@ module Ravensat
17
18
  def result
18
19
  @value
19
20
  end
21
+
22
+ def to_dimacs
23
+ @dimacs_name
24
+ end
20
25
  end
21
26
  end
data/lib/ravensat/claw.rb CHANGED
@@ -12,20 +12,20 @@ module Ravensat
12
12
 
13
13
  def self.commander_amo(bool_vars)
14
14
  m = bool_vars.size / 2
15
- introductory_variables = []
15
+ commander_variables = []
16
16
  formula = Ravensat::InitialNode.new
17
- bool_vars.each_slice(2) do |e|
17
+ bool_vars.each_slice(2) do |g|
18
18
  c = Ravensat::VarNode.new
19
- subset = e << ~c
19
+ subset = g << ~c
20
20
  formula &= pairwise_amo(subset)
21
21
  formula &= alo(subset)
22
- introductory_variables << c
22
+ commander_variables << c
23
23
  end
24
24
 
25
25
  if m < 6
26
- formula &= pairwise_amo(introductory_variables)
26
+ formula &= pairwise_amo(commander_variables)
27
27
  else
28
- formula &= commander_amo(introductory_variables)
28
+ formula &= commander_amo(commander_variables)
29
29
  end
30
30
  end
31
31
 
@@ -1,18 +1,21 @@
1
1
  module Ravensat
2
2
  class DimacsDecoder
3
- def decode(model, name_table)
4
- inverted_name_table = name_table.invert
3
+ def decode(model, cnf)
4
+ # inverted_name_table = name_table.invert
5
+ prop_vars = cnf.vars
5
6
  case model.first
6
7
  when "SAT"
7
8
  model.last.split.each do |e|
8
9
  if e == '0'
9
10
  next
10
11
  elsif e[0] == "-"
11
- index = e.slice(1..-1)
12
- inverted_name_table[index].value = false
12
+ dimacs_name = e.slice(1..-1)
13
+ var = prop_vars.find{|i| i.dimacs_name == dimacs_name}
14
+ var.value = false
13
15
  else
14
- index = e
15
- inverted_name_table[index].value = true
16
+ dimacs_name = e
17
+ var = prop_vars.find{|i| i.dimacs_name == dimacs_name}
18
+ var.value = true
16
19
  end
17
20
  end
18
21
  true
@@ -6,26 +6,23 @@ module Ravensat
6
6
  end
7
7
 
8
8
  def to_dimacs(formula)
9
- return nil unless formula.cnf?
10
-
11
9
  dimacs_header = "p cnf #{formula.vars_size} #{formula.clauses_size}\n"
12
10
  dimacs_body = ""
13
- create_table(formula)
14
- formula.each do |node|
15
- case node
16
- when AndNode then dimacs_body << " 0\n"
17
- when OrNode then dimacs_body << " "
18
- when NotNode then dimacs_body << "-"
19
- when VarNode then dimacs_body << @name_table[node]
20
- end
11
+ return nil unless formula.cnf?
12
+
13
+ set_dimacs_name(formula)
14
+
15
+ formula.each_DP do |node|
16
+ dimacs_body << node.to_dimacs
21
17
  end
22
- dimacs_body << " 0\n"
23
18
 
24
19
  dimacs_header + dimacs_body
25
20
  end
26
21
 
27
- def create_table(formula)
28
- @name_table = formula.vars.zip((1..formula.vars.size).map(&:to_s)).to_h
22
+ def set_dimacs_name(formula)
23
+ formula.vars.each_with_index do |node, i|
24
+ node.dimacs_name = (i+1).to_s
25
+ end
29
26
  end
30
27
  end
31
28
  end
@@ -24,7 +24,7 @@ module Ravensat
24
24
 
25
25
  decoder = DimacsDecoder.new
26
26
  model = @output_file.read.split("\n")
27
- decoder.decode(model, encoder.name_table)
27
+ decoder.decode(model, cnf)
28
28
  end
29
29
 
30
30
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ravensat
4
- VERSION = "0.3.2"
4
+ VERSION = "1.0.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ravensat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - rikuto matsuda
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-06-15 00:00:00.000000000 Z
11
+ date: 2022-07-11 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: