[Python] My ArrivalTimeUpdatingVariableListener is not working, how to make it work

Discussão em 'Python' iniciado por Stack, Setembro 15, 2024.

  1. Stack

    Stack Membro Participativo

    I am developing route optimiser using timefold python but ArrivalTimeUpdatingVariableListener is not running I also included print statement to check if it's running in background or not. I have used the code that was given in the tests folder in timefold_solver_python official repo. link: https://github.com/TimefoldAI/timefold-solver-python/blob/main/tests/test_vehicle_routing.py#L28

    My code:

    `class ArrivalTimeUpdatingVariableListener(VariableListener):
    @staticmethod
    def calculate_arrival_time(customer: Optional['Customer'], previous_departure_time: Optional[datetime]) \
    -> datetime | None:
    if customer is None or previous_departure_time is None:
    return None
    return previous_departure_time + timedelta(seconds=customer.drivingTimeSecondsFromPreviousStandstill)

    def after_variable_changed(self, score_director: ScoreDirector, customer: 'Customer') -> None:
    print("b")
    if customer.vehicle is None:
    if customer.arrival_time is not None:
    score_director.before_variable_changed(customer, 'arrival_time')
    customer.arrival_time = None
    score_director.after_variable_changed(customer, 'arrival_time')
    return None
    previous_customer = customer.previous_customer
    departure_time = customer.vehicle.departure_time if previous_customer is None \
    else previous_customer.get_departure_time()
    next_customer = customer
    arrival_time = ArrivalTimeUpdatingVariableListener.calculate_arrival_time(next_customer, departure_time)
    while next_customer is not None and next_customer.arrival_time != arrival_time:
    score_director.before_variable_changed(next_customer, 'arrival_time')
    next_customer.arrival_time = arrival_time
    score_director.after_variable_changed(next_customer, 'arrival_time')
    departure_time = next_customer.get_departure_time()
    next_customer = next_customer.next_customer
    arrival_time = ArrivalTimeUpdatingVariableListener.calculate_arrival_time(next_customer, departure_time)


    @planning_entity
    class Customer(JsonDomainBase):
    id: Annotated[str, PlanningId]
    name: Optional[str] = None
    location: Annotated[Location, LocationSerializer, LocationValidator]
    ready_time: datetime
    due_time: datetime
    service_duration: timedelta
    demand: int
    vehicle: Annotated[Optional['Vehicle'], InverseRelationShadowVariable(
    source_variable_name='customers'), IdSerializer, VehicleValidator, Field(default=None)]
    previous_customer: Annotated[Optional['Customer'], PreviousElementShadowVariable(
    source_variable_name='customers'), IdSerializer, CustomerValidator, Field(default=None)]
    next_customer: Annotated[Optional['Customer'], NextElementShadowVariable(
    source_variable_name='customers'), IdSerializer, CustomerValidator, Field(default=None)]
    arrival_time: Annotated[Optional[datetime], ShadowVariable(
    variable_listener_class=ArrivalTimeUpdatingVariableListener,
    source_variable_name='vehicle'), ShadowVariable(
    variable_listener_class=ArrivalTimeUpdatingVariableListener,
    source_variable_name='previous_customer'), Field(default=None)]



    I have tried to debug but nothing was printing, I want arrival time

    Continue reading...

Compartilhe esta Página