【问题标题】:Data structure for a basic circuit基本电路的数据结构
【发布时间】:2020-01-28 20:58:54
【问题描述】:

创建一个非常非常基本的电路模拟器,它接受以下组件:

  • 具有固定不变电压的直流电池电源
  • 零电阻接线
  • 具有固定电阻的电阻器

我想“描述”一个设置,例如:

我想“组件”可能看起来像这样:

class Battery:
    def __init__(self, voltage=None):
        self.voltage = voltage

class Resistor:
    def __init__(self, resistance=None):
        self.resistance = resistance

class Wire:
    # how to describe variable connections?


>>> V = Battery(9)
>>> R1, R2, R3 = Resistor(10e3), Resistor(2e3), Resistor(1e3)

然后,一旦我有了组件,如何描述所有组件如何“连接”的示例是什么?

【问题讨论】:

    标签: python data-structures graph


    【解决方案1】:

    您可以添加 startend 参数来指示组件在电路中的位置。

    class BaseComponent:
        def __init__(self, start, end):
            self.start = start
            self.end = end
    
        def __repr__(self):
            return f'<{self.__class__.__name__} start={self.start} end={self.end} at 0x{id(self):x}>'
    
        def __eq__(self, other):
            if not type(self)==type(other):
                return False
            return all(v==getattr(other, k) for k, v in self.__dict__.items())
    
    class Battery(BaseComponent):
        def __init__(self, start, end, voltage=None):
            super().__init__(start, end)
            self.voltage = voltage
    
    class Resistor(BaseComponent):
        def __init__(self, start, end, resistance=None):
            super().__init__(start, end)
            self.resistance = resistance
    
    class Wire(BaseComponent):
        def __init__(self, start, end):
            super().__init__(start, end)
    

    然后您可以通过以下方式进行布局:

    V = Battery(8, 1, voltage=9)
    w1, w2, w3 = Wire(1, 2), Wire(2, 3), Wire(3, 4)
    R1, R2, R3 = Resistor(2, 7, 10e3), Resistor(3, 6, 2e3), Resistor(4, 5, 1e3)
    w5, w6, w7 = Wire(5, 6), Wire(6, 7), Wire(7, 8)
    

    如果你想变得花哨,你也可以把它变成一个图表。

    class Circuit:
        """A directed graph structure for building circuits"""
    
        def __init__(self):
            """Creates the graph.  Note only one edge is allowed between nodes"""
            self._outward = {}
            self._inward = {}
    
        @staticmethod
        def _norm(name):
            return str(name)
    
        def add_node(self, name):
            """
            Adds a single node to the graph. The `name` is converted to a string.
            """
            node = self._norm(name)
            # check for existance
            if node in self._outward:
                return node
            self._outward.setdefault(node, {})
            self._inward.setdefault(node, {})
            return node
    
        def add_component(self, component):
            """
            Adds a circuitry component to the circuit.
            """
            s = self._norm(component.start)
            e = self._norm(component.end)
            if self.has_edge(s, e):
                raise KeyError(f'A link from {s} to {e} already exists')
            start = self.add_node(component.start)
            end = self.add_node(component.end)
            self._outward[start][end] = component
            self._inward[end][start] = component
    
        def has_node(self, node):
            return self._norm(node) in self._outward
    
        def has_component(self, component):
            start = self._norm(component.start)
            end = self._norm(component.end)
            return component==self._outward.get(start, {}).get(end)
    
        def has_edge(self, start, end):
            s = self._norm(start)
            e = self._norm(end)
            return bool(e in self._outward.get(s, {}))
    
        def __iadd__(self, other):
            self.add_component(other)
            return self
    

    从这里您可以通过以下方式构建电路:

    circuit = Circuit()
    for c in (V, w1, w2, w3, R1, R2, R3, w5, w6, w7):
        circuit.add_component(c)
    

    【讨论】:

    • 干得好!努力肯定表明:)
    • 这是一个非常有趣的小编码项目。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-22
    • 2020-05-17
    • 1970-01-01
    • 2016-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多