Window_Store

布局基础:Canvas,StackPanel,Grid

Grid:分成两行两列

 1 <Grid>
 2     <Grid.RowDefinitions>
 3           <RowDefinition></RowDefinition>
 4           <RowDefinition></RowDefinition>
 5     </Grid.RowDefinitions>
 6     <Grid.ColumnDefinitions>
 7           <ColumnDefinitions></ColumnDefinitions>
 8           <ColumnDefinitions></ColumnDefinitions>
 9     </Grid.ColumnDefinitions>
10 </Grid>

动态生成:

注意://通过代码来为Image赋值,如果图片位于项目中,则需要给出ms-appx:///

为图片赋值路径:image.Source = new BitmapImage(new Uri(fileName));

 1   for (int i = 0; i < 10; i++)
 2             {
 3                 RowDefinition row = new RowDefinition();
 4                 grid1.RowDefinitions.Add(row);
 5             }
 6             for (int i = 0; i < 8; i++)
 7             {
 8                 ColumnDefinition column = new ColumnDefinition();
 9                 grid1.ColumnDefinitions.Add(column);
10             }
11             Random r = new Random();
12             for (int i = 0; i < 10; i++)
13             {
14                 for (int j = 0; j < 8; j++)
15                 {
16                     Image image = new Image();                    
17                     int num = r.Next(1,10);
18                     //通过代码来为Image赋值,如果图片位于项目中,则需要给出ms-appx:///
19                     string fileName="ms-appx:///Images/"+num+".png";
20                     //为图片赋路径
21                     image.Source = new BitmapImage(new Uri(fileName));
22                     grid1.Children.Add(image);
23                     Grid.SetRow(image,i);
24                     Grid.SetColumn(image,j);
25                 }
26             }

画刷:

1 创建渐变的Button
2 <Button.Background>
3      <LinearGradientBrush>
4            <GradientStop Offset="0" Color="Red"></GradientStop>
5            <GradientStop Offset="0.7" Color="Yellow"></GradientStop>
6            <GradientStop Offset="1" Color="Blue"></GradientStop>
7      </LinearGradientBrush>
8 </Button.Background>

仅用画刷实现模拟歌词的移动:

 1 <TextBlock Margin="50,350" FontSize="70" Text="今天天气好晴朗">
 2             <TextBlock.Foreground>
 3                 <LinearGradientBrush>
 4                     <GradientStop Color="Yellow" Offset="0"></GradientStop>
 5                     <GradientStop x:Name="gs1" Color="Yellow" Offset="0"></GradientStop>
 6                     <GradientStop x:Name="gs2" Color="Blue" Offset="0"></GradientStop>
 7                     <GradientStop Color="Blue" Offset="1"></GradientStop>
 8                 </LinearGradientBrush>
 9             </TextBlock.Foreground>
10         </TextBlock>
 1 后台
 2  protected override void OnNavigatedTo(NavigationEventArgs e)
 3         {
 4             DispatcherTimer timer = new DispatcherTimer();
 5             timer.Interval = TimeSpan.FromMilliseconds(200);
 6             timer.Tick+=timer_Tick;
 7             timer.Start();
 8         }
 9 
10         private void timer_Tick(object sender, object e)
11         {
12             gs1.Offset += 0.03;
13             gs2.Offset += 0.03;
14         }

 变换:

RotateTransform:旋转Angle

ScaleTransform:变大变小 ScaleY="-1" ScaleX="2"

TranslateTransform:左右平移 X="100"

3D投影:

<Image.Projection>
<PlaneProjection x:Name="pp1" RotationX="30" ></PlaneProjection>
</Image.Projection>

 

win8中是没有对话框的概念,不同的界面是不同的页,类似于网页,通过Frame.Navigate(typeof(TestPage),obj参数)来在不同页面导航。

NavigationService属性的方法:GoBack()后退;GoForward()前进;CanGoBack()属性判断是否可以后退。

页面导航默认是不启用缓存的,也就是后退以后前一个页面恢复成原始状态,需要在构造函数中执行NavigationCacheMode=NavigationCacheMode.Enable

页面三个和页面导航相关的四个虚方法:

OnNavigatedTo:当页面成为活动页面时调用。e.NavigationMode得知是新来的(New)还是后退回来的(Back)还是前进来的(Forward),一般初始化放到OnNavigatedTo中,注意判断Mode

OnNavigatedFrom():当前页面不是活动页面时调用

OnNavigatingFrom():在页面即将不再是活动页面时调用,实现确认是否退出

页面间跳转:Frame.Navigate(typeof(MainPage),1);

1位所传的参数

启用缓存:NavigationCacheMode.Enable启用页面缓存,当后退到这个页面的时候之前的修改还在,必须写在构造函数里面

在OnNavigatedTo的方法中

e.NavigationMode:可以用来判断是第一次访问还是后退回来的,前进来的。。。

NavigationMode.New:加载数据

设置样式:

直接在xaml中设置:

 1 <Page.Resources>
 2 <!--<Style TargetType="Button">
 3 <Setter Property="Background" Value="Red"></Setter>
 4 <Setter Property="BorderBrush" Value="Blue"></Setter>
 5 <Setter Property="BorderThickness" Value="5"></Setter>
 6 </Style>
 7 <Style TargetType="Button" x:Key="btnStyle">
 8 <Setter Property="Background" Value="Yellow"></Setter>
 9 <Setter Property="BorderBrush" Value="Gray"></Setter>
10 <Setter Property="BorderThickness" Value="5"></Setter>
11 </Style>-->
12 
13 </Page.Resources>
 1
用资源字典:
<Page.Resources> 2 3 <ResourceDictionary Source="NomalDic.xaml"></ResourceDictionary> 4 </Page.Resources> 5 <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 6 <Button Style="{StaticResource nomalBtn}" Content="Button" HorizontalAlignment="Left" Margin="216,252,0,0" VerticalAlignment="Top" Height="82" Width="184"/> 7 <Button Style="{StaticResource nomalBtn}" Content="Button" HorizontalAlignment="Left" Margin="652,245,0,0" VerticalAlignment="Top" Height="160" Width="202"/> 8 <Button Style="{StaticResource nomalBtn}" Content="Button" HorizontalAlignment="Left" Margin="1040,255,0,0" VerticalAlignment="Top" Height="150" Width="122"/> 9 <TextBlock Style="{StaticResource nomalTb}" HorizontalAlignment="Left" Margin="348,52,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="149" Height="107"/> 10 <TextBlock Style="{StaticResource nomalTb}" HorizontalAlignment="Left" Margin="748,92,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="329" Height="109"/> 11 12 </Grid>

资源字典的写法:

 1 <ResourceDictionary
 2     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
 3     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4     xmlns:local="using:App4">
 5     <Style TargetType="TextBlock" x:Key="nomalTb">
 6         <Setter Property="Foreground" Value="Red"></Setter>
 7     </Style>
 8     <Style TargetType="Button" x:Key="nomalBtn">
 9         <Setter Property="Background" Value="Red"></Setter>
10         <Setter Property="BorderBrush" Value="Blue"></Setter>
11         <Setter Property="BorderThickness" Value="5"></Setter>
12     </Style>
13 </ResourceDictionary>

数据绑定:

Silverlight中的数据绑定对被绑定对象有特殊要求,如果只是普通的get、set属性的对象用在数据绑定上有很多问题(无法双向绑定。只有Model值不会变或者Model变了不要求界面跟着变才可以使用普通属性),一般要求类实现INotifyPropertyChanged接口.

绑定中类属性的特殊写法:

 1 public class Person : INotifyPropertyChanged
 2     {
 3         private string _name;
 4         public string Name
 5         {
 6             get 
 7             {
 8                 return _name;
 9             }
10             set
11             {
12                 _name = value;
13                 if (PropertyChanged != null)
14                 {
15                     PropertyChanged(this,new PropertyChangedEventArgs("Name"));
16                 }
17             }
18         }
19         private int _age;
20         public int Age
21         {
22             get
23             {
24                 return _age;
25             }
26             set
27             {
28                 _age = value;
29                 if (PropertyChanged != null)
30                 {
31                     PropertyChanged(this,new PropertyChangedEventArgs("Age"));
32                 }
33             }
34         }
35 
36         public event PropertyChangedEventHandler PropertyChanged;
37     }

三种绑定模式:

OneTime:一次绑定,绑定创建时使用源数据更新控件。(可以是普通的get、set)。

OneWay(默认值):单向绑定,在绑定创建时使用源数据更新控件,当源数据发生变化的时候也更新控件。(必须实现INotifyPropertyChanged接口或者继承自DependencyObject)。相当于Eval

TwoWay:双向绑定,数据源的变化会更新控件,控件的变化也会更新到数据源。(必须实现INotifyPropertyChanged接口或者继承自DependencyObject)。相当于Bind

例:Text="{Binding Age,Mode=OneTime}"

绑定数据集合:

普通的集合的变化是不会影响UI的,集合需要实现INotifyCollectionChanged接口

如果使用OneWay、TwoWay以达到数据源 发生变化时UI的项随着数据变化,那么数据可以放到 private ObservableCollection<string> list1 = new ObservableCollection<string>()中,因为ObservableCollection中定义了一个集合改变的通知事件。

动画:

资源加载:

1  <Storyboard x:Name="sb1">
2             <DoubleAnimation Storyboard.TargetName="st1"   Storyboard.TargetProperty="ScaleX" From="0" To="3">             </DoubleAnimation>
3 </Storyboard>
1 控件部分:
2 <Image Name="img1" HorizontalAlignment="Left" Height="85" Margin="154,243,0,0" VerticalAlignment="Top" Width="86" Source="Images/China.jpg">
3             <Image.RenderTransform>
4                 <ScaleTransform x:Name="st1" ScaleX="3"></ScaleTransform>
5             </Image.RenderTransform>
6             <Image.Projection>
7                 <PlaneProjection x:Name="pp1" RotationY="30"></PlaneProjection>
8             </Image.Projection>
9         </Image>

 AppBar

当向上手势或者鼠标右键的时候会显示AppBar(以FreshPaint为例),AppBar分为顶部TopAppBar、底部BottomAppBar两块,建议把主要操作放在底部、顶部有特殊意义再放

Xaml中Page的TopAppBar、BottomAppBar两个属性赋值,AppBar类型,然后Content内容自由绘制。一般绘制横向的圆形图标,一般采用StandrardStyle.xaml中预置的AppBarButtonStyle样式,用到哪个反注释哪个,AutomationProperties.Name设定显示的文本。用法:<Button Style="{StaticResource EditAppBarButtonStyle}" AutomationProperties.Name="编辑"></Button>

高级:IsOpen打开状态(可以实现程序打开时默认AppBar显示);IsSticky是否钉住;Opened、Closed打开关闭事件。

高级容器:

ProgressBar进度条:IsIndeterminate模式

ProcessRing圆形的不确定进度条,IsActive属性表示是否活动

ToggleSwitch开关控件,IsOn是否打开,OffContent、OnContent、Heade关闭、打开、头部的显示内容;Toggled更改事件;

ScrollViewer用于显示大内容,显示尺寸不够则可以通过滚动条控制。

HorizontalScrollBarVisibility、VerticalScrollBarVisibility控制滚动条显示。

.net 4.5异步编程

异步方法不用等任务结束后才返回。 异步编程可以避免界面卡死的问题,但是会导致编写“等待一个异步操作执行完成再执行一段代码”的程序很麻烦。

异步方法返回Task或者IAsyncOperation对象(鼠标悬停会显示“可等待”),一般以Async结尾,异步方法不会等结束才返回。

在.net4.5中引入了async、await关键字,这样方法中这一行代码都将在异步方法执行完成后才执行。注意:一个方法中如果有await,则必须标注为async。

可以直接在await前声明变量,当异步方法执行完成后把执行结果返回设置给变量,一般var自动推断。

WinRT中所有执行时间可能会比较长操作都被设置为异步方法。

 

转载于:https://www.cnblogs.com/pushudepu/p/6105380.html

def reward_shaping(_obs, _extra_info, act, agent): """ 奖励塑造函数:结合等待时间变化、队列长度、旅行效率和通行效率计算奖励 适配FeatureProcess类的数据结构,减少重复计算,提升逻辑准确性 """ junction_ids = list(agent.preprocess.junction_dict.keys()) fp = agent.preprocess # 简化引用 frame_state = _obs["framestate"] frame_time = frame_state["frame_time"] vehicles = frame_state["vehicles"] # 初始化奖励字典 rewards = {j_id: {'total': 0, 'components': {}} for j_id in junction_ids} # 先更新交通信息(确保FeatureProcess中的动态数据是最新的) fp.update_traffic_info(_obs, _extra_info) # 预计算所有路口的平均等待时间(复用FeatureProcess的计算结果) all_junction_waiting = fp.get_all_junction_waiting_time(vehicles) # 为每个路口计算奖励分量 for j_id in junction_ids: # 1. 等待时间变化奖励(基于平均等待时间的变化量) current_waiting = all_junction_waiting.get(j_id, 0.0) prev_waiting = fp.old_waiting_time[j_id] waiting_delta = prev_waiting - current_waiting # 等待时间减少则奖励为正 # 归一化:将变化量映射到[0, 2](避免过大波动,0表示无改善,2表示最大改善) waiting_reward = 1.0 + np.tanh(waiting_delta / 5.0) # 5.0为缩放因子,可调整 fp.old_waiting_time[j_id] = current_waiting # 更新历史等待时间 rewards[j_id]['components']['waiting'] = waiting_reward # 2. 队列长度奖励(惩罚过长队列) queue_length = calculate_queue_length(j_id, fp) # 归一化:队列越长惩罚越大,映射到[-1, 0](tanh确保极端情况不会无限惩罚) queue_reward = -np.tanh(queue_length / 15.0) # 15.0为车道容量参考值,可调整 rewards[j_id]['components']['queue'] = queue_reward # 3. 旅行效率奖励(奖励车辆在进口道的有效行驶距离) travel_reward = calculate_travel_reward(j_id, fp, vehicles) rewards[j_id]['components']['travel'] = travel_reward # 4. 通行效率奖励(单位时间通过路口的车辆数) throughput_reward = calculate_throughput(j_id, frame_time, fp) rewards[j_id]['components']['throughput'] = throughput_reward # 组合奖励(权重可根据场景动态调整) weights = { 'waiting': 0.4, # 优先优化等待时间 'queue': 0.3, # 次优先优化队列长度 'travel': 0.15, # 辅助优化行驶效率 'throughput': 0.15 # 辅助优化通行量 } rewards[j_id]['total'] = sum( weights[k] * rewards[j_id]['components'][k] for k in weights ) # 按路口ID排序,为每个路口返回2个奖励值(对应相位和时长控制) sorted_junctions = sorted(junction_ids) return tuple( rewards[j_id]['total'] for j_id in sorted_junctions for _ in range(2) # 每个路口输出2个相同奖励(适配多维度动作) ) def calculate_queue_length(junction_id, fp: FeatureProcess): """ 计算路口进口道的总排队车辆数(仅统计处于等待状态的车辆) 复用FeatureProcess维护的lane_volume和waiting_time_store """ junction = fp.junction_dict[junction_id] total_queue = 0 # 遍历路口所有进口车道 for direction in junction["enter_lanes_on_directions"]: for lane_id in direction["lanes"]: # 仅统计车道上处于等待状态的车辆(速度≤0.1m/s) # 从lane_volume中筛选在waiting_time_store中的车辆(等待状态) waiting_vehicles = [ v_id for v_id in fp.lane_volume.get(lane_id, []) if v_id in fp.waiting_time_store ] total_queue += len(waiting_vehicles) return total_queue def calculate_travel_reward(junction_id, fp: FeatureProcess, vehicles): """ 计算路口进口道车辆的平均旅行效率(有效行驶距离) 结合车辆当前位置和历史行驶距离,确保统计的是当前路口的车辆 """ total_distance = 0.0 valid_count = 0 # 遍历当前路口进口道上的车辆(通过vehicle_prev_junction和当前junction判断) for vehicle in vehicles: v_id = vehicle["v_id"] # 条件1:车辆当前在进口车道上 # 条件2:车辆的目标路口是当前路口(确保属于该路口管控范围) if on_enter_lane(vehicle) and vehicle["target_junction"] == junction_id: # 累加该车辆在当前进口道的行驶距离 if v_id in fp.vehicle_distance_store: total_distance += fp.vehicle_distance_store[v_id] valid_count += 1 if valid_count == 0: return 0.0 # 无车辆时奖励为0 avg_distance = total_distance / valid_count # 归一化:距离越长(行驶顺畅)奖励越高,映射到[0, 1] return np.tanh(avg_distance / 100.0) # 100.0为参考距离,可根据道路长度调整 def calculate_throughput(junction_id, current_time, fp: FeatureProcess): """ 计算路口单位时间的通行效率(最近30秒通过的车辆数) 基于FeatureProcess的enter_lane_time和waiting_time_store判断车辆是否通过 """ time_window = 30.0 # 统计最近30秒 vehicles_passed = 0 # 遍历所有曾进入当前路口进口道的车辆 for v_id, enter_time in fp.enter_lane_time.items(): # 条件1:车辆的目标路口是当前路口 # 条件2:进入时间在统计窗口内 # 条件3:车辆已离开进口道(不在等待状态,即不在waiting_time_store中) if (fp.vehicle_prev_junction.get(v_id, -1) == junction_id and (current_time - enter_time) <= time_window and v_id not in fp.waiting_time_store): vehicles_passed += 1 # 归一化:通行量越高奖励越高,映射到[0, 1] # 转换为"车辆/分钟"并缩放(假设单车道最大通行量约为120辆/分钟) throughput = vehicles_passed * 2 # 30秒→60秒的转换 return np.tanh(throughput / 120.0) # 120为最大参考值,可调整 # 辅助函数:判断车辆是否在进口车道上(复用原逻辑,确保与FeatureProcess一致) def on_enter_lane(vehicle): """判断车辆是否处于进口车道(参考FeatureProcess中的逻辑)""" return vehicle["lane"] != "" and vehicle["in_junction"] is False 检查此代码是否和以下特征处理匹配? class FeatureProcess: """ Update traffic information and perform feature processing """ """ 更新交通信息并进行特征处理 """ def __init__(self, logger): self.logger = logger self.reset() def reset(self): # Store road structure and other relatively fixed dictionary-type variables # 存储道路结构等相对固定的字典型变量 self.junction_dict = {} self.edge_dict = {} self.lane_dict = {} self.l_id_to_index = {} self.vehicle_configs_dict = {} # Store dictionary-type variables for dynamic traffic information in traffic scenarios, # and update data after each communication # 存储交通场景中动态交通信息的字典型变量, 在每次通信后进行数据更新 self.vehicle_prev_junction = {} self.vehicle_prev_position = {} self.vehicle_distance_store = {} self.last_waiting_moment = {} self.waiting_time_store = {} self.enter_lane_time = {} # Stores variables that can be used to calculate rewards # 存储可用于计算奖励的变量 self.lane_volume = {} # User-defined variable # 用户自定义变量 self.old_waiting_time = {} self.enter_lane_count = 26 def init_road_info(self, start_info): """ Obtain fixed variables such as road structure """ """ 获取道路结构等信息固定的变量 """ junctions, signals, edges = ( start_info["junctions"], start_info["signals"], start_info["edges"], ) lane_configs, vehicle_configs = ( start_info["lane_configs"], start_info["vehicle_configs"], ) # Store road structure information in various variables # 将道路结构信息存储到各个变量 for junction in junctions: self.junction_dict[junction["j_id"]] = junction self.l_id_to_index[junction["j_id"]] = {} self.old_waiting_time[junction["j_id"]] = 0 index = 0 for approaching_edges in junction["enter_lanes_on_directions"]: for lane in approaching_edges["lanes"]: self.l_id_to_index[junction["j_id"]][lane] = index index += 1 for edge in edges: self.edge_dict[edge["e_id"]] = edge for lane in lane_configs: self.lane_dict[lane["l_id"]] = lane for vehicle_config in vehicle_configs: self.vehicle_configs_dict[vehicle_config["v_config_id"]] = vehicle_config for lane in lane_configs: self.lane_volume[lane["l_id"]] = [] def update_traffic_info(self, obs, extra_info): """ Update vehicle history information and calculate various dynamic traffic variables """ """ 更新车辆历史信息, 计算各项动态交通变量 """ frame_state = obs["framestate"] frame_no = frame_state["frame_no"] frame_time, vehicles = frame_state["frame_time"], frame_state["vehicles"] if frame_no <= 1: # Initial frame loads road structure information # 初始帧载入道路结构信息 game_info = extra_info["gameinfo"] self.init_road_info(game_info) for vehicle in vehicles: # If the vehicle appears for the first time, initialize the vehicle's historical intersection information # 如果车辆第一次出现,则初始化车辆的历史交叉口信息 if vehicle["v_id"] not in self.vehicle_prev_junction: self.vehicle_prev_junction[vehicle["v_id"]] = vehicle["junction"] # For vehicles that appear for the first time, if they are on the lane, record their appearance time # 对于首次出现的车辆, 若在车道上则记录其出现时间 if ( self.vehicle_prev_junction[vehicle["v_id"]] == -1 and on_enter_lane(vehicle) and vehicle["v_id"] not in self.enter_lane_time ): self.enter_lane_time[vehicle["v_id"]] = frame_time # When a vehicle enters another entrance lane from the intersection, recalculate its appearance time # 当车辆从交叉口驶入另一进口车道时, 重新统计其出现时间 elif self.vehicle_prev_junction[vehicle["v_id"]] != vehicle["junction"]: if self.vehicle_prev_junction[vehicle["v_id"]] != -1 and on_enter_lane(vehicle): self.enter_lane_time[vehicle["v_id"]] = frame_time self.cal_waiting_time(frame_time, vehicle) self.cal_travel_distance(vehicle) self.cal_v_num_in_lane(vehicle) def cal_waiting_time(self, frame_time, vehicle): """ Calculate the waiting time of the vehicle. When the vehicle is on the enter lane, count the accumulated time when its speed is <= 0.1m/s as its waiting time when driving at the intersection """ """ 计算车辆等待时间, 当车辆处于进口车道上时, 统计其车速<=0.1m/s的累计时长作为其在该交叉口行驶时的等待时间 """ waiting_time = 0 # Determine whether the vehicle in the lane approaching the intersection is in a waiting state, # and calculate the waiting time # 对处于车道驶向交叉口的车辆判断是否处于等待状态, 计算等待时间 if on_enter_lane(vehicle): # Determine whether the vehicle is in a waiting state. # The determination condition is that the vehicle speed is <= 0.1m/s # 判断车辆是否处于等待状态, 判定条件为车辆速度<=0.1m/s if vehicle["speed"] <= 0.1: if vehicle["v_id"] not in self.last_waiting_moment: # Record the starting moment of each time the vehicle enters the waiting state # 记录车辆在每次进入等待状态的起始时刻 self.last_waiting_moment[vehicle["v_id"]] = frame_time # When the vehicle is in the waiting state for the first time, # initialize its accumulated waiting time # 车辆首次处于等待状态则初始化车辆累计等待时间 if vehicle["v_id"] not in self.waiting_time_store: self.waiting_time_store[vehicle["v_id"]] = 0 else: # When a vehicle enters the waiting state on a lane, # waiting_time records the duration of the current waiting state # 车辆在一条道路上进入等待状态, waiting_time记录本次等待状态已持续的时间 waiting_time = frame_time - self.last_waiting_moment[vehicle["v_id"]] self.waiting_time_store[vehicle["v_id"]] += waiting_time self.last_waiting_moment[vehicle["v_id"]] = frame_time else: if vehicle["v_id"] in self.last_waiting_moment: del self.last_waiting_moment[vehicle["v_id"]] else: # Prevent repeated del when the vehicle is generated for the first time or at an intersection, # v_id is not stored in self.waiting_time_store # 防止车辆首次生成或位于交叉口时反复del, v_id未储存在self.waiting_time_store内 if vehicle["v_id"] in self.waiting_time_store: del self.waiting_time_store[vehicle["v_id"]] if vehicle["v_id"] in self.last_waiting_moment: del self.last_waiting_moment[vehicle["v_id"]] def cal_travel_distance(self, vehicle): """ Calculate the travel distance. When the vehicle is on the enter lane, count the total distance it travels at the intersection """ """ 计算旅行路程, 当车辆处于进口车道上时, 统计其在该交叉口行驶时的总路程 """ # When the vehicle is on the lane, calculate the cumulative distance # 当车辆处于车道上时, 计算累计路程 if on_enter_lane(vehicle): # When the vehicle enters the lane from inside the intersection for the second or subsequent time, # clear the cumulative distance and prepare to calculate the distance of this entry into the inlane # 车辆非首次从交叉口内部驶入车道时, 清空累计路程, 准备计算此次进入进口车道的路程 if self.vehicle_prev_junction[vehicle["v_id"]] != -1 and vehicle["v_id"] in self.vehicle_distance_store: del self.vehicle_distance_store[vehicle["v_id"]] if vehicle["v_id"] not in self.vehicle_distance_store: self.vehicle_distance_store[vehicle["v_id"]] = 0 self.vehicle_prev_position[vehicle["v_id"]] = [ vehicle["position_in_lane"]["x"], vehicle["position_in_lane"]["y"], ] else: if vehicle["v_id"] in self.vehicle_distance_store and vehicle["v_id"] in self.vehicle_prev_position: try: # Calculate Euclidean distance # 计算欧氏距离 self.vehicle_distance_store[vehicle["v_id"]] += math.sqrt( math.pow( vehicle["position_in_lane"]["x"] - self.vehicle_prev_position[vehicle["v_id"]][0], 2, ) + math.pow( vehicle["position_in_lane"]["y"] - self.vehicle_prev_position[vehicle["v_id"]][1], 2, ) ) except Exception: raise ValueError # Update the vehicle's historical position after each distance calculation # 每次计算距离后更新车辆历史位置 self.vehicle_prev_position[vehicle["v_id"]] = [ vehicle["position_in_lane"]["x"], vehicle["position_in_lane"]["y"], ] else: # When the vehicle enters the intersection, # delete the historical location information to avoid calculating the driving distance # based on the last departure position when entering the lane next time # 当车辆驶入交叉口, 删除历史位置信息, 避免下次进入车道时按上一次离开路口位置计算行驶距离 if vehicle["v_id"] in self.vehicle_prev_position: del self.vehicle_prev_position[vehicle["v_id"]] def cal_v_num_in_lane(self, vehicle): """ Calculate the number of vehicles on the lane. When a vehicle is in the import lane, the number of vehicles on the enter lane increases """ """ 计算车道上的车辆数, 当车辆处于进口车道上时, 则该进口车道上车辆数增加 """ # Update the number of vehicles on each lane # 更新每条车道上的车辆数量 if on_enter_lane(vehicle): if vehicle["v_id"] not in self.lane_volume[vehicle["lane"]]: self.lane_volume[vehicle["lane"]].append(vehicle["v_id"]) # Update the vehicle's historical intersection information # 更新车辆的历史所在交叉口信息 self.vehicle_prev_junction[vehicle["v_id"]] = vehicle["junction"] def get_all_junction_waiting_time(self, vehicles: list): """ This function obtain a dict of waiting_time by junction Args: - vehicles (list): input list of Vehicle - vehicle_waiting_time (list): input key = v_id (uint32), value = vehicle_waiting_time (list) Returns: - dict: key = vehicle.junction (uint32), value = junction waiting time """ """ 此函数获取交叉口车辆等待时间的字典 参数: - vehicles (list): 车辆的输入列表 - vehicle_waiting_time (list): input key = v_id (uint32), value = vehicle_waiting_time (list) 返回: - dict: key = vehicle.junction (uint32), value = junction waiting time """ res = {} v_num = {} for junction_id in self.junction_dict: res[junction_id] = 0 v_num[junction_id] = 0 for vehicle in vehicles: if vehicle["junction"] != -1 or vehicle["target_junction"] == -1: continue if vehicle["v_id"] in self.waiting_time_store: t = self.waiting_time_store[vehicle["v_id"]] else: t = 0 res[vehicle["target_junction"]] += t v_num[vehicle["target_junction"]] += 1 # Calculate the average waiting time of all vehicles in the scene # 计算场景内所有车辆的平均等待时间 for junction_id in self.junction_dict: if v_num[junction_id] != 0: res[junction_id] /= v_num[junction_id] return res
最新发布
08-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值