<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json, sys
from utils.file import load_json
from utils.osrm import table

# Parse a json-formatted input instance, compute the matrix using
# OSRM, then add the matrix and all relevant indices to the input
# problem. Possible usage include checking that solving is consistent
# between both instances, or creating a "standalone" problem instance
# that can be further solved even without an OSRM server handy.

def round_to_cost(d):
  return int(d + 0.5)

def get_index(locations, locations_indices, loc):
  new_index = len(locations)

  lon_str = str(loc[0])
  lat_str = str(loc[1])

  if lon_str in locations_indices:
    if lat_str in locations_indices[lon_str]:
      return locations_indices[lon_str][lat_str]
    else:
      locations_indices[lon_str][lat_str] = new_index
  else:
    locations_indices[lon_str] = {lat_str: new_index}

  # loc is not already listed in locations.
  locations.append(loc)
  return new_index

if __name__ == "__main__":
  input_file = sys.argv[1]
  output_name = input_file[:input_file.rfind('.json')] + '_matrix.json'

  data = load_json(input_file)

  # Retrieve all problem locations in the same order as in
  # input_parser.cpp.
  locs = []
  index_of_known_locations = {}

  for v in data['vehicles']:
    if ('start' not in v) and ('end' not in v):
      sys.exit("Missing coordinates for vehicle.")

    if 'start' in v:
      v['start_index'] = get_index(locs, index_of_known_locations, v['start'])
    if 'end' in v:
      v['end_index'] = get_index(locs, index_of_known_locations, v['end'])

  for job in data['jobs']:
    if ('location' not in job):
      sys.exit("Missing coordinates for job.")
    else:
      job['location_index'] = get_index(locs, index_of_known_locations, job['location'])

  for shipment in data['shipments']:
    if ('location' not in shipment['pickup']):
      sys.exit("Missing coordinates for shipment pickup.")
    else:
      shipment['pickup']['location_index'] = get_index(locs, index_of_known_locations, shipment['pickup']['location'])

    if ('location' not in shipment['delivery']):
      sys.exit("Missing coordinates for shipment delivery.")
    else:
      shipment['delivery']['location_index'] = get_index(locs, index_of_known_locations, shipment['delivery']['location'])

  # Get table from OSRM.
  matrix = table(locs)['durations']
  data['matrix'] = []

  # Round all costs to the nearest integer (same behavior as in
  # osrm_wrapper.h)
  for line in matrix:
    data['matrix'].append(map(lambda d: round_to_cost(d), line))

  with open(output_name, 'w') as out:
    print('Writing problem with matrix to ' + output_name)
    json.dump(data, out, indent = 2)
<commit_msg>Handle cases without jobs or shipments for add_osrm_matrix script.<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json, sys
from utils.file import load_json
from utils.osrm import table

# Parse a json-formatted input instance, compute the matrix using
# OSRM, then add the matrix and all relevant indices to the input
# problem. Possible usage include checking that solving is consistent
# between both instances, or creating a "standalone" problem instance
# that can be further solved even without an OSRM server handy.

def round_to_cost(d):
  return int(d + 0.5)

def get_index(locations, locations_indices, loc):
  new_index = len(locations)

  lon_str = str(loc[0])
  lat_str = str(loc[1])

  if lon_str in locations_indices:
    if lat_str in locations_indices[lon_str]:
      return locations_indices[lon_str][lat_str]
    else:
      locations_indices[lon_str][lat_str] = new_index
  else:
    locations_indices[lon_str] = {lat_str: new_index}

  # loc is not already listed in locations.
  locations.append(loc)
  return new_index

if __name__ == "__main__":
  input_file = sys.argv[1]
  output_name = input_file[:input_file.rfind('.json')] + '_matrix.json'

  data = load_json(input_file)

  # Retrieve all problem locations in the same order as in
  # input_parser.cpp.
  locs = []
  index_of_known_locations = {}

  for v in data['vehicles']:
    if ('start' not in v) and ('end' not in v):
      sys.exit("Missing coordinates for vehicle.")

    if 'start' in v:
      v['start_index'] = get_index(locs, index_of_known_locations, v['start'])
    if 'end' in v:
      v['end_index'] = get_index(locs, index_of_known_locations, v['end'])

  if ('jobs' in data):
    for job in data['jobs']:
      if ('location' not in job):
        sys.exit("Missing coordinates for job.")
      else:
        job['location_index'] = get_index(locs, index_of_known_locations, job['location'])

  if ('shipments' in data):
    for shipment in data['shipments']:
      if ('location' not in shipment['pickup']):
        sys.exit("Missing coordinates for shipment pickup.")
      else:
        shipment['pickup']['location_index'] = get_index(locs, index_of_known_locations, shipment['pickup']['location'])

      if ('location' not in shipment['delivery']):
        sys.exit("Missing coordinates for shipment delivery.")
      else:
        shipment['delivery']['location_index'] = get_index(locs, index_of_known_locations, shipment['delivery']['location'])

  # Get table from OSRM.
  matrix = table(locs)['durations']
  data['matrix'] = []

  # Round all costs to the nearest integer (same behavior as in
  # osrm_wrapper.h)
  for line in matrix:
    data['matrix'].append(map(lambda d: round_to_cost(d), line))

  with open(output_name, 'w') as out:
    print('Writing problem with matrix to ' + output_name)
    json.dump(data, out, indent = 2)

{'fraction_non_alphanumeric': 0.12102252413299965, 'fraction_numerical': 0.0025026814444047193, 'mean_word_length': 4.067934782608695, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '318413', 'n_tokens_mistral': 1817, 'n_tokens_neox': 1750, 'n_words': 532}