Class: Whatnot::DimacsCNFPrinter

Inherits:
Object
  • Object
show all
Defined in:
lib/whatnot/dimacs_cnf_printer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ DimacsCNFPrinter

Returns a new instance of DimacsCNFPrinter.



5
6
7
8
9
# File 'lib/whatnot/dimacs_cnf_printer.rb', line 5

def initialize(*args)
  @vars = {}
  @vars_total = 1
  @file = File.open("solver-out.txt", "w+")
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



11
12
13
# File 'lib/whatnot/dimacs_cnf_printer.rb', line 11

def file
  @file
end

Instance Method Details

#all_different(varnames) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/whatnot/dimacs_cnf_printer.rb', line 56

def all_different(varnames)
  puts "c -------------"
  puts "c all_different..."
  puts "c varnames: #{varnames.inspect}"

  varnames.combination(2).each do |varname1, varname2|
    var1 = @vars[varname1]
    var2 = @vars[varname2]

    var1.matching_pairs(var2).each do |key1, key2|
      puts "-#{key1} -#{key2} 0"
    end
  end
  puts "c -------------"

end

#all_pairs(vars, &block) ⇒ Object



103
104
105
106
107
# File 'lib/whatnot/dimacs_cnf_printer.rb', line 103

def all_pairs(vars, &block)
  vars.combination(2).each do |var1, var2|
    constrain(var1, var2, &block)
  end
end

#constrain(*varnames) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/whatnot/dimacs_cnf_printer.rb', line 73

def constrain(*varnames)
  puts "c -------------"
  puts "c constraining..."
  puts "c varnames: #{varnames.inspect}"

  argument_sets = nil
  varnames.each do |varname|
    argument_set = @vars[varname].argument_set()

    if argument_sets.nil?
      argument_sets ||= argument_set
    else
      argument_sets = argument_sets.product(argument_set).map(&:flatten)
    end
  end

  argument_sets.each do |argument_set|
    arguments_to_constraint = argument_set.values_at(* argument_set.each_index.select {|i| i.even?})
    key_set =                 argument_set.values_at(* argument_set.each_index.select {|i| i.odd?})

    result = yield(*arguments_to_constraint)

    if !result
      puts "#{key_set.map { |k| "-" + k.to_s }.join(" ")} 0"
    end
  end

  puts "c -------------"
end

#puts(str = "") ⇒ Object



13
14
15
# File 'lib/whatnot/dimacs_cnf_printer.rb', line 13

def puts(str="")
  @file << str.chomp + "\n"
end

#solveObject



109
110
111
112
113
114
# File 'lib/whatnot/dimacs_cnf_printer.rb', line 109

def solve
  `minisat solver-out.txt minisat-out.txt`

  solution = File.read("minisat-out.txt").lines[1]
  DimacsCNFVar.interpret(solution)
end

#value_name_from_pair(name, value) ⇒ Object



17
18
19
# File 'lib/whatnot/dimacs_cnf_printer.rb', line 17

def value_name_from_pair(name, value)
  "#{name}=#{value.to_json}"
end

#var(name, domain) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/whatnot/dimacs_cnf_printer.rb', line 27

def var(name, domain)
  puts "c -------------"
  puts "c uniquifying..."
  puts "c var:    #{name.inspect}"

  iter1 = true
  JSON.pretty_generate(domain).each_line do |slice|
    prefix = iter1 ? "c domain: " : "c         "
    iter1 = false
    puts "#{prefix}#{slice}"
  end

  new_var = DimacsCNFVar.new(name, domain, key_iter: @vars_total)
  @vars_total = new_var.key_iter()

  # it must be at least one of the possible values
  puts "#{new_var.all_keys_as_array().join(" ")} 0"

  # it can't be two values
  new_var.all_keys_as_array().combination(2).each do |key1, key2|
    puts "-#{key1} -#{key2} 0"
  end

  @vars[name] = new_var

  puts "c -------------"
  puts
end

#vars(names, domain) ⇒ Object



21
22
23
24
25
# File 'lib/whatnot/dimacs_cnf_printer.rb', line 21

def vars(names, domain)
  names.each do |name|
    var(name, domain)
  end
end