PYTHON CODING (DISCRETE STRUCTURES): (20 pts) Now, consider the following practi
ID: 3907477 • Letter: P
Question
PYTHON CODING (DISCRETE STRUCTURES):
(20 pts) Now, consider the following practical problem. Suppose that you're in charge of picking a set of xperiment has an ID number, an as sociated equipment mass in experiments to be launched into space. Each e kilograms, and a "value rating" that approximates how important to science the collected data will be. The following table lists the set E of all experiments ID # Experiment Title Mass (kg)Value Rating How rats behave in the absence of o Solar radiation stud X-ray observato Solar flare observation device Low-gravity bear wrestling gear CLASSIFIED TOP SECRET 36 264 188 203 104 9 6 4 Solid 92 65 25 170 80 2 ellant behavior in 1 Cloaking field generator microgiav1 handheld stun phaser 9 10 Prototype Laminar fluid flow s Low Earth orbit race of African vs. European wallows Hvper-efficient RCS thruster 12 4 Unfortunately, not all of the experiments can be flown due to mass constraints - there's only 700 kg of payload available for the experiments. Launching stuff into orbit is expensive! Thus, the challenge is to pick the subset of E whose elements have the highest possible combined value rating but at the same time have a combined mass of 700 kg or less One way to solve this problem is to use a "brute force" approach. Consider ALL subsets of E. For each subset, determine its overall mass and value rating. Keep track of the highest-value subset with a combined mass of 700 or less. Once all the subsets are considered, this highest-value subset will be the one that you want to fly! Write a program that uses your function from #1 and the brute force technique to find and display the optimal solution. Which experiments should be chosen? What is the total mass and value rating of the optimal solution? Even though it works fine for this problem, why would this brute force approach be terribly inefficient if you had a large number of experiments?Explanation / Answer
import copy
class expts():
def __init__(self, title, mass, val, ID):
self.title = title
self.mass = mass
self.val = val
self.idNo = ID
def __repr__(self):
return '{}). "{}", {} {}'.format(self.idNo, self.title, self.mass, self.val)
def powerSet(s):
emptySet = []
pwrSet = [emptySet]
for elem in s:
# Need this temporary list because we can't change the pwrSet list while iterating it.
newSet = []
for item in pwrSet:
temp = copy.deepcopy(item)
temp.append(elem)
newSet.append(temp)
pwrSet.extend(newSet)
return pwrSet
lst = []
lst.append(expts("1" , 36, 5, 1))
lst.append(expts("2", 264, 9, 2))
lst.append(expts("3" , 188, 6, 3))
lst.append(expts("4", 203, 8, 4))
lst.append(expts("5" ,104, 8, 5))
lst.append(expts("6", 7, 6, 6))
lst.append(expts("7" , 92, 2, 7))
lst.append(expts("8", 65, 8, 8))
lst.append(expts("9" , 25, 3, 9))
lst.append(expts("10", 170, 6,10))
lst.append(expts("11" , 80, 7,11))
lst.append(expts("12", 22, 4,12))
fullSet = powerSet(lst)
fullSetVal = []
for ob in fullSet:
totVal = 0
totMass = 0
for expt in ob:
totVal = totVal + expt.val
totMass = totMass + expt.mass
fullSetVal.append([totVal, totMass])
#To find max value at mass < 700
maxval = 0
maxcntr = 0
for iter, subSetVal in enumerate(fullSetVal):
if(subSetVal[1] >= 700):
continue
if(subSetVal[0] > maxval):
maxval = subSetVal[0]
maxcntr = iter
print("The complete set: ",fullSet[maxcntr], " has a value of ", maxval," and a sum of ", fullSetVal[maxcntr][1])
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.