A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 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.

  • This Question is Open
  1. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  2. mathmate
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  3. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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?

  4. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  5. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

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
  • 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.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.