ankane / or-tools-ruby

Operations research tools for Ruby
Apache License 2.0
173 stars 22 forks source link

seg fault @ run solver. #4

Closed cokeito closed 4 years ago

cokeito commented 4 years ago

hey, im back again i've made a service file for testing:


def self.ejemplo
    # define the data
    num_nurses = 4
    num_shifts = 3
    num_days = 3
    all_nurses = num_nurses.times.to_a
    all_shifts = num_shifts.times.to_a
    all_days = num_days.times.to_a

    # create the variables
    model = ORTools::CpModel.new

    shifts = {}
    all_nurses.each do |n|
      all_days.each do |d|
        all_shifts.each do |s|
          shifts[[n, d, s]] = model.new_bool_var("shift_n%id%is%i" % [n, d, s])
        end
      end
    end

    # assign nurses to shifts
    all_days.each do |d|
      all_shifts.each do |s|
        model.add(model.sum(all_nurses.map { |n| shifts[[n, d, s]] }) == 1)
      end
    end

    all_nurses.each do |n|
      all_days.each do |d|
        model.add(model.sum(all_shifts.map { |s| shifts[[n, d, s]] }) <= 1)
      end
    end

    # assign shifts evenly
    min_shifts_per_nurse = (num_shifts * num_days) / num_nurses
    max_shifts_per_nurse = min_shifts_per_nurse + 1
    all_nurses.each do |n|
      num_shifts_worked = model.sum(all_days.flat_map { |d| all_shifts.map { |s| shifts[[n, d, s]] } })
      model.add(num_shifts_worked >= min_shifts_per_nurse)
      model.add(num_shifts_worked <= max_shifts_per_nurse)
    end

    # call the solver and display the results
    solver = ORTools::CpSolver.new

    a_few_solutions = 5.times.to_a
    solution_printer = NursesPartialSolutionPrinter.new(
      shifts, num_nurses, num_days, num_shifts, a_few_solutions
      )
    solver.search_for_all_solutions(model,solution_printer)

    puts
    puts "Statistics"
    puts "  - conflicts       : %i" % solver.num_conflicts
    puts "  - branches        : %i" % solver.num_branches
    puts "  - wall time       : %f s" % solver.wall_time
    puts "  - solutions found : %i" % solution_printer.solution_count
  end

3] pry(main)> DevelService.ejemplo /usr/local/rvm/gems/ruby-2.4.1/gems/or-tools-0.2.0/lib/or_tools/cp_solver.rb:24: [BUG] Segmentation fault at 0x00000000000000 ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]

-- Control frame information ----------------------------------------------- c:0057 p:---- s:0338 e:000337 CFUNC :_solve_with_observer c:0056 p:0014 s:0331 e:000330 METHOD /usr/local/rvm/gems/ruby-2.4.1/gems/or-tools-0.2.0/lib/or_tools/cp_solver.rb:24 c:0055 p:0202 s:0325 E:001988 METHOD /home/coke/_prj/turnos-copec/app/services/devel_service.rb:125 c:0054 p:0012 s:0308 E:0019f0 EVAL (pry):3 [FINISH] c:0053 p:---- s:0305 e:000304 CFUNC :eval c:0052 p:0052 s:0298 E:001a70 METHOD /usr/local/rvm/gems/ruby-2.4.1/gems/pry-0.13.1/lib/pry/pry_instance.rb:290 c:0051 p:0292 s:0292 E:001b18 METHOD /usr/local/rvm/gems/ruby-2.4.1/gems/pry-0.13.1/lib/pry/pry_instance.rb:659 c:0050 p:0014 s:0281 E:001c00 BLOCK /usr/local/rvm/gems/ruby-2.4.1/gems/pry-0.13.1/lib/pry/pry_instance.rb:261 [FINISH]

cokeito commented 4 years ago

can you provide a working example (repo to test) to test this example, and discard an error from my side.

cheers

ankane commented 4 years ago

Hey @cokeito, all of the examples in the readme should work. Are you able to run any examples successfully?

cokeito commented 4 years ago

Yep, the flow one ran without problem, i tried to run the Sodoku one and doesn't work neither. i was trying to debug last nite, if u run the solver(mode). it doesn't crash

solver.search_for_all_solutions(model,solution_printer), dies with segfault.

letme know what can i do to provide help for you

ps2: i've tried to run the sodoku example, and got this:

image

def self.sodoku_test
    grid = [
      [0, 6, 0, 0, 5, 0, 0, 2, 0],
      [0, 0, 0, 3, 0, 0, 0, 9, 0],
      [7, 0, 0, 6, 0, 0, 0, 1, 0],
      [0, 0, 6, 0, 3, 0, 4, 0, 0],
      [0, 0, 4, 0, 7, 0, 1, 0, 0],
      [0, 0, 5, 0, 9, 0, 8, 0, 0],
      [0, 4, 0, 0, 0, 1, 0, 0, 6],
      [0, 3, 0, 0, 0, 8, 0, 0, 0],
      [0, 2, 0, 0, 4, 0, 0, 5, 0]
    ]
    sudoku = ORTools::Sudoku.new(grid)
    sudoku.solution
  end
ankane commented 4 years ago

The Sudoku class is only available on the master branch. You can try cloning the repo and running the test suite.

git clone https://github.com/ankane/or-tools.git
cd or-tools
bundle install
bundle exec rake clobber compile -- --with-or-tools-dir=/path/to/or-tools-cpp-library
bundle exec rake test

Also, make sure the OR-Tools C++ library is 7.7.

ankane commented 4 years ago

Cleaning up stale issues