首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么列表中的第一个位置不变?

为什么列表中的第一个位置不变?
EN

Stack Overflow用户
提问于 2022-10-12 22:41:00
回答 2查看 44关注 0票数 0

在这个小的交通模拟系统中,列表中的第一个位置不会改变,你可以看到:

代码语言:javascript
复制
[.....], (G), [.....]
[.....], (G), [...S.]
[.....], (G), [..SS.]
[.....], (G), [.SSS.]
[...S.], (G), [.SSS.]
[..SS.], (G), [.SSS.]
[.SSS.], (G), [.SSW.]
[.SSS.], (G), [.SWS.]
[.SSS.], (G), [.WSS.]
[.SSW.], (G), [.SSS.]
[.SWS.], (G), [.SSW.]
[.WSS.], (G), [.SWW.]
[.SSS.], (G), [.WWS.]
[.SSW.], (G), [.WSW.]
[.SWW.], (G), [.SWS.]
[.WWS.], (G), [.WSS.]
[.WSW.], (G), [.SSS.]
[.SWS.], (G), [.SSW.]
[.WSS.], (G), [.SWS.]
[.SSS.], (G), [.WSW.]

Final state:
[.SSW.], (G), [.SWW.]

我试图让“目的地S”定义的汽车也出现在第一个位置,就像

代码语言:javascript
复制
[.....] (G) [.....]   []
    [.....] (G) [....S]   []
    [.....] (G) [...SS]   []
    [.....] (G) [..SSS]   []
    [.....] (G) [.SSSS]   []
    [.....] (G) [SSSSS]   []
    [....S] (G) [SSSSW]   []
    [...SS] (G) [SSSWS]   []
    [..SSS] (R) [SSWSS]   []
    [.SSS.] (R) [SSWSS]   ['S']
    [SSS..] (G) [SSWSS]   ['S', 'W']
    [SS..S] (G) [SWSSS]   ['W', 'W']
    [S..SS] (G) [WSSSW]   ['W', 'S']
    [..SSW] (G) [SSSWW]   ['S', 'W']
    [.SSWS] (G) [SSWWS]   ['W', 'S']
    [SSWSS] (G) [SWWSW]   ['S', 'S']
    [SWSSS] (G) [WWSWS]   ['S', 'S']
    [WSSSW] (G) [WSWSS]   ['S', 'W']
    [SSSWW] (R) [SWSSS]   ['W', 'S']
    [SSWW.] (R) [SWSSS]   ['W', 'S', 'W']
    [SWW..] (G) [SWSSS]   ['W', 'S', 'W', 'W']
    [WW..S] (G) [WSSSW]   ['S', 'W', 'W', 'S']
    [W..SW] (G) [SSSWS]   ['W', 'W', 'S', 'W']
    [..SWS] (G) [SSWSW]   ['W', 'S', 'W', 'W']
    [.SWSS] (G) [SWSWW]   ['S', 'W', 'W']
    [SWSSS] (G) [WSWWS]   ['W', 'W', 'S']
    [WSSSW] (G) [SWWSW]   ['W', 'S']
    [SSSWS] (G) [WWSWW]   ['S']
    [SSWSW] (R) [WSWWS]   []
    [SWSW.] (R) [WSWWS]   []
    [WSW..] (G) [WSWWS]   ['W']
    [SW..W] (G) [SWWSW]   ['S']
    [W..WS] (G) [WWSWS]   []
    [..WSW] (G) [WSWS.]   []

类目的地是模拟新车辆到达交通系统的过程。对其step方法的每个调用都返回None,表示没有车辆到达该步骤,或者返回目的地('W‘或'S'),这个类被传递给我,我不允许更改它,如果有人能为我解释它,我不明白它到底是如何工作的。

类交通灯表示交通信号。

类车道是一种包含大量车辆和许多空空间的结构。车辆只可在行车线开始时驶入,而在行车线尽头离开,即行车线就象一条“管子”。

类车辆它的任务只是跟踪车辆创建的时间和它要去的地方。

Class TrafficSystem定义了特定的流量系统。它跟踪哪些车道、哪些信号以及车辆将如何通过它拥有的系统进行移动:

  1. 创建组件的构造函数.

一个方法步骤(),它将所有组件向前推进一步,将车辆移进或移出系统,通过信号灯和车道之间,就像this

  • Remove --图中的第一(左)车道上的第一辆车一样。-步进第一车道及其步进法。-如果信号是绿色的,将第一辆车从第二(右)车道移至第一车道。-按其步进法对交通信号进行步进。-走第二车道。-调用Destinations对象中的step方法--如果返回一个目标(即没有其他的内容),那么创建一个车辆并将其放在第二车道上。

  1. --一个打印系统快照的方法快照(),它应该基于来自各个组件的str()方法。

4.返回系统中当前车辆总数的number_in_system()方法。

5.打印一些统计数据的方法print_statistics()。

守则是:

代码语言:javascript
复制
class Destinations:
    """ Generates a sequence of destinations (None, 'W', 'S') """

    def __init__(self):
        self._arrivals = (  # 0:52, 1:26, 2:22
            2, 2, 2, 2, 2, 1, 2, 2, 2, 1, 1, 2, 1, 2, 2, 2, 1, 2, 1, 1,
            2, 1, 1, 0, 2, 0, 0, 0, 0, 1, 2, 0, 0, 0, 1, 0, 0, 2, 0, 1,
            2, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1,
            0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2, 0, 0,
            1, 2, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1)

        self._internal_time = 0
        self._total_cycle = len(self._arrivals)

    def step(self):
        ind = self._arrivals[self._internal_time]
        self._internal_time = (self._internal_time + 1) % len(self._arrivals)
        return 'W' if ind == 1 else 'S' if ind == 2 else None
# Traffic system components
class Vehicle:
    '''Represents vehicles in traffic simulations
    '''

    def __init__(self, destination, borntime):
        self.destination = destination
        self.borntime = borntime

    def __repr__(self):
        '''A verbose representation
        '''
        return f'Vehicle({self.destination}, {self.borntime})'

    def __str__(self):
        '''A simple representation
        '''
        return self.destination


class Light:
    """Represents a traffic light"""

    def __init__(self, period, green_period):
        self.period = period
        self.green_period = green_period
        self.color = 'G'
        self.time = 0

    def __str__(self):
        return f"({self.color})"

    def step(self):
        self.time += 1
        if self.time % self.period < self.green_period:
            self.color = 'G'
        else: 
            self.color = 'R'

    def is_green(self):
        if self.color == 'G':
            return True
        elif self.color == 'R':
            return False


class Lane:
    '''Represents a lane with (possible) vehicles
    '''

    def __init__(self, length):
        self.length = length
        self.lane = ['.'] * self.length

    def __str__(self):
        '''Lane representation
        '''
        return '[' + ''.join([str(s) for s in self.lane]) + ']'

    def enter(self, vehicle):
        self.lane[-1] = vehicle

    def is_last_free(self):
        return self.lane[-1] == '.'

    def step(self):
        '''Implementation of step
        '''
        for i in range(len(self.lane) - 1):
            if self.lane[i] == '.':  # if a spot is empty
                self.lane[i] = self.lane[i + 1]  # move next spot forward
                self.lane[i + 1] = '.'  # and clear next spot
        self.lane[-1] = '.'  # clear the very last spot

    def get_first(self):
        if self.lane[0] == '.':
            return None
        else:
            return self.lane[0]

    def remove_first(self):
        x = self.lane[0]
        if x == '.':
            return None
        else:
            self.lane[0] = '.'
            return x

    def number_in_lane(self):
        return len(self.lane) - self.lane.count('.')

from statistics import mean, median
from time import sleep
import destinations as ds
import trafficComponents as tc


class TrafficSystem:
    """Defines a traffic system"""

    def __init__(self):
        self.time = 0
        self.destinations = ds.Destinations()
        self.lane_1 = tc.Lane(5)
        self.lane_2 = tc.Lane(5)
        self.light = tc.Light(10, 8)

    def snapshot(self):
        print(
            f"{self.lane_1.__str__()}, {self.light.__str__()}, {self.lane_2.__str__()}")

    def step(self):
        v = tc.Vehicle(str(self.destinations.step()),10)
        self.lane_1.remove_first()
        if self.light.is_green:
            self.lane_2.enter(v)
            self.lane_2.step()
            if self.lane_2.get_first() != None:
                self.lane_1.enter(self.lane_2.remove_first())
                self.lane_1.step()
                self.lane_1.remove_first()
       
        

    def in_system(self):
        pass

    def print_statistics(self):
        pass


def main():
    ts = TrafficSystem()
    for i in range(20):
        ts.snapshot()
        ts.step()
        sleep(0.1)
    print('\nFinal state:')
    ts.snapshot()
    print()
    ts.print_statistics()


if __name__ == '__main__':
    main()
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-10-13 09:45:39

我的错误是在步骤()方法中排序赞扬。

守则是:

代码语言:javascript
复制
def step(self):
        self.time += 1
        self.lane_1.remove_first()
        self.lane_1.step()
        if self.light.is_green():
            self.lane_1.enter(self.lane_2.remove_first())
        self.light.step()
        self.lane_2.step()
        self.destinations.step()
        x = self.destinations.step()
        if x != None:
            v = tc.Vehicle(x, self.time)
            self.lane_2.enter(v)

我将循环范围更改为100。

结果是:

代码语言:javascript
复制
[.....], (G), [.....]
[.....], (G), [....S]
[.....], (G), [...SS]
[.....], (G), [..SSW]
[.....], (G), [.SSWS]
[.....], (G), [SSWSW]
[....S], (G), [SWSWS]
[...SS], (G), [WSWSS]
[..SSW], (R), [SWSSS]
[.SSW.], (R), [SWSSS]
[SSW..], (G), [SWSSW]
[SW..S], (G), [WSSWW]
[W..SW], (G), [SSWW.]
[..SWS], (G), [SWW..]
[.SWSS], (G), [WW...]
[SWSSW], (G), [W...W]
[WSSWW], (G), [...W.]
[SSWW.], (G), [..W..]
[SWW..], (R), [.W...]
[WW...], (R), [W...S]
[W....], (G), [W..SW]
[....W], (G), [..SW.]
[...W.], (G), [.SW..]
[..W..], (G), [SW...]
[.W..S], (G), [W....]
[W..SW], (G), [....W]
[..SW.], (G), [...W.]
[.SW..], (G), [..W.W]
[SW...], (R), [.W.W.]
[W....], (R), [W.W.W]
[.....], (G), [WW.WW]
[....W], (G), [W.WWW]
[...WW], (G), [.WWW.]
[..WW.], (G), [WWW..]
[.WW.W], (G), [WW..W]
[WW.WW], (G), [W..W.]
[W.WWW], (G), [..W.W]
[.WWW.], (G), [.W.W.]
[WWW..], (R), [W.W.W]
[WW...], (R), [WW.WS]
[W....], (G), [WWWS.]
[....W], (G), [WWS.S]
[...WW], (G), [WS.SW]
[..WWW], (G), [S.SWS]
[.WWWS], (G), [.SWSS]
[WWWS.], (G), [SWSS.]
[WWS.S], (G), [WSS..]
[WS.SW], (G), [SS...]
[S.SWS], (R), [S....]
[.SWS.], (R), [S....]
[SWS..], (G), [S...W]
[WS..S], (G), [...WS]
[S..S.], (G), [..WSS]
[..S..], (G), [.WSSW]
[.S...], (G), [WSSWS]
[S...W], (G), [SSWSW]
[...WS], (G), [SWSWS]
[..WSS], (G), [WSWSS]
[.WSSW], (R), [SWSSS]
[WSSW.], (R), [SWSSS]
[SSW..], (G), [SWSSW]
[SW..S], (G), [WSSWW]
[W..SW], (G), [SSWW.]
[..SWS], (G), [SWW..]
[.SWSS], (G), [WW...]
[SWSSW], (G), [W...W]
[WSSWW], (G), [...W.]
[SSWW.], (G), [..W..]
[SWW..], (R), [.W...]
[WW...], (R), [W...S]
[W....], (G), [W..SW]
[....W], (G), [..SW.]
[...W.], (G), [.SW..]
[..W..], (G), [SW...]
[.W..S], (G), [W....]
[W..SW], (G), [....W]
[..SW.], (G), [...W.]
[.SW..], (G), [..W.W]
[SW...], (R), [.W.W.]
[W....], (R), [W.W.W]
[.....], (G), [WW.WW]
[....W], (G), [W.WWW]
[...WW], (G), [.WWW.]
[..WW.], (G), [WWW..]
[.WW.W], (G), [WW..W]
[WW.WW], (G), [W..W.]
[W.WWW], (G), [..W.W]
[.WWW.], (G), [.W.W.]
[WWW..], (R), [W.W.W]
[WW...], (R), [WW.WS]
[W....], (G), [WWWS.]
[....W], (G), [WWS.S]
[...WW], (G), [WS.SW]
[..WWW], (G), [S.SWS]
[.WWWS], (G), [.SWSS]
[WWWS.], (G), [SWSS.]
[WWS.S], (G), [WSS..]
[WS.SW], (G), [SS...]
[S.SWS], (R), [S....]
[.SWS.], (R), [S....]

Final state:
[SWS..], (G), [S...W]
票数 1
EN

Stack Overflow用户

发布于 2022-10-12 23:17:10

TrafficSystem.step()总是从lane_1中移除第一个位置。

代码语言:javascript
复制
    def step(self):
        v = tc.Vehicle(str(self.destinations.step()),10)
        self.lane_1.remove_first()
        if self.light.is_green:
            self.lane_2.enter(v)
            self.lane_2.step()
            if self.lane_2.get_first() != None:
                self.lane_1.enter(self.lane_2.remove_first())
                self.lane_1.step()
                self.lane_1.remove_first()

它做的第一件事是self.lane_1.remove_first(),它移除第一个元素。

然后,如果lane_2的第一个元素不是None,它会将第一个元素移动到lane_1,步骤1,然后再次删除第一个元素。

因此,不管条件如何,它对self.lane_1所做的最后一件事就是remove_first()

我不明白模拟,所以我不知道你应该做什么来修复它。您需要找出真正需要调用remove_first()的时间。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74048681

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档