ravensat 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4096e2cf92f90de177d68da91023a52126e1c585ecb4adb4bebd03f98d4d7eb2
4
+ data.tar.gz: ed1650dda444677d788404feb1a08af0c95d11adcc9ed50863fdd4148784da63
5
+ SHA512:
6
+ metadata.gz: ec26889a68bf4114d3bf0ed6c56c2e8368b31e09ac1865f31272f70bdc1415f319b291f677bfdf730f71b1ed9bcc1e56ffabddbcdbcba1a11c6441e64d888bd1
7
+ data.tar.gz: b174cd6db0867ab511a558a19a23a372cab7d293429ed434c4892817505f6a6d12cc1e15f2a67431bfa52c7376575b8a057ba536c7315fe14fc452d7a71fcd25
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ .ruby-version
10
+ Gemfile.lock
11
+
12
+ test.rb
13
+ # rspec failure tracking
14
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.0.0
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in ravensat.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "pry"
13
+ gem "pry-byebug"
data/Gemfile.lock ADDED
@@ -0,0 +1,45 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ravensat (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ byebug (11.1.3)
10
+ coderay (1.1.3)
11
+ diff-lcs (1.4.4)
12
+ method_source (1.0.0)
13
+ pry (0.14.0)
14
+ coderay (~> 1.1)
15
+ method_source (~> 1.0)
16
+ pry-byebug (3.8.0)
17
+ byebug (~> 11.0)
18
+ pry (~> 0.10)
19
+ rake (13.0.3)
20
+ rspec (3.10.0)
21
+ rspec-core (~> 3.10.0)
22
+ rspec-expectations (~> 3.10.0)
23
+ rspec-mocks (~> 3.10.0)
24
+ rspec-core (3.10.1)
25
+ rspec-support (~> 3.10.0)
26
+ rspec-expectations (3.10.1)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.10.0)
29
+ rspec-mocks (3.10.2)
30
+ diff-lcs (>= 1.2.0, < 2.0)
31
+ rspec-support (~> 3.10.0)
32
+ rspec-support (3.10.2)
33
+
34
+ PLATFORMS
35
+ x86_64-linux
36
+
37
+ DEPENDENCIES
38
+ pry
39
+ pry-byebug
40
+ rake (~> 13.0)
41
+ ravensat!
42
+ rspec (~> 3.0)
43
+
44
+ BUNDLED WITH
45
+ 2.2.24
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 rikuto matsuda
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # Ravensat
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ravensat`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation(Not implemented)
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'ravensat'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install ravensat
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ require 'ravensat'
27
+
28
+ a = Ravensat::PropVar.new
29
+ b = Ravensat::PropVar.new
30
+
31
+ a.value #=> nil
32
+ b.value #=> nil
33
+
34
+ logic = (a | b) & (~a | b) & (a | ~b)
35
+
36
+ solver = Ravensat::Solver.new
37
+ solver.solve logic #=> true(SAT)
38
+
39
+ a.value #=> true
40
+ b.value #=> true
41
+ ```
42
+
43
+ ## Development(Not implemented)
44
+
45
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
46
+
47
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
48
+
49
+ ## Contributing
50
+
51
+ Bug reports and pull requests are welcome on GitHub at https://github.com/matsuda0528/ravensat.
52
+
53
+ ## License
54
+
55
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "ravensat"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ require "pry"
12
+ Pry.start
13
+
14
+ # require "irb"
15
+ # IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,40 @@
1
+ require_relative './cnf.rb'
2
+ module Arcteryx
3
+ SAT = true
4
+ UNSAT = false
5
+
6
+ def DPLL(cnf)
7
+ if cnf.empty? then return SAT end
8
+ cnf.unit_propagation
9
+ if cnf.exist_empty_clause? then return UNSAT end
10
+ x = cnf.choose_variable
11
+ dup_cnf = cnf.deep_dup
12
+ return DPLL(cnf.append x) || DPLL(dup_cnf.append -1*x)
13
+ end
14
+
15
+ def solve( input_file, output_file = nil )
16
+ cnf = Arcteryx::CNF.new
17
+ cnf.parse(input_file)
18
+ case DPLL(cnf)
19
+ when SAT
20
+ if output_file
21
+ File.open(output_file,"w") do |f|
22
+ f.write("SAT\n"+cnf.result)
23
+ end
24
+ else
25
+ puts "SAT"
26
+ puts cnf.result
27
+ end
28
+ when UNSAT
29
+ if output_file
30
+ File.open(output_file,"w") do |f|
31
+ f.write("UNSAT")
32
+ end
33
+ else
34
+ puts "UNSAT"
35
+ end
36
+ end
37
+ end
38
+ module_function :solve
39
+ module_function :DPLL
40
+ end
@@ -0,0 +1,74 @@
1
+ module Arcteryx
2
+ class CNF
3
+ def initialize(formula=[],truth={})
4
+ @formula = formula
5
+ @truth_assignment = truth
6
+ end
7
+
8
+ def parse(file_path)
9
+ header = ""; clauses = [];
10
+ File.open(file_path,"r") do |f|
11
+ header = f.readline
12
+ clauses = f.readlines
13
+ end
14
+ header.split[2].to_i.times do |i|
15
+ @truth_assignment["#{i+1}".to_sym] = nil
16
+ end
17
+ clauses.each do |e|
18
+ clause = e.split.map{|i| i.to_i}
19
+ clause.pop
20
+ @formula.append clause
21
+ end
22
+ end
23
+
24
+ def unit_propagation
25
+ while !self.exist_empty_clause? and l = self.find_unit_clause
26
+ @truth_assignment["#{l.abs}".to_sym] = l > 0
27
+ self.simplify(l)
28
+ end
29
+ end
30
+
31
+ def simplify(l)
32
+ @formula.delete_if do |cls|
33
+ cls.include?(l)
34
+ end
35
+ @formula.map{|cls| if cls.include?(-1*l) then cls.delete(-1*l) end}
36
+ end
37
+
38
+ def empty?
39
+ @formula.empty?
40
+ end
41
+
42
+ def find_unit_clause
43
+ @formula.map{|cls| if cls.size == 1 then return cls.first end}
44
+ return false
45
+ end
46
+
47
+ def exist_empty_clause?
48
+ @formula.map{|cls| if cls.empty? then return true end}
49
+ return false
50
+ end
51
+
52
+ def choose_variable
53
+ @truth_assignment.map{|key,value| if value == nil then return key.to_s.to_i end}
54
+ return false
55
+ end
56
+
57
+ def append l
58
+ @formula.append [l] if l
59
+ return self
60
+ end
61
+
62
+ def deep_dup
63
+ tmp_formula = Marshal.load(Marshal.dump(@formula))
64
+ tmp_truth = Marshal.load(Marshal.dump(@truth_assignment))
65
+ CNF.new(tmp_formula,tmp_truth)
66
+ end
67
+
68
+ def result
69
+ str = ""
70
+ @truth_assignment.map{|key,value| unless value then str<<"-" end; str<<key.to_s+" "}
71
+ return str << "0"
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,92 @@
1
+ module Ravensat
2
+ class PropLogic
3
+ attr_reader :formula
4
+ attr_reader :name_table
5
+
6
+ def initialize( init_formula )
7
+ @formula = init_formula
8
+ # (a | b) & (~a | b) & (a | ~b) & (~a | ~b)
9
+
10
+ # [:and,
11
+ # [:or, a, b],
12
+ # [:or, [:not, a], b],
13
+ # [:or, a, [:not, b]],
14
+ # [:or, [:not, a], [:not, b]]
15
+ # ]
16
+ end
17
+
18
+ def to_cnf
19
+
20
+ end
21
+
22
+ def to_dimacs
23
+ cnf_text = String.new
24
+ tmp_name_table = @formula.flatten.uniq.reject{|e| e.class == Symbol}
25
+ @name_table = tmp_name_table.zip((1..tmp_name_table.size).to_a.map{|e| e.to_s}).to_h
26
+
27
+ nr_vars = tmp_name_table.size
28
+ nr_clses = @formula.size - 1
29
+ cnf_header = 'p cnf ' << nr_vars.to_s << ' ' << nr_clses.to_s << "\n"
30
+ # @formula.is_cnf?
31
+ @formula.each do |clause|
32
+ # next if clause.first != :and
33
+ case clause
34
+ when Symbol
35
+ next
36
+ when Array
37
+ case clause.first
38
+ when :or
39
+ clause.each do |literal|
40
+ case literal
41
+ when Symbol
42
+ next
43
+ when Ravensat::PropVar
44
+ cnf_text << @name_table[literal] << ' '
45
+ when Array #&& literal.first == :not
46
+ cnf_text << '-' << @name_table[literal.last] << ' '
47
+ end
48
+ end
49
+ when :not
50
+ cnf_text << '-' << @name_table[clause.last] << ' '
51
+ end
52
+ when Ravensat::PropVar
53
+ cnf_text << @name_table[clause] << ' '
54
+ end
55
+ cnf_text << '0' << "\n"
56
+ end
57
+ cnf_header + cnf_text
58
+ end
59
+
60
+ def &( object )
61
+ if @formula.first == :and
62
+ @formula.append object.formula
63
+ else
64
+ @formula = [:and, @formula, object.formula]
65
+ end
66
+ self
67
+ # Ravensat::PropLogic.new [:and, @formula, object]
68
+ # formulaがcnfである前提の実装
69
+ # @formula & other_formula
70
+ end
71
+
72
+ def |( object )
73
+ if @formula.first == :or
74
+ @formula.append object.formula
75
+ else
76
+ @formula = [:or, @formula, object.formula]
77
+ end
78
+ self
79
+ # Ravensat::PropLogic.new [:or, @formula, object]
80
+ # 木構造に落とすのが難しそう,実装見送り
81
+ # @formula | other_formula
82
+ end
83
+
84
+ # def to_a
85
+ # @formula.each do |f|
86
+ # if f.class == PropLogic
87
+ # f = f.to_a
88
+ # end
89
+ # end
90
+ # end
91
+ end
92
+ end
@@ -0,0 +1,45 @@
1
+ module Ravensat
2
+ class PropVar
3
+ attr_accessor :value
4
+
5
+ def initialize
6
+ @value # => true | false | undef
7
+ end
8
+
9
+ # def +@
10
+ # # unuse?
11
+ # # return PropLogic object
12
+ # 'this is +@ method'
13
+ # end
14
+
15
+ # def -@
16
+ # # return PropLogic object
17
+ # 'this is -@ method'
18
+ # end
19
+
20
+ def ~@
21
+ Ravensat::PropLogic.new [:not, self]
22
+ # return PropLogic object
23
+ end
24
+
25
+ def &( object )
26
+ Ravensat::PropLogic.new [:and, self, object.formula]
27
+ # return PropLogic object
28
+ end
29
+
30
+ def |( object )
31
+ Ravensat::PropLogic.new [:or, self, object.formula]
32
+ # return PropLogic object
33
+ end
34
+
35
+ def formula
36
+ # $BIU$1>F?O%a%=%C%I(B
37
+ self
38
+ end
39
+
40
+ # def self
41
+ # 'this is self method'
42
+ # end
43
+
44
+ end
45
+ end
@@ -0,0 +1,5 @@
1
+ def amo(proposal_values)
2
+ end
3
+
4
+ def alo(proposal_values)
5
+ end
@@ -0,0 +1,46 @@
1
+ require 'tempfile'
2
+
3
+ module Ravensat
4
+ class Solver
5
+ attr_accessor :name
6
+ def initialize( default_solver_name = "arcteryx" )
7
+ @name = default_solver_name
8
+ # @cnf = Array.new
9
+ # @nr_vars
10
+ # @nr_clses
11
+ end
12
+
13
+ # def <<( clause )
14
+ # 'this is << method'
15
+ # end
16
+
17
+ def solve( cnf )
18
+ @input_file = Tempfile.open(["ravensat",".cnf"])
19
+ @output_file = Tempfile.open(["ravensat",".mdl"])
20
+
21
+ @input_file.write cnf.to_dimacs
22
+ @input_file.flush
23
+
24
+ case @name
25
+ when "arcteryx"
26
+ Arcteryx.solve(@input_file.to_path, @output_file.to_path)
27
+ else
28
+ system("#{@name} #{@input_file.to_path} #{@output_file.to_path}")
29
+ end
30
+
31
+ model = @output_file.read.split("\n")
32
+
33
+ case model.first
34
+ when "SAT"
35
+ model.last.split.each do |e|
36
+ next if e == '0'
37
+ cnf.name_table.find{|key,value| value == e.to_i.abs.to_s}.first.value = !e.start_with?('-')
38
+ end
39
+ Arcteryx::SAT
40
+ when "UNSAT"
41
+ Arcteryx::UNSAT
42
+ end
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ravensat
4
+ VERSION = "0.1.0"
5
+ end
data/lib/ravensat.rb ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "ravensat/version"
4
+
5
+ module Ravensat
6
+ ravensat = File.dirname(__FILE__) + '/ravensat'
7
+ arcteryx = File.dirname(__FILE__) + '/arcteryx'
8
+
9
+ autoload :Solver, ravensat + '/solver.rb'
10
+ autoload :PropVar, ravensat + '/prop_var.rb'
11
+ autoload :PropLogic, ravensat + '/prop_logic.rb'
12
+
13
+ autoload :Arcteryx, arcteryx + '/arcteryx.rb'
14
+ end
data/ravensat.gemspec ADDED
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/ravensat/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "ravensat"
7
+ spec.version = Ravensat::VERSION
8
+ spec.authors = ["rikuto matsuda"]
9
+ spec.email = ["p99y92og@s.okayama-u.ac.jp"]
10
+
11
+ spec.summary = "An interface to the SAT solver for Ruby"
12
+ # spec.description = "TODO: Write a longer description or delete this line."
13
+ spec.homepage = "https://github.com/matsuda0528/ravensat"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
16
+
17
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
18
+
19
+ # spec.metadata["homepage_uri"] = spec.homepage
20
+ # spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
21
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ # Uncomment to register a new dependency of your gem
33
+ # spec.add_dependency "example-gem", "~> 1.0"
34
+
35
+ # For more information and examples about making a new gem, checkout our
36
+ # guide at: https://bundler.io/guides/creating_gem.html
37
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ravensat
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - rikuto matsuda
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-12-03 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - p99y92og@s.okayama-u.ac.jp
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".rspec"
22
+ - ".ruby-version"
23
+ - Gemfile
24
+ - Gemfile.lock
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - bin/console
29
+ - bin/setup
30
+ - lib/arcteryx/arcteryx.rb
31
+ - lib/arcteryx/cnf.rb
32
+ - lib/ravensat.rb
33
+ - lib/ravensat/prop_logic.rb
34
+ - lib/ravensat/prop_var.rb
35
+ - lib/ravensat/ravenclaw.rb
36
+ - lib/ravensat/solver.rb
37
+ - lib/ravensat/version.rb
38
+ - ravensat.gemspec
39
+ homepage: https://github.com/matsuda0528/ravensat
40
+ licenses:
41
+ - MIT
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: 2.4.0
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubygems_version: 3.2.3
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: An interface to the SAT solver for Ruby
62
+ test_files: []