<commit_before># -*- coding:utf-8 -*-
import random
import model
import genome
import copy
import itertools

def init(ind_class, size, nb_vehicle, data):
    """
    Initialisation operator. Fill the inidividual's first part with a random permutation of
    appointement and compute the second part with a valid vehicle count.
    """
    remaining_size = size 
    second_part = []
    first_part = []

    # Create the second part of the individual
    # Choose random value while checking the upper bound
    for i in range(0, nb_vehicle):
        vehicle_capacity = random.randrange(0, (size / nb_vehicle) * 2)
        vehicle_capacity %= remaining_size
        remaining_size -= vehicle_capacity
        second_part.append(vehicle_capacity)

    # If there is still unassignated vehicles at the end
    # Assigned them to the last vehicle
    if remaining_size > 0:
        second_part[-1] += remaining_size

    # Create the first part of the chromosome 
    # Just a random permutation of indexes
    first_part = range(0, size)
    random.shuffle(first_part)

    mroute = []
    
    offset = 0

    for vehicle_size in second_part:
        route = []
        for idx in range(offset, vehicle_size+offset):
            route = insert_appointment1D(route, first_part[idx], data)
        mroute.append(first_part[offset:offset+vehicle_size])
        offset += vehicle_size

    first_part = list(itertools.chain(*mroute))

    # Create the individual and return it
    ind = ind_class((first_part, second_part))
    return ind


def evaluate(individual, data, depot, size):
    """
    Evaluate the genome.
    """
    splitted_route = []
    idx = 0
    capacity_per_vehicle = 5
    distance = 0
    load = 0
    appointment_missing_malus = 10

    # Split the first part of the individual as a list of route using the second part
    for vehicle_size in individual.vehicles:
        splitted_route.append(individual.routes[idx:vehicle_size])
        idx += vehicle_size

    # For each vehicle (route)
    # Compute the distance travelled and the load 
    for route in splitted_route:
        if len(route) > 0:
            distance += model.euclidian_distance(depot, data[route[0]])
            for gene1, gene2 in zip(route[0:-1], route[1:]):
                distance += model.euclidian_distance(data[gene1], data[gene2])

            for gene1, gene2 in zip(route[0:-1], route[1:]):
                load += 1

            load -= capacity_per_vehicle

    distance += (size-len(individual.vehicles))*appointment_missing_malus

    # Return a tuple of fitness: the cost and the load 
    return distance, load

def window_bounds_checking(data1, data2):
    """
    Checks whether data2 is contained within data1.
    This function is used during insertion, and is therefore
    solely called in order to check whether inserting data1<commit_msg>Set malus in case of time window constraint violation<commit_after># -*- coding:utf-8 -*-
import random
import model
import genome
import copy
import itertools

def init(ind_class, size, nb_vehicle, data):
    """
    Initialisation operator. Fill the inidividual's first part with a random permutation of
    appointement and compute the second part with a valid vehicle count.
    """
    remaining_size = size 
    second_part = []
    first_part = []

    # Create the second part of the individual
    # Choose random value while checking the upper bound
    for i in range(0, nb_vehicle):
        vehicle_capacity = random.randrange(0, (size / nb_vehicle) * 2)
        vehicle_capacity %= remaining_size
        remaining_size -= vehicle_capacity
        second_part.append(vehicle_capacity)

    # If there is still unassignated vehicles at the end
    # Assigned them to the last vehicle
    if remaining_size > 0:
        second_part[-1] += remaining_size

    # Create the first part of the chromosome 
    # Just a random permutation of indexes
    first_part = range(0, size)
    random.shuffle(first_part)

    mroute = []
    
    offset = 0
    
    #
    #for vehicle_size in second_part:
    #    route = []
    #    for idx in range(offset, vehicle_size+offset):
    #        route = insert_appointment1D(route, first_part[idx], data)
    #    mroute.append(first_part[offset:offset+vehicle_size])
    #    offset += vehicle_size

    #first_part = list(itertools.chain(*mroute))

    # Create the individual and return it
    ind = ind_class((first_part, second_part))
    return ind


def evaluate(individual, data, depot, size):
    """
    Evaluate the genome.
    """
    splitted_route = []
    idx = 0
    capacity_per_vehicle = 5
    distance = 0
    load = 0
    malus_appointment_missing = 10
    malus_conflict = 1000
    
    # Split the first part of the individual as a list of route using the second part
    for vehicle_size in individual.vehicles:
        splitted_route.append(individual.routes[idx:vehicle_size])
        idx += vehicle_size

    # For each vehicle (route)
    # Compute the distance travelled and the load 
    for route in splitted_route:
        if len(route) > 0:
            distance += model.euclidian_distance(depot, data[route[0]])
            for gene1, gene2 in zip(route[0:-1], route[1:]):
                distance += model.euclidian_distance(data[gene1], data[gene2])

            for gene1, gene2 in zip(route[0:-1], route[1:]):
                load += 1

            load -= capacity_per_vehicle

    # Set the different feasability malus
    distance += (size - len(individual.vehicles)) * malus_appointment_missing
    distance += individual.is_time_constraint_respected(data) * malus_conflict
    
    # Return a tuple of fitness: the cost and the load 
    return distance, load

def window_bounds_checking(data1, data2):
    """
    Checks whether data2 is contained within data1.
    This function is used during insertion, and is therefore
    solely called in order to check whether inserting data1
{'fraction_non_alphanumeric': 0.07959527824620573, 'fraction_numerical': 0.012478920741989882, 'mean_word_length': 2.843810758263124, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '2071306', 'n_tokens_mistral': 1785, 'n_tokens_neox': 1661, 'n_words': 557}