3

I'm trying to find a suitable inheritance configuration for the following problem:

My base class is an Agent Class which has 3 abstract methods:

class Agent():
__metaclass__ = abc.ABCMeta
 def __init__(self):
 @abc.abstractmethod
 def bidding_curve(self):
 @abc.abstractmethod
 def evaluate_bidding_curve(self):
 @abc.abstractmethod
 def action(self):

An agent can either be simulated using an executable (dymo) or using an FMU (a zip file of C-source code). So I created 2 different types of Agents that inherit from the base Agent and implement their way of communicating with the simulation. Here is the implementation of the dymo simulation environment but the same goes for the FMU.

class DymoAgent(Agent):
 def __init__(self, power, dymo):
 """ 
 Agent based on the dymosim simulation environment.
 @param dymo: The dymo that needs to be run
 """
 super(DymoAgent, self).__init__()
 self.dymo = dymo
 def bidding_curve(self):
 pass
 def evaluate_bidding_curve(self, priority):
 pass
 def action(self, priority):
 self.dymo.action()

The DymoAgent and the FMUAgent determine how my Agent will interact with the simulation environment.

Next I need an implementation of the Agent to determine how the Agent will interact with an application. However, I want this interaction to be independent of the simulation environment. E.g. I want to create an agent that can communicate with a Heater application and implements the logic to do so.

class Heater(WhatAgentShouldIUseHere):
 pass

I want this Heater class to implement the abstract methods from the Agent base-class and the implementation of a Dymo- or FMUAgent so it knows how to interact with the simulation. But I don't want to write 2 Heater classes with the same logic (and thus basically the same code) inheriting from the different simulation Agents. I summarized the question in this image:

enter image description here

Is there a way to use inheritance to prevent this?

/Arnout

asked Mar 10, 2014 at 7:42

1 Answer 1

7

Make Heater a simple object which accepts an agent in its constructor:

class Heater(object):
 def __init__(self, agent):
 self.agent = agent
 # define other methods for interaction with Heater application
 # in which you can access agent methods like self.agent.a_method(...)
answered Mar 10, 2014 at 7:58
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.