解释下列代码:def points(self): return self._points def add_points(self, u): self._points += u def playwith(self, ad_action, isOne): action = rnd.random() if action <= self.strategy(): act = 0 # 0 表示合作 else: act = 1 # 1 表示背叛 if (isOne == 1): act = 1 self.update_history(ad_action, act) self.modify_strategy(act,ad_action) d_u = U[0][act][ad_action] d_au = U[1][act][ad_action] self.add_points(d_u) return (act, d_u, d_au) def update_history(self, ad_action, my_action): self._history[0] += 1 # Playing times + 1 self._history[1] += ad_action # Times betrayal + 1 self._history.append(ad_action) self._myhistory[0] += 1 # Playing times + 1 self._myhistory[1] += my_action # Times betrayal + 1 self._myhistory.append(my_action)
时间: 2023-12-06 21:40:21 浏览: 173
这是一个类中的几个方法,具体解释如下:
- points(self):获取当前玩家的得分,返回值为 self._points。
- add_points(self, u):给当前玩家增加得分,传入参数 u 为增加的分数,将其加到 self._points 上。
- playwith(self, ad_action, isOne):模拟当前玩家与对手进行一局博弈,传入参数 ad_action 为对手的行动(0 表示合作,1 表示背叛),isOne 为当前玩家是否为第一个玩家。根据当前策略(由 self.strategy() 方法返回),随机选择合作或背叛,返回值为一个元组,包含当前玩家的行动 act、当前玩家得到的分数 d_u 和对手得到的分数 d_au。并且会调用 update_history 方法和 modify_strategy 方法进行历史记录更新和策略更新。
- update_history(self, ad_action, my_action):更新当前玩家的历史记录,传入参数 ad_action 为对手的行动,my_action 为当前玩家的行动。将当前玩家和对手的玩法记录分别存储在 self._myhistory 和 self._history 中。
- modify_strategy(self, act, ad_action):根据当前玩家的行动和对手的行动,更新当前玩家的策略。具体实现需要看完整代码。
相关问题
class AbstractGreedyAndPrune(): def __init__(self, aoi: AoI, uavs_tours: dict, max_rounds: int, debug: bool = True): self.aoi = aoi self.max_rounds = max_rounds self.debug = debug self.graph = aoi.graph self.nnodes = self.aoi.n_targets self.uavs = list(uavs_tours.keys()) self.nuavs = len(self.uavs) self.uavs_tours = {i: uavs_tours[self.uavs[i]] for i in range(self.nuavs)} self.__check_depots() self.reachable_points = self.__reachable_points() def __pruning(self, mr_solution: MultiRoundSolution) -> MultiRoundSolution: return utility.pruning_multiroundsolution(mr_solution) def solution(self) -> MultiRoundSolution: mrs_builder = MultiRoundSolutionBuilder(self.aoi) for uav in self.uavs: mrs_builder.add_drone(uav) residual_ntours_to_assign = {i : self.max_rounds for i in range(self.nuavs)} tour_to_assign = self.max_rounds * self.nuavs visited_points = set() while not self.greedy_stop_condition(visited_points, tour_to_assign): itd_uav, ind_tour = self.local_optimal_choice(visited_points, residual_ntours_to_assign) residual_ntours_to_assign[itd_uav] -= 1 tour_to_assign -= 1 opt_tour = self.uavs_tours[itd_uav][ind_tour] visited_points |= set(opt_tour.targets_indexes) # update visited points mrs_builder.append_tour(self.uavs[itd_uav], opt_tour) return self.__pruning(mrs_builder.build()) class CumulativeGreedyCoverage(AbstractGreedyAndPrune): choice_dict = {} for ind_uav in range(self.nuavs): uav_residual_rounds = residual_ntours_to_assign[ind_uav] if uav_residual_rounds > 0: uav_tours = self.uavs_tours[ind_uav] for ind_tour in range(len(uav_tours)): tour = uav_tours[ind_tour] quality_tour = self.evaluate_tour(tour, uav_residual_rounds, visited_points) choice_dict[quality_tour] = (ind_uav, ind_tour) best_value = max(choice_dict, key=int) return choice_dict[best_value] def evaluate_tour(self, tour : Tour, round_count : int, visited_points : set): new_points = (set(tour.targets_indexes) - visited_points) return round_count * len(new_points) 如何改写上述程序,使其能返回所有已经探索过的目标点visited_points的数量,请用代码表示
可以在 `solution()` 方法中添加一个变量来记录已经探索过的目标点数量,然后在每次更新 `visited_points` 后更新这个变量。下面是修改后的代码:
```
class AbstractGreedyAndPrune():
def __init__(self, aoi: AoI, uavs_tours: dict, max_rounds: int, debug: bool = True):
self.aoi = aoi
self.max_rounds = max_rounds
self.debug = debug
self.graph = aoi.graph
self.nnodes = self.aoi.n_targets
self.uavs = list(uavs_tours.keys())
self.nuavs = len(self.uavs)
self.uavs_tours = {i: uavs_tours[self.uavs[i]] for i in range(self.nuavs)}
self.__check_depots()
self.reachable_points = self.__reachable_points()
def __pruning(self, mr_solution: MultiRoundSolution) -> MultiRoundSolution:
return utility.pruning_multiroundsolution(mr_solution)
def solution(self) -> Tuple[MultiRoundSolution, int]:
mrs_builder = MultiRoundSolutionBuilder(self.aoi)
for uav in self.uavs:
mrs_builder.add_drone(uav)
residual_ntours_to_assign = {i : self.max_rounds for i in range(self.nuavs)}
tour_to_assign = self.max_rounds * self.nuavs
visited_points = set()
explored_points = 0
while not self.greedy_stop_condition(visited_points, tour_to_assign):
itd_uav, ind_tour = self.local_optimal_choice(visited_points, residual_ntours_to_assign)
residual_ntours_to_assign[itd_uav] -= 1
tour_to_assign -= 1
opt_tour = self.uavs_tours[itd_uav][ind_tour]
new_points = set(opt_tour.targets_indexes) - visited_points
explored_points += len(new_points)
visited_points |= new_points # update visited points
mrs_builder.append_tour(self.uavs[itd_uav], opt_tour)
return self.__pruning(mrs_builder.build()), explored_points
class CumulativeGreedyCoverage(AbstractGreedyAndPrune):
def evaluate_tour(self, tour : Tour, round_count : int, visited_points : set):
new_points = set(tour.targets_indexes) - visited_points
return round_count * len(new_points)
def local_optimal_choice(self, visited_points, residual_ntours_to_assign):
choice_dict = {}
for ind_uav in range(self.nuavs):
uav_residual_rounds = residual_ntours_to_assign[ind_uav]
if uav_residual_rounds > 0:
uav_tours = self.uavs_tours[ind_uav]
for ind_tour in range(len(uav_tours)):
tour = uav_tours[ind_tour]
quality_tour = self.evaluate_tour(tour, uav_residual_rounds, visited_points)
choice_dict[quality_tour] = (ind_uav, ind_tour)
best_value = max(choice_dict, key=int)
return choice_dict[best_value]
for k in range(cfg.RPN.SA_CONFIG.NPOINTS.__len__()): mlps = cfg.RPN.SA_CONFIG.MLPS[k].copy() channel_out = 0 for idx in range(mlps.__len__()): mlps[idx] = [channel_in] + mlps[idx] channel_out += mlps[idx][-1] self.SA_modules.append( PointnetSAModuleMSG( npoint=cfg.RPN.SA_CONFIG.NPOINTS[k], radii=cfg.RPN.SA_CONFIG.RADIUS[k], nsamples=cfg.RPN.SA_CONFIG.NSAMPLE[k], mlps=mlps, use_xyz=use_xyz, bn=cfg.RPN.USE_BN ) ) skip_channel_list.append(channel_out) channel_in = channel_out self.FP_modules = nn.ModuleList() for k in range(cfg.RPN.FP_MLPS.__len__()): pre_channel = cfg.RPN.FP_MLPS[k + 1][-1] if k + 1 < len(cfg.RPN.FP_MLPS) else channel_out self.FP_modules.append( PointnetFPModule(mlp=[pre_channel + skip_channel_list[k]] + cfg.RPN.FP_MLPS[k]) ) def _break_up_pc(self, pc): xyz = pc[..., 0:3].contiguous() features = ( pc[..., 3:].transpose(1, 2).contiguous() if pc.size(-1) > 3 else None ) return xyz, features def forward(self, pointcloud: torch.cuda.FloatTensor): xyz, features = self._break_up_pc(pointcloud) l_xyz, l_features = [xyz], [features] for i in range(len(self.SA_modules)): li_xyz, li_features = self.SA_modules[i](l_xyz[i], l_features[i]) l_xyz.append(li_xyz) l_features.append(li_features) for i in range(-1, -(len(self.FP_modules) + 1), -1): l_features[i - 1] = self.FP_modules[i]( l_xyz[i - 1], l_xyz[i], l_features[i - 1], l_features[i] ) return l_xyz[0], l_features[0]在forward函数中,如果我要使用channel_out变量传入SA_modules中,我该如何在forward函数中计算并得到它,再传入SA_modules中,你可以给我详细的代码吗?
在 forward 函数中,可以定义一个变量 channel_out,用于保存当前 SA_module 的输出通道数。然后在每一次循环时更新 channel_out 的值,并将其传入 SA_module 中。代码如下:
```
def forward(self, pointcloud: torch.cuda.FloatTensor):
xyz, features = self._break_up_pc(pointcloud)
l_xyz, l_features = [xyz], [features]
channel_out = 0 # 初始化 channel_out
for i in range(len(self.SA_modules)):
mlps = self.SA_modules[i].mlps # 获取当前 SA_module 的 MLPs
# 计算当前 SA_module 的输出通道数
channel_out = sum(mlp[-1] for mlp in mlps)
li_xyz, li_features = self.SA_modules[i](
l_xyz[i], l_features[i], channel_out=channel_out # 传入 channel_out
)
l_xyz.append(li_xyz)
l_features.append(li_features)
for i in range(-1, -(len(self.FP_modules) + 1), -1):
l_features[i - 1] = self.FP_modules[i](
l_xyz[i - 1], l_xyz[i], l_features[i - 1], l_features[i]
)
return l_xyz[0], l_features[0]
```
在 SA_module 的 forward 函数中,需要修改函数签名,增加一个参数 channel_out,用于指定输出通道数。然后在函数中使用该参数来初始化 MLP 中的第一个元素。代码如下:
```
class PointnetSAModuleMSG(nn.Module):
def __init__(self, npoint, radii, nsamples, mlps, use_xyz=True, bn=True):
super(PointnetSAModuleMSG, self).__init__()
self.npoint = npoint
self.radii = radii
self.nsamples = nsamples
self.bn = bn
self.use_xyz = use_xyz
self.SA_modules = nn.ModuleList()
for j in range(len(mlps)):
in_channel = mlps[j][0]
mlp = nn.Sequential()
for k in range(len(mlps[j])-1):
mlp.add_module('layer{}'.format(k),
pt_utils.Conv1d(in_channel, mlps[j][k+1], bn=self.bn))
in_channel = mlps[j][k+1]
self.SA_modules.append(mlp)
def forward(self, xyz, features, channel_out):
if self.use_xyz:
new_features_list = [xyz.transpose(1, 2)]
else:
new_features_list = []
for i in range(len(self.radii)):
idx, pts_cnt = pt_utils.query_ball_point(self.radii[i], self.nsamples[i], xyz, xyz)
grouped_xyz = pt_utils.grouping_operation(xyz, idx)
grouped_xyz -= xyz.unsqueeze(-1)
if features is not None:
grouped_features = pt_utils.grouping_operation(features, idx)
grouped_features = torch.cat([grouped_features, grouped_xyz], dim=1)
else:
grouped_features = grouped_xyz
new_features = self.SA_modules[i](grouped_features)
new_features = new_features.max(dim=-1)[0]
new_features_list.append(new_features)
new_features = torch.cat(new_features_list, dim=1)
for i in range(len(self.SA_modules)):
new_features[:, i:i+1] = self.SA_modules[i][0](new_features[:, i:i+1]) # 初始化 MLP 中的第一个元素
if channel_out != new_features.size(1): # 如果输出通道数不一致,进行升维或降维
new_features = pt_utils.Conv1d(new_features.size(1), channel_out, bn=self.bn)(new_features)
new_xyz = pt_utils.gather_operation(xyz, pt_utils.furthest_point_sample(xyz, self.npoint))
return new_xyz, new_features
```
阅读全文