Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 20 Online
OpenStudy (anonymous):

6.00 Pset 11 - Help with constraints? Hi all, I've been stuck for days on PS11. I have been building off the code presented in the lectures but am totally stuck when it comes to adding constraints. Can anyone help? I'm posting my code in a comment below.

OpenStudy (anonymous):

The code that I'm specifically referring to is a function I've named called "shortestPathWeightedConstraintsGabi". The previous three functions were iterations to get to this point.

OpenStudy (mathmate):

Constraints: If you are doing a brute-force search, all you need to do is to check that a particular path satisfies \(both\) given constraints of total distance and outdoor distance. If you find one (feasible solution), then put it aside. If you find another feasible solution, compare with the previous and keep only the one with a lesser total distance. At the end of the search, the retained feasible solution will be the feasible path with minimum total distance as required.

OpenStudy (anonymous):

Thanks - I think conceptually I understand it, I'm just having trouble with the implementation. Here's the code that (I thought) goes through that process: def shortestPathWeightedConstraintsGabi(graph, start, end, maxTotalDist, maxDistOutdoors, visited = []): start = Node(start) end = Node(end) if not (graph.hasNode(start) and graph.hasNode(end)): raise ValueError('Start or end not in graph.') path = [str(start)] if start == end: return path shortest = None for node in graph.childrenOf(start): if (str(node) not in visited): visited = visited + [str(node)] #new list newPath = shortestPathWeightedConstraintsGabi(graph, node, end, maxTotalDist, maxDistOutdoors, visited) if newPath == None: continue if (shortest == None or calcTotalDist(newPath, graph) < calcTotalDist(shortest, graph)): if calcTotalDist(newPath, graph) < maxTotalDist and calcDistOutdoors(newPath, graph) < maxDistOutdoors: shortest = newPath if shortest != None: path = path + shortest else: path = None return path I've tested my custom functions (calcTotalDist and calcDistOutdoors) and I know they return the correct integers. Is there something I'm missing/not seeing here?

OpenStudy (anonymous):

Here's a different implementation I've also been testing that breaks when I put in the constraints: def shortestPathWeightedConstraints2(graph, start, end, path, shortest, maxTotalDist, maxDistOutdoors): start = Node(start) end = Node(end) if not (graph.hasNode(start) and graph.hasNode(end)): raise ValueError('Start or end not in graph.') path = path + [str(start)] #print 'Current DFS path:', path #print 'Total Dist Outdoors:', calcDistOutdoors(path, graph) if start == end: return path #shortest = None for node in graph.childrenOf(start): if (str(node) not in path): newPath = shortestPathWeightedConstraints2(graph, node, end, path, shortest, maxTotalDist, maxDistOutdoors) if shortest == None or calcTotalDist(newPath, graph) < calcTotalDist(shortest,graph): if newPath != None: if calcTotalDist(newPath, graph) < maxTotalDist and calcDistOutdoors(newPath, graph) < maxDistOutdoors: print 'FOUND BETTER PATH' shortest = newPath return shortest

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!