博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
贪婪算法 python_在Python中使用贪婪算法进行优化
阅读量:2535 次
发布时间:2019-05-11

本文共 3330 字,大约阅读时间需要 11 分钟。

贪婪算法 python

In the real world, choosing the best option is an optimization problem and as a result, we have the best solution with us. In mathematics, optimization is a very broad topic which aims to find the best fit for the data/problem. Such optimization problems can be solved using the Greedy Algorithm ("A greedy algorithm is an algorithm that follows the problem-solving heuristic of making the locally optimal choice at each stage with the intent of finding a global optimum"). This is the and we find one of the optimum solutions by keeping constraints in mind. This is one of the simplest algorithms used for optimization.

在现实世界中,选择最佳选项是一个优化问题,因此,我们拥有最佳解决方案。 在数学中,优化是一个非常广泛的主题,旨在找到最适合数据/问题的方法。 可以使用贪婪算法 ( “贪心算法是遵循在每个阶段做出局部最优选择以寻求全局最优的意图的问题解决启发式 算法 ”)来解决此类优化问题。 这是 ,我们通过牢记约束来找到最佳解决方案之一。 这是用于优化的最简单算法之一。

Let us consider a problem where Hareus gets 1500$ as pocket money. He is a hostler and needs to buy essentials for the month. So, he reserves 1000$ for essentials and now he has the rest of the 500$ for his spending. He went to the supermarket and there he had to decide what to buy according to the value(a measure of each item related to productivity) and also have a constraint of 500$. This is one of the optimization problems and the following is the code for choosing the items in one of the best ways.

让我们考虑一个问题,Hareus得到1500美元的零用钱。 他是一个招待员,需要购买该月的必需品。 因此,他保留了1000美元的必需品,现在剩下500美元中的其余钱用于他的支出。 他去了超市,在那里他不得不根据价值(与生产力相关的每个项目的度量)决定要买什么,并且还具有500 $的约束。 这是优化问题之一,以下是以最佳方式之一选择项目的代码。

Key Idea: Productivity Maximum with 500$.

关键思想:最高生产率为500 $。

Python Implementation:

Python实现:

# Greedy Algorithm for a Optimisation Problem# Defined a class for item, # with its name, value and costclass Itm(object):    def __init__(self, name, val, cost):        self.name = name        self.val = val        self.cost = cost            def getvalue(self):        return self.val        def getcost(self):        return self.cost        def __str__(self):        return self.name   # Defining a function for building a List # which generates list of items that are # available at supermart   def buildlist(names, values, costs):    menu = []    for i in range(len(names)):        menu.append(Itm(names[i], values[i], costs[i]))    return menu# Implementation of greedy algorithm # to choose one of the optimum choicedef greedy(items, maxcal, keyfunction):    itemscopy = sorted(items, key = keyfunction, reverse = True)        result = []    totalval = 0     totalcal = 0        for i in range(len(items)):        if (totalcal + itemscopy[i].getcost() <= maxcal):            result.append(itemscopy[i])            totalval = totalval + itemscopy[i].getvalue()            totalcal = totalcal + itemscopy[i].getcost()                return (result, totalval)# Main Function# All values are random    names = ['Ball', 'Gloves', 'Notebook', 'Bagpack', 'Charger', 'Pillow', 'Cakes', 'Pencil']values = [89,90,95,100,90,79,50,10]costs = [123,154,25,145,365,150,95,195]Itemrs = buildlist(names, values, costs)maxcost = 500 # maximum money he have to spendtaken, totvalue = greedy(Itemrs, maxcost, Itm.getvalue)print('Total vaule taken : ', totvalue)# Printing the list of item slected for optimum valuefor i in range(len(taken)):    print('  ', taken[i])

Output

输出量

Total vaule taken :  374   Bagpack   Notebook   Gloves   Ball

翻译自:

贪婪算法 python

转载地址:http://xivzd.baihongyu.com/

你可能感兴趣的文章
原生js大总结二
查看>>
PHP基础
查看>>
UVa 11488 超级前缀集合(Trie的应用)
查看>>
Django 翻译与 LANGUAGE_CODE
查看>>
[转]iOS教程:SQLite的创建数据库,表,插入查看数据
查看>>
【转载】OmniGraffle (一)从工具栏开始
查看>>
初识ionic
查看>>
java 中打印调用栈
查看>>
开发 笔记
查看>>
ajax跨域,携带cookie
查看>>
阶段3 2.Spring_01.Spring框架简介_03.spring概述
查看>>
阶段3 2.Spring_02.程序间耦合_1 编写jdbc的工程代码用于分析程序的耦合
查看>>
阶段3 2.Spring_01.Spring框架简介_04.spring发展历程
查看>>
阶段3 2.Spring_02.程序间耦合_3 程序的耦合和解耦的思路分析1
查看>>
阶段3 2.Spring_02.程序间耦合_5 编写工厂类和配置文件
查看>>
阶段3 2.Spring_01.Spring框架简介_05.spring的优势
查看>>
阶段3 2.Spring_02.程序间耦合_7 分析工厂模式中的问题并改造
查看>>
阶段3 2.Spring_02.程序间耦合_4 曾经代码中的问题分析
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_2 spring中的Ioc前期准备
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_4 ApplicationContext的三个实现类
查看>>