class Greedy(object): def greedy(self, goods, W): # goods是物品的集合,W是背包的空闲重量 result = [] sum_weight = 0 ppp = 0 danjia= 0 goods.sort(key=lambda obj: obj.value / obj.weight, reverse=True) for i in goods: if sum_weight + i.weight <= W : sum_weight = sum_weight + i.weight ppp = ppp + i.value danjia = danjia + i.value / i.weight result.append(i.weight) return result, sum_weight, ppp, danjia
时间: 2024-02-15 22:27:41 浏览: 141
这是一个实现背包问题贪心算法的类,其中`greedy`方法接受两个参数,`goods`表示物品的集合,`W`表示背包的剩余空闲重量。函数返回值包含四个部分,`result`表示被选中的物品的重量列表,`sum_weight`表示被选中的物品的总重量,`ppp`表示被选中的物品的总价值,`danjia`表示被选中的物品的平均价值。
在方法中,首先对物品按照单位重量价值从大到小排序,然后依次将物品放入背包中,直到背包装满为止。在放入物品的过程中,记录被选中的物品的重量列表、总重量、总价值和平均价值,并返回这些信息。
相关问题
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]
请解释以下代码:# 根据输入观察值,采样输出的动作值,有一定概率采取随机动作(探索) def sample(self, obs): if np.random.uniform(0, 1) < (1.0 - self.epsilon): #根据table的Q值选动作 action = self.predict(obs) else: action = np.random.choice(self.act_n) #有一定概率随机探索选取一个动作 return action
这段代码是一个采样函数,用于根据输入的观察值进行动作选择。它使用了epsilon-greedy策略,在一定概率下采取随机动作以进行探索。
首先,代码通过生成一个0到1之间的随机数,判断是否小于1.0减去epsilon的值。如果小于该值,则以1-epsilon的概率选择预测的动作,即调用self.predict(obs)来获取动作。
如果生成的随机数大于等于1.0减去epsilon的值,则以epsilon的概率进行随机探索,通过np.random.choice(self.act_n)来从动作空间中随机选择一个动作。
最后,返回选取的动作值。这样,根据epsilon-greedy策略,在一定概率下会采取随机动作以进行探索,而在其他情况下会根据预测值选择动作。
阅读全文