Example #1
0
def fitness(individual: IndividualType, fleet: Fleet,
            hp: AlphaGA_HyperParameters):
    """
    Positive fitness of the individual.
    :param individual: the individual to evaluate
    :param fleet: fleet instance
    :param hp: GA hyper-parameters
    :return: fitness value of the individual (positive)
    """

    # Decode
    routes = decode(individual, fleet, hp)

    # Set routes
    fleet.set_routes_of_vehicles(routes)

    # Iterate
    fleet.iterate()

    # Cost
    costs = np.array(fleet.cost_function()
                     )  # cost_tt, cost_ec, cost_chg_time, cost_chg_cost

    # Calculate penalization
    feasible, distance, accept = fleet.feasible()

    if distance > 0.:
        penalization = distance + hp.hard_penalization
    else:
        penalization = 0

    # Calculate fitness
    fit = np.dot(costs, np.asarray(hp.weights)) + penalization

    return fit, feasible, accept
Example #2
0
def fitness(individual: IndividualType, indices: IndicesType, init_state: Dict,
            fleet: Fleet, hp: HyperParameters):
    """
    Positive fitness of the individual.
    :param individual: the individual to evaluate
    :param fleet: fleet instance
    :param hp: GA hyper-parameters
    :return: fitness value of the individual (positive)
    """

    # Decode
    routes = decode(individual, indices, init_state, fleet)

    # Set routes
    fleet.set_routes_of_vehicles(routes)

    # Cost
    costs = np.array(fleet.cost_function())

    # Calculate penalization
    feasible, distance, accept = fleet.feasible()

    penalization = distance + hp.hard_penalization if fleet.deterministic else distance

    # Calculate fitness
    fit = np.dot(costs, np.asarray(hp.weights)) + penalization

    return fit, feasible, accept
Example #3
0
def fitness(individual: IndividualType, indices: IndicesType,
            critical_points: Dict, fleet: Fleet, hp: HyperParameters):
    """
    Calculates fitness of individual.
    """

    # Decode
    m = len(fleet.vehicles)
    routes = decode(individual, indices, critical_points, fleet, hp)

    # Set routes
    reaching_states = {
        id_ev: (r.x1_0, r.x2_0, r.x3_0)
        for id_ev, r in critical_points.items()
    }
    vehicles_pos = [cp.S0 for cp in critical_points.values()]
    init_theta = np.array(
        [1 if i in vehicles_pos else 0 for i in range(len(fleet.network))])
    fleet.set_routes_of_vehicles(routes,
                                 reaching_states=reaching_states,
                                 init_theta=init_theta)

    # Cost
    costs = np.array(fleet.cost_function())

    # Calculate penalization
    feasible, distance, accept = fleet.feasible()

    penalization = 0
    if not accept:
        penalization = distance + hp.hard_penalization + hp.K2
    elif accept and not feasible:
        penalization = distance + hp.hard_penalization

    # Calculate fitness
    fit = np.dot(costs, np.asarray(hp.weights)) + penalization

    return fit, feasible, accept