MIT6 0002F16 Lecture 1 - MIT OpenCourseWare · An objective function that is to be maximized or...
Transcript of MIT6 0002F16 Lecture 1 - MIT OpenCourseWare · An objective function that is to be maximized or...
![Page 1: MIT6 0002F16 Lecture 1 - MIT OpenCourseWare · An objective function that is to be maximized or minimized, e.g., #Minimize time spent traveling from New York to Boston A set of constraints](https://reader035.fdocuments.net/reader035/viewer/2022070800/5f024be47e708231d4039059/html5/thumbnails/1.jpg)
��������������� ����������������������������
����������������� �� ���� � � �������� ��������������� � ���
�������������1
![Page 2: MIT6 0002F16 Lecture 1 - MIT OpenCourseWare · An objective function that is to be maximized or minimized, e.g., #Minimize time spent traveling from New York to Boston A set of constraints](https://reader035.fdocuments.net/reader035/viewer/2022070800/5f024be47e708231d4039059/html5/thumbnails/2.jpg)
������������������������������������������������ !�"��# ��$����%!���� !�"���&�'
�(���%�����!����"�����������$�����)�������%�����%����!�(���%�����!����"����������%���%�����"�������� �)$$������
!"###$������%�����
������������� �
��������
![Page 3: MIT6 0002F16 Lecture 1 - MIT OpenCourseWare · An objective function that is to be maximized or minimized, e.g., #Minimize time spent traveling from New York to Boston A set of constraints](https://reader035.fdocuments.net/reader035/viewer/2022070800/5f024be47e708231d4039059/html5/thumbnails/3.jpg)
� ���%�������# ���������������%���������������# *����+��!�)���������������,�%%�# -�%��!�)�%������"���������)�%��������%
�(���������������# .��!����%%�����������������%����������������"�%��!�)�%�����������%���������������������
�������������������������������,# /���"�����,�������������������%�����)���������%���+������!�%���)������������%�������
�����0�������������+�
����&��������'�������
������������� &
![Page 4: MIT6 0002F16 Lecture 1 - MIT OpenCourseWare · An objective function that is to be maximized or minimized, e.g., #Minimize time spent traveling from New York to Boston A set of constraints](https://reader035.fdocuments.net/reader035/viewer/2022070800/5f024be47e708231d4039059/html5/thumbnails/4.jpg)
� �����������������������������������# (��)�����������"������%�����������%+����"�����������������
�����)�����������������������������)������%%����������$��������������������)��%������������������1���������)����������!�)�����������������������
(�)��������������������!"###*+
������������� 2
![Page 5: MIT6 0002F16 Lecture 1 - MIT OpenCourseWare · An objective function that is to be maximized or minimized, e.g., #Minimize time spent traveling from New York to Boston A set of constraints](https://reader035.fdocuments.net/reader035/viewer/2022070800/5f024be47e708231d4039059/html5/thumbnails/5.jpg)
�/�$������������%�������$� !�"���3�$���������������������������,�����-������!�)����������������-�%%4
(�����,��������������� -���
������������� '
![Page 6: MIT6 0002F16 Lecture 1 - MIT OpenCourseWare · An objective function that is to be maximized or minimized, e.g., #Minimize time spent traveling from New York to Boston A set of constraints](https://reader035.fdocuments.net/reader035/viewer/2022070800/5f024be47e708231d4039059/html5/thumbnails/6.jpg)
�����������)����������"�%��)�����������"�����%������"��"����%�+��Experimental devices that help us to understand something that has happened or to predict the future
�5�����6����������%��3���������%�����%��3��)%����������%�
�������������������
������������� �
Images © sources unknown. All rights reserved. This content is excluded from our CreativeCommons license. For more information, see https://ocw.mit.edu/help/faq-fair-use.
![Page 7: MIT6 0002F16 Lecture 1 - MIT OpenCourseWare · An objective function that is to be maximized or minimized, e.g., #Minimize time spent traveling from New York to Boston A set of constraints](https://reader035.fdocuments.net/reader035/viewer/2022070800/5f024be47e708231d4039059/html5/thumbnails/7.jpg)
�3������� �� �3�������'�2�7%������$)�������8
.���'����.��������������/0��1�����
������������� 9
![Page 8: MIT6 0002F16 Lecture 1 - MIT OpenCourseWare · An objective function that is to be maximized or minimized, e.g., #Minimize time spent traveling from New York to Boston A set of constraints](https://reader035.fdocuments.net/reader035/viewer/2022070800/5f024be47e708231d4039059/html5/thumbnails/8.jpg)
�����������)����������"�%��)�����������"�����%������"��"����%�+��Experimental devices that help us to understand something that has happened or to predict the future
�������������� ��
�3���������%�����%��3��)%����������%�
�������������������
������������� :
Images © sources unknown. All rights reserved. This content is excluded from our CreativeCommons license. For more information, see https://ocw.mit.edu/help/faq-fair-use.
![Page 9: MIT6 0002F16 Lecture 1 - MIT OpenCourseWare · An objective function that is to be maximized or minimized, e.g., #Minimize time spent traveling from New York to Boston A set of constraints](https://reader035.fdocuments.net/reader035/viewer/2022070800/5f024be47e708231d4039059/html5/thumbnails/9.jpg)
2���������� ��������������+
� An objective function that is to be maximized or minimized, e.g.,# Minimize time spent traveling from New York to Boston
�A set of constraints (possibly empty) that must be honored, e.g.,# Cannot spend more than $100# Must be in Boston before 5:00PM
������������� ;
Images © sources unknown. All rights reserved. This content is excluded from our CreativeCommons license. For more information, see https://ocw.mit.edu/help/faq-fair-use.
![Page 10: MIT6 0002F16 Lecture 1 - MIT OpenCourseWare · An objective function that is to be maximized or minimized, e.g., #Minimize time spent traveling from New York to Boston A set of constraints](https://reader035.fdocuments.net/reader035/viewer/2022070800/5f024be47e708231d4039059/html5/thumbnails/10.jpg)
3�����-���������
������������� �
Images © sources unknown. All rights reserved. This content is excluded from our CreativeCommons license. For more information, see https://ocw.mit.edu/help/faq-fair-use.
![Page 11: MIT6 0002F16 Lecture 1 - MIT OpenCourseWare · An objective function that is to be maximized or minimized, e.g., #Minimize time spent traveling from New York to Boston A set of constraints](https://reader035.fdocuments.net/reader035/viewer/2022070800/5f024be47e708231d4039059/html5/thumbnails/11.jpg)
3�����-��������
�<�)�"�+��%��������������"1�����"��������������)������"��,������,��"���!�)���������!�<�)���)%��%�,�������,���������)$$��"���!�)���������!�-������!�)��"������"��"���)$$������,�������"��"����%��+����"���4����+�������# �= �,������,�����%��# �����)�)�����$��������%�,������,�����%��
�������������
Images © sources unknown. All rights reserved. This content is excluded from our CreativeCommons license. For more information, see https://ocw.mit.edu/help/faq-fair-use.
![Page 12: MIT6 0002F16 Lecture 1 - MIT OpenCourseWare · An objective function that is to be maximized or minimized, e.g., #Minimize time spent traveling from New York to Boston A set of constraints](https://reader035.fdocuments.net/reader035/viewer/2022070800/5f024be47e708231d4039059/html5/thumbnails/12.jpg)
�/�1����4��'�����3�����-��������
������������� �
Images © sources unknown. All rights reserved. This content is excluded from our CreativeCommons license. For more information, see https://ocw.mit.edu/help/faq-fair-use.
![Page 13: MIT6 0002F16 Lecture 1 - MIT OpenCourseWare · An objective function that is to be maximized or minimized, e.g., #Minimize time spent traveling from New York to Boston A set of constraints](https://reader035.fdocuments.net/reader035/viewer/2022070800/5f024be47e708231d4039059/html5/thumbnails/13.jpg)
����"����������������������!�������1������ ��� ������"��,������,��������������������������"�������%�����"���$����������"�����/�+�����1��1��$�%����"�1�������������"�������$��+��%��%�������������"��%�������$��"��+������������������/�+�����1��1��$�%����"�1����)�����������������"��"����������������������,�����*$���������1���������� �����,�����*$���������1���������� ���������,���
#5*�3�����-��������6�7��������
������������� &
![Page 14: MIT6 0002F16 Lecture 1 - MIT OpenCourseWare · An objective function that is to be maximized or minimized, e.g., #Minimize time spent traveling from New York to Boston A set of constraints](https://reader035.fdocuments.net/reader035/viewer/2022070800/5f024be47e708231d4039059/html5/thumbnails/14.jpg)
#5*�3�����-��������6�7��������
������������� 2
![Page 15: MIT6 0002F16 Lecture 1 - MIT OpenCourseWare · An objective function that is to be maximized or minimized, e.g., #Minimize time spent traveling from New York to Boston A set of constraints](https://reader035.fdocuments.net/reader035/viewer/2022070800/5f024be47e708231d4039059/html5/thumbnails/15.jpg)
� ����)��������%%�������%����������������$���������"�����������!1�����������%%��)�������$��"�������$���������"��������%%����"��������������������+���%%��$��"����������������"��������%�)��������������"���%%���������"���&��(�����"��������������������������"�������!������"����+�%)������"��%�������
8�����7����&�������
������������� '
![Page 16: MIT6 0002F16 Lecture 1 - MIT OpenCourseWare · An objective function that is to be maximized or minimized, e.g., #Minimize time spent traveling from New York to Boston A set of constraints](https://reader035.fdocuments.net/reader035/viewer/2022070800/5f024be47e708231d4039059/html5/thumbnails/16.jpg)
�-�������������������4�����%%# /�+�����1��1��$�%����"�1����)�����������������"��"����������������������,�����*$���������1���������� �����,�����*$���������1�������������������,��
�-������!�������%����$$������+�%)�������� "�+�4# /�����!���$$�����������!��)������������������������������� ����
�(��������%�1��$��"�������� �������������"�����$���1��"����������������$���6�4# 1��91�'�1���1��:1��;12� 12;�19�&1��'1&9�
�����9���������
��������������������� �
![Page 17: MIT6 0002F16 Lecture 1 - MIT OpenCourseWare · An objective function that is to be maximized or minimized, e.g., #Minimize time spent traveling from New York to Boston A set of constraints](https://reader035.fdocuments.net/reader035/viewer/2022070800/5f024be47e708231d4039059/html5/thumbnails/17.jpg)
�/%��1�����= �,������,�����%��������"�����%!�����������%�>)�����?���������
&���2�������8���� ����+
������������� 9
![Page 18: MIT6 0002F16 Lecture 1 - MIT OpenCourseWare · An objective function that is to be maximized or minimized, e.g., #Minimize time spent traveling from New York to Boston A set of constraints](https://reader035.fdocuments.net/reader035/viewer/2022070800/5f024be47e708231d4039059/html5/thumbnails/18.jpg)
�while knapsack not fullput “best” available item in knapsack
�>)���"�����������������4# @����+�%)��%�# �������������+�# -��"����+�%)�=)����
�����/�&�����������������&�������'�
������������� :
![Page 19: MIT6 0002F16 Lecture 1 - MIT OpenCourseWare · An objective function that is to be maximized or minimized, e.g., #Minimize time spent traveling from New York to Boston A set of constraints](https://reader035.fdocuments.net/reader035/viewer/2022070800/5f024be47e708231d4039059/html5/thumbnails/19.jpg)
�<�)��������)����������������������%�<�)�,����"����)�"�!�)�+�%)����$$������$����1�����1�!�)�%�,�����)���������"������%���>)��!�)�"�+������%������)����1�����1�!�)����?��������������)���������"���9'����%�������"��������"���������������,������,�����%��
&���:�����
������������� ;
![Page 20: MIT6 0002F16 Lecture 1 - MIT OpenCourseWare · An objective function that is to be maximized or minimized, e.g., #Minimize time spent traveling from New York to Boston A set of constraints](https://reader035.fdocuments.net/reader035/viewer/2022070800/5f024be47e708231d4039059/html5/thumbnails/20.jpg)
����?��%��,���������������"����������)��������������"�����������
&�����
������������� ��
��� ��� ���� ����� ������ ����� ���� ����� ����.�%)� :; ;� &� '� ;� 9; ;� �
��%����� �& '2 �': &'2 &�' '� ;' ;'
![Page 21: MIT6 0002F16 Lecture 1 - MIT OpenCourseWare · An objective function that is to be maximized or minimized, e.g., #Minimize time spent traveling from New York to Boston A set of constraints](https://reader035.fdocuments.net/reader035/viewer/2022070800/5f024be47e708231d4039059/html5/thumbnails/21.jpg)
������7���class Food(object):
def __init__(self, n, v, w):self.name = nself.value = vself.calories = w
def getValue(self):return self.value
def getCost(self):return self.calories
def density(self):return self.getValue()/self.getCost()
def __str__(self):return self.name + ': <' + str(self.value)\
+ ', ' + str(self.calories) + '>'������������� �
![Page 22: MIT6 0002F16 Lecture 1 - MIT OpenCourseWare · An objective function that is to be maximized or minimized, e.g., #Minimize time spent traveling from New York to Boston A set of constraints](https://reader035.fdocuments.net/reader035/viewer/2022070800/5f024be47e708231d4039059/html5/thumbnails/22.jpg)
8������������7����
def buildMenu(names, values, calories):"""names, values, calories lists of same length.
name a list of stringsvalues and calories lists of numbersreturns list of Foods"""
menu = []for i in range(len(values)):
menu.append(Food(names[i], values[i],calories[i]))
return menu
������������� ��
![Page 23: MIT6 0002F16 Lecture 1 - MIT OpenCourseWare · An objective function that is to be maximized or minimized, e.g., #Minimize time spent traveling from New York to Boston A set of constraints](https://reader035.fdocuments.net/reader035/viewer/2022070800/5f024be47e708231d4039059/html5/thumbnails/23.jpg)
�����������������7��:���������/
def greedy(items, maxCost, keyFunction):"""Assumes items a list, maxCost >= 0,
keyFunction maps elements of items to numbers"""itemsCopy = sorted(items, key = keyFunction,
reverse = True)result = []totalValue, totalCost = 0.0, 0.0
for i in range(len(itemsCopy)):if (totalCost+itemsCopy[i].getCost()) <= maxCost:
result.append(itemsCopy[i])totalCost += itemsCopy[i].getCost()totalValue += itemsCopy[i].getValue()
return (result, totalValue)
������������� �&
![Page 24: MIT6 0002F16 Lecture 1 - MIT OpenCourseWare · An objective function that is to be maximized or minimized, e.g., #Minimize time spent traveling from New York to Boston A set of constraints](https://reader035.fdocuments.net/reader035/viewer/2022070800/5f024be47e708231d4039059/html5/thumbnails/24.jpg)
&�������������/
def greedy(items, maxCost, keyFunction):itemsCopy = sorted(items, key = keyFunction,
reverse = True)result = []totalValue, totalCost = 0.0, 0.0
for i in range(len(itemsCopy)):if (totalCost+itemsCopy[i].getCost()) <= maxCost:
result.append(itemsCopy[i])totalCost += itemsCopy[i].getCost()totalValue += itemsCopy[i].getValue()
return (result, totalValue)
������������� �2
��������
![Page 25: MIT6 0002F16 Lecture 1 - MIT OpenCourseWare · An objective function that is to be maximized or minimized, e.g., #Minimize time spent traveling from New York to Boston A set of constraints](https://reader035.fdocuments.net/reader035/viewer/2022070800/5f024be47e708231d4039059/html5/thumbnails/25.jpg)
;���������/
def testGreedy(items, constraint, keyFunction):taken, val = greedy(items, constraint, keyFunction)print('Total value of items taken =', val)for item in taken:
print(' ', item)
������������� �'
![Page 26: MIT6 0002F16 Lecture 1 - MIT OpenCourseWare · An objective function that is to be maximized or minimized, e.g., #Minimize time spent traveling from New York to Boston A set of constraints](https://reader035.fdocuments.net/reader035/viewer/2022070800/5f024be47e708231d4039059/html5/thumbnails/26.jpg)
;���������/
def testGreedys(maxUnits):print('Use greedy by value to allocate', maxUnits,
'calories')testGreedy(foods, maxUnits, Food.getValue)print('\nUse greedy by cost to allocate', maxUnits,
'calories')testGreedy(foods, maxUnits,
lambda x: 1/Food.getCost(x))print('\nUse greedy by density to allocate', maxUnits,
'calories')testGreedy(foods, maxUnits, Food.density)
testGreedys(800)
������������� ��
4
![Page 27: MIT6 0002F16 Lecture 1 - MIT OpenCourseWare · An objective function that is to be maximized or minimized, e.g., #Minimize time spent traveling from New York to Boston A set of constraints](https://reader035.fdocuments.net/reader035/viewer/2022070800/5f024be47e708231d4039059/html5/thumbnails/27.jpg)
�%������)������������������!��)��$)�������# lambda A�� 1����1�B����C0�A����������C# ���)������$)��������$������)�����
�������+��!�"���!1����"���� �����%��������������6��������%�������%������������������D��?�E)���def �������
������
������������� �9
![Page 28: MIT6 0002F16 Lecture 1 - MIT OpenCourseWare · An objective function that is to be maximized or minimized, e.g., #Minimize time spent traveling from New York to Boston A set of constraints](https://reader035.fdocuments.net/reader035/viewer/2022070800/5f024be47e708231d4039059/html5/thumbnails/28.jpg)
;���������/
def testGreedys(foods, maxUnits):print('Use greedy by value to allocate', maxUnits,
'calories')testGreedy(foods, maxUnits, Food.getValue)print('\nUse greedy by cost to allocate', maxUnits,
'calories')testGreedy(foods, maxUnits,
lambda x: 1/Food.getCost(x))print('\nUse greedy by density to allocate', maxUnits,
'calories')testGreedy(foods, maxUnits, Food.density)
names = ['wine', 'beer', 'pizza', 'burger', 'fries','cola', 'apple', 'donut', 'cake']
values = [89,90,95,100,90,79,50,10]calories = [123,154,258,354,365,150,95,195]foods = buildMenu(names, values, calories)testGreedys(foods, 750)
�)������
������������� �:
![Page 29: MIT6 0002F16 Lecture 1 - MIT OpenCourseWare · An objective function that is to be maximized or minimized, e.g., #Minimize time spent traveling from New York to Boston A set of constraints](https://reader035.fdocuments.net/reader035/viewer/2022070800/5f024be47e708231d4039059/html5/thumbnails/29.jpg)
�3�F)������$�%���%%!�G������%H��"���������?���%��!��!��%�����%���%%!�������%���%)����
�*�������!��!�������!��%��!����������4# �!�����I����!�7$����1� ���8
2�/����������&��)���+
������������� �;
![Page 30: MIT6 0002F16 Lecture 1 - MIT OpenCourseWare · An objective function that is to be maximized or minimized, e.g., #Minimize time spent traveling from New York to Boston A set of constraints](https://reader035.fdocuments.net/reader035/viewer/2022070800/5f024be47e708231d4039059/html5/thumbnails/30.jpg)
����!�������%���������)�������%%!��$$������
�>)������������%��!��!��%���"���������%)����# D��?���+���,����"���������"������������������
�*���"�������%���)�����?%%�%��,����$���������)%!�������%���%)�����
��������������������������/
������������� &�
![Page 31: MIT6 0002F16 Lecture 1 - MIT OpenCourseWare · An objective function that is to be maximized or minimized, e.g., #Minimize time spent traveling from New York to Boston A set of constraints](https://reader035.fdocuments.net/reader035/viewer/2022070800/5f024be47e708231d4039059/html5/thumbnails/31.jpg)
MIT OpenCourseWarehttps://ocw.mit.edu
6.0002 Introduction to Computational Thinking and Data ScienceFall 2016
For information about citing these materials or our Terms of Use, visit: https://ocw.mit.edu/terms.