TUDelft-CITG / OpenCLSim

Rule driven scheduling of cyclic activities for in-depth comparison of alternative operating strategies.
https://openclsim.readthedocs.io
MIT License
14 stars 17 forks source link

Site as loader cannot release its rescource #85

Closed Pietervanhalem closed 4 years ago

Pietervanhalem commented 4 years ago

Summary

Description

I am trying to build a simulation where a barge travels between to locations. It is loaded by the origin (dredging_site) and unloads itself. However, when I set up the simulation the site seems not able to release its resource.

This is not a urgent issue. However, can you spot my mistake or fix the OpenCLSim bug?

What I Did

import datetime, time
import simpy
import shapely.geometry
import openclsim.core as core
import openclsim.model as model
my_env = simpy.Environment(initial_time = time.mktime(datetime.datetime.now().timetuple()))
my_env.epoch = time.mktime(datetime.datetime.now().timetuple())
discharge_site_GC = type(
    'Site', 
    (
        core.Identifiable,
        core.Log,
        core.Locatable,
        core.HasContainer,
        core.HasResource
    ),
    {}
)

dredging_site_GC = type(
    'Site', 
    (
        core.Identifiable,
        core.Log,
        core.Locatable,
        core.HasContainer,
        core.HasResource,
        core.UnloadingFunction,
        core.LoadingFunction,
        core.Processor,
    ),
    {}
)

barge_GC = type(
    "Barge",
    (
        core.Identifiable,
        core.Log,
        core.ContainerDependentMovable,
        core.Processor,
        core.HasResource,
        core.UnloadingFunction,
    ),
    {},
)
def compute_v_provider(v_empty, v_full):
    return lambda x: x * (v_full - v_empty) + v_empty

discharge_site_location = shapely.geometry.Point(4.18055556, 52.18664444)
dredging_site_location = shapely.geometry.Point(4.25222222, 52.11428333)

discharge_site_data = {
    "env": my_env,
    "name": "discharge_site_data",
    "geometry": discharge_site_location,
    "capacity": 500_000,
    "level": 0
}

dredging_site_data = {
    "env": my_env,
    "name": "dredging_site_data",
    "geometry": dredging_site_location,
    "capacity": 500_000,
    "level": 500_000,
    "unloading_rate": 1,
    "loading_rate": 1,
}

barge_data = {
    "env": my_env,
    "name": "Hopper 01",
    "geometry": discharge_site_location,
    "unloading_rate": 1,
    "capacity": 5_000,
    "compute_v": compute_v_provider(5, 4.5),
}

dredging_site = dredging_site_GC(**dredging_site_data)
discharge_site = discharge_site_GC(**discharge_site_data)
barge = barge_GC(**barge_data)
activity = model.Activity(
    env = my_env,
    name = "Soil movement",
    origin = dredging_site,
    destination = discharge_site,
    loader = dredging_site,
    mover = barge,
    unloader = barge,
    start_event = None,
    stop_event = None
)
my_env.run()
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/opt/conda/lib/python3.7/site-packages/openclsim/model.py in conditional_process(activity_log, env, stop_event, sub_processes)
    176         for sub_process in sub_processes:
--> 177             yield from sub_process(activity_log=activity_log, env=env)
    178 

/opt/conda/lib/python3.7/site-packages/openclsim/model.py in single_run_process(activity_log, env, origin, destination, loader, mover, unloader, engine_order, filling, stop_reservation_waiting_event, verbose)
    359             _release_resource(
--> 360                 resource_requests, origin.resource, kept_resource=mover.resource
    361             )

/opt/conda/lib/python3.7/site-packages/openclsim/model.py in _release_resource(requested_resources, resource, kept_resource)
    502     if resource != kept_resource:
--> 503         resource.release(requested_resources[resource])
    504         del requested_resources[resource]

KeyError: <simpy.resources.resource.Resource object at 0x7fbc7c8e5f60>

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
<ipython-input-6-cf25dcac65e6> in <module>
----> 1 my_env.run()

/opt/conda/lib/python3.7/site-packages/simpy/core.py in run(self, until)
    136         try:
    137             while True:
--> 138                 self.step()
    139         except StopSimulation as exc:
    140             return exc.args[0]  # == until.value

/opt/conda/lib/python3.7/site-packages/simpy/core.py in step(self)
    228             exc = type(event._value)(*event._value.args)
    229             exc.__cause__ = event._value
--> 230             raise exc

KeyError: <simpy.resources.resource.Resource object at 0x7fbc7c8e5f60>
markvankoningsveld commented 4 years ago

I have been able to reproduce the issue. Will have a look at what is happening there.