<commit_before># Copyright © Mapotempo, 2013-2014
#
# This file is part of Mapotempo.
#
# Mapotempo is free software. You can redistribute it and/or
# modify since you respect the terms of the GNU Affero General
# Public License as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any later version.
#
# Mapotempo is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the Licenses for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Mapotempo. If not, see:
# <http://www.gnu.org/licenses/agpl.html>
#
require 'json'
require 'tempfile'


get '/' do
  'optimizer-api'
end

post '/0.1/optimize_tsptw' do
  jdata = JSON.parse(params[:data])
  begin
    optim = optimize(jdata['capacity'], jdata['matrix'], jdata['time_window'], jdata['rest_window'], jdata['optimize_time'], jdata['soft_upper_bound'])
    if !optim
      puts "No optim result !"
      halt(500)
    end
    {status: :ok, optim: optim}.to_json
  rescue Exception => e
    puts e
    halt(500, e.to_s)
  end
end


def optimize(capacity, matrix, time_window, rest_window, optimize_time = nil, soft_upper_bound = nil)
  @exec = settings.optimizer_exec
  @tmp_dir = settings.optimizer_tmp_dir
  @time = optimize_time || settings.optimizer_default_time
  @soft_upper_bound = soft_upper_bound || settings.optimizer_soft_upper_bound

  input = Tempfile.new('optimize-route-input', tmpdir=@tmp_dir)
  output = Tempfile.new('optimize-route-output', tmpdir=@tmp_dir)

  begin
    output.close

    input.write(matrix.size)
    input.write("\n")
    input.write(rest_window.size)
    input.write("\n")
    input.write(matrix.collect{ |a| a.collect{ |b| b.join(" ") }.join(" ") }.join("\n"))
    input.write("\n")
    input.write((time_window + [[0, 2147483647, 0]]).collect{ |a| [a[0] ? a[0]:0, a[1]? a[1]:2147483647, a[2]].join(" ") }.join("\n"))
    input.write("\n")
    input.write(rest_window.collect{ |a| [a[0] ? a[0]:-2147483648, a[1]? a[1]:2147483647, a[2]].join(" ") }.join("\n"))
    input.write("\n")

    input.close

    cmd = "#{@exec} -time_limit_in_ms #{@time} -soft_upper_bound #{@soft_upper_bound} -nearby -instance_file '#{input.path}' > '#{output.path}'"
    puts cmd
    system(cmd)
    puts $?.exitstatus
    if $?.exitstatus == 0
      result = File.read(output.path)
      result = result.split("\n")[-1]
      puts result.inspect
      result.split(' ').collect{ |i| Integer(i) }
    end
  ensure
    input.unlink
    output.unlink
  end
end<commit_msg>Swith ruby code to double TW<commit_after># Copyright © Mapotempo, 2013-2014
#
# This file is part of Mapotempo.
#
# Mapotempo is free software. You can redistribute it and/or
# modify since you respect the terms of the GNU Affero General
# Public License as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any later version.
#
# Mapotempo is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the Licenses for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Mapotempo. If not, see:
# <http://www.gnu.org/licenses/agpl.html>
#
require 'json'
require 'tempfile'


get '/' do
  'optimizer-api'
end

post '/0.1/optimize_tsptw' do
  jdata = JSON.parse(params[:data])
  begin
    optim = optimize(jdata['capacity'], jdata['matrix'], jdata['time_window'], jdata['rest_window'], jdata['optimize_time'], jdata['soft_upper_bound'])
    if !optim
      puts "No optim result !"
      halt(500)
    end
    {status: :ok, optim: optim}.to_json
  rescue Exception => e
    puts e
    halt(500, e.to_s)
  end
end


def optimize(capacity, matrix, time_window, rest_window, optimize_time = nil, soft_upper_bound = nil)
  @exec = settings.optimizer_exec
  @tmp_dir = settings.optimizer_tmp_dir
  @time = optimize_time || settings.optimizer_default_time
  @soft_upper_bound = soft_upper_bound || settings.optimizer_soft_upper_bound

  input = Tempfile.new('optimize-route-input', tmpdir=@tmp_dir)
  output = Tempfile.new('optimize-route-output', tmpdir=@tmp_dir)

  begin
    output.close

    input.write(matrix.size)
    input.write("\n")
    input.write(rest_window.size)
    input.write("\n")
    input.write(matrix.collect{ |a| a.collect{ |b| b.join(" ") }.join(" ") }.join("\n"))
    input.write("\n")
    input.write((time_window + [[0, 2147483647, -2147483648, 2147483647, 0]]).collect{ |a| [a[0] ? a[0]:-2147483648, a[1]? a[1]:2147483647, a[2] ? a[2]:-2147483648, a[3]? a[3]:2147483647, a[4]].join(" ") }.join("\n"))
    input.write("\n")
    input.write(rest_window.collect{ |a| [a[0] ? a[0]:-2147483648, a[1]? a[1]:2147483647, a[2] ? a[2]:-2147483648, a[3]? a[3]:2147483647, a[4]].join(" ") }.join("\n"))
    input.write("\n")

    input.close

    cmd = "#{@exec} -time_limit_in_ms #{@time} -soft_upper_bound #{@soft_upper_bound} -nearby -instance_file '#{input.path}' > '#{output.path}'"
    puts cmd
    system(cmd)
    puts $?.exitstatus
    if $?.exitstatus == 0
      result = File.read(output.path)
      result = result.split("\n")[-1]
      puts result.inspect
      result.split(' ').collect{ |i| Integer(i) }
    end
  ensure
    input.unlink
    output.unlink
  end
end
{'fraction_non_alphanumeric': 0.15023818248442652, 'fraction_numerical': 0.04049102235251008, 'mean_word_length': 3.856761565836299, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4590657', 'n_tokens_mistral': 2045, 'n_tokens_neox': 1922, 'n_words': 502}