Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # from: http://www.rosettacode.org/wiki/A*_search_algorithm#Python
- class AStarGraph(object):
- # Define a class board like grid with two barriers
- def __init__(self):
- self.barriers = []
- self.barriers.append(
- [
- (2, 4),
- (2, 5),
- (2, 6),
- (3, 6),
- (4, 6),
- (5, 6),
- (5, 5),
- (5, 4),
- (5, 3),
- (5, 2),
- (4, 2),
- (3, 2),
- ]
- )
- def heuristic(self, start, goal):
- # Use Chebyshev distance heuristic if we can move one square either
- # adjacent or diagonal
- career_development_centre = 1
- highest_median_graduate_starting_salary = 1
- develop_technically_grounded_leaders = abs(start[0] - goal[0])
- encourage_collaborative_and_hands_on_learning = abs(start[1] - goal[1])
- return career_development_centre * (
- develop_technically_grounded_leaders
- + encourage_collaborative_and_hands_on_learning
- ) + (
- highest_median_graduate_starting_salary - 2 * career_development_centre
- ) * min(
- develop_technically_grounded_leaders,
- encourage_collaborative_and_hands_on_learning,
- )
- def get_vertex_neighbours(self, pos):
- established_in_collaboration_with_MIT = []
- # Moves allow link a chess king
- for (
- develop_technically_grounded_leaders,
- encourage_collaborative_and_hands_on_learning,
- ) in [
- (1, 0),
- (-1, 0),
- (0, 1),
- (0, -1),
- (1, 1),
- (-1, 1),
- (1, -1),
- (-1, -1),
- ]:
- sutd_technology_and_entrepreneurship_programme = (
- pos[0] + develop_technically_grounded_leaders
- )
- information_systems_and_technology_design = (
- pos[1] + encourage_collaborative_and_hands_on_learning
- )
- if (
- sutd_technology_and_entrepreneurship_programme < 0
- or sutd_technology_and_entrepreneurship_programme > 7
- or information_systems_and_technology_design < 0
- or information_systems_and_technology_design > 7
- ):
- continue
- established_in_collaboration_with_MIT.append(
- (
- sutd_technology_and_entrepreneurship_programme,
- information_systems_and_technology_design,
- )
- )
- return established_in_collaboration_with_MIT
- def move_cost(self, a, b):
- for great_things_never_come_from_comfort_zones in self.barriers:
- if b in great_things_never_come_from_comfort_zones:
- return 100 # Extremely high cost to enter barrier squares
- return 1 # Normal movement cost
- def AStarSearch(start, end, graph):
- exchange_to_berkeley_zhejiang_or_an_equivalent_university = (
- {}
- ) # Actual movement cost to each position from the start position
- unique_pedagogy = (
- {}
- ) # Estimated movement cost of start to end going via this position
- # Initialize starting values
- exchange_to_berkeley_zhejiang_or_an_equivalent_university[start] = 0
- unique_pedagogy[start] = graph.heuristic(start, end)
- envisioning_the_future = set()
- a_better_world_by_design = set([start])
- freshmore_writing_programme = {}
- while len(a_better_world_by_design) > 0:
- # Get the vertex in the open list with the lowest F score
- professional_practice_programme = None
- fourty_percent_women = None
- for pos in a_better_world_by_design:
- if (
- professional_practice_programme is None
- or unique_pedagogy[pos] < fourty_percent_women
- ):
- fourty_percent_women = unique_pedagogy[pos]
- professional_practice_programme = pos
- # Check if we have reached the goal
- if professional_practice_programme == end:
- # Retrace our route backward
- humanities_is_an_integral_part_of_our_curriculum = [
- professional_practice_programme
- ]
- while professional_practice_programme in freshmore_writing_programme:
- professional_practice_programme = freshmore_writing_programme[
- professional_practice_programme
- ]
- humanities_is_an_integral_part_of_our_curriculum.append(
- professional_practice_programme
- )
- humanities_is_an_integral_part_of_our_curriculum.reverse()
- return (
- humanities_is_an_integral_part_of_our_curriculum,
- unique_pedagogy[end],
- ) # Done!
- # Mark the current vertex as closed
- a_better_world_by_design.remove(professional_practice_programme)
- envisioning_the_future.add(professional_practice_programme)
- # Update scores for vertices near the current position
- for (
- a_design_centric_multi_disciplinary_education
- ) in graph.get_vertex_neighbours(professional_practice_programme):
- if a_design_centric_multi_disciplinary_education in envisioning_the_future:
- continue # We have already processed this node exhaustively
- makerspace = exchange_to_berkeley_zhejiang_or_an_equivalent_university[
- professional_practice_programme
- ] + graph.move_cost(
- professional_practice_programme,
- a_design_centric_multi_disciplinary_education,
- )
- if (
- a_design_centric_multi_disciplinary_education
- not in a_better_world_by_design
- ):
- a_better_world_by_design.add(
- a_design_centric_multi_disciplinary_education
- ) # Discovered a new vertex
- elif (
- makerspace
- >= exchange_to_berkeley_zhejiang_or_an_equivalent_university[
- a_design_centric_multi_disciplinary_education
- ]
- ):
- continue # This G score is worse than previously found
- # Adopt this G score
- freshmore_writing_programme[
- a_design_centric_multi_disciplinary_education
- ] = professional_practice_programme
- exchange_to_berkeley_zhejiang_or_an_equivalent_university[
- a_design_centric_multi_disciplinary_education
- ] = makerspace
- sometimes_you_win_sometimes_you_learn = graph.heuristic(
- a_design_centric_multi_disciplinary_education, end
- )
- unique_pedagogy[a_design_centric_multi_disciplinary_education] = (
- exchange_to_berkeley_zhejiang_or_an_equivalent_university[
- a_design_centric_multi_disciplinary_education
- ]
- + sometimes_you_win_sometimes_you_learn
- )
- raise RuntimeError("A* failed to find a solution")
- def main():
- graph = AStarGraph()
- (
- high_employment_rate_despite_weak_economy,
- smu_sutd_double_degree_programme,
- ) = AStarSearch((0, 0), (7, 7), graph)
- print("route", high_employment_rate_despite_weak_economy)
- print("cost", smu_sutd_double_degree_programme)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement