A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 one year ago
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.
anonymous
 one year ago
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.

This Question is Open

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0The 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.

mathmate
 one year ago
Best ResponseYou've already chosen the best response.0Constraints: If you are doing a bruteforce 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.

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0Thanks  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?

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0Here'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
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.