Exemple #1
0
def path_mileage(path):
    # return the total mileage of the input path
    mileage=0
    newpath = path[::-1]
    q=0
    for state,idx in newpath:
        if (q == 0):
            lastplace=''
            q+=1
            continue
            
        if (state.location == ''):
            pass
        else:
#            print(lastplace+"->"+state.location,inc.get_miles(lastplace,state.location))
            mileage+=inc.get_miles(lastplace,state.location)
            lastplace=state.location
        
    return mileage
Exemple #2
0
def solve(Widgets, mileage=False):
    # heuristic: average number of parts needed to
    # complete each widget
    
    if (mileage == False):
        print("Solving for minimum number of stops...")
    else:
        print("Solving for minimum mileage...")

    frontier = []  # this will be a list of nodes
    
    startNode1 = inc.Node('',Widgets,0,None)
    
    frontier.append(startNode1)
    prevcost = 0
    step=0
    
    while len(frontier) > 0:
        step+=1
        
        # decide what node to expand next
        mineval = 99999999
        minnode = None
        for n in frontier:
            if (n.evaluation < mineval):
                mineval = n.evaluation
                minnode = n
        
        curWidgets = minnode.widgets

        
        if minnode.cost != prevcost:
            prevcost = minnode.cost
            #print("cost:",prevcost)
        
        # check if widgets are all done
        alldone = True
        for w in curWidgets:
            if w.done == False:
                alldone = False
                break
        if (alldone == True):
            break
        
        if (mileage == False):
            newcost = [minnode.cost+1,minnode.cost+1,minnode.cost+1,minnode.cost+1,minnode.cost+1]
        else:
            newcost = [minnode.cost+inc.get_miles(minnode.value,'A'),
                       minnode.cost+inc.get_miles(minnode.value,'B'),
                       minnode.cost+inc.get_miles(minnode.value,'C'),
                       minnode.cost+inc.get_miles(minnode.value,'D'),
                       minnode.cost+inc.get_miles(minnode.value,'E')]
            
        # generate new nodes
        
        newNodeA = inc.Node('A',add_component(curWidgets, 'A'), newcost[0], minnode, mileage)
        newNodeB = inc.Node('B',add_component(curWidgets, 'B'), newcost[1], minnode, mileage)
        newNodeC = inc.Node('C',add_component(curWidgets, 'C'), newcost[2], minnode, mileage)
        newNodeD = inc.Node('D',add_component(curWidgets, 'D'), newcost[3], minnode, mileage)
        newNodeE = inc.Node('E',add_component(curWidgets, 'E'), newcost[4], minnode, mileage)
        
        if (newNodeA.evaluation != mineval+1 and minnode.value != 'A'):  # ignore new states where nothing was accomplished
                                                # only applies for minimum steps
            frontier.append(newNodeA)
        if (newNodeB.evaluation != mineval+1 and minnode.value != 'B'):
            frontier.append(newNodeB)
        if (newNodeC.evaluation != mineval+1 and minnode.value != 'C'):
            frontier.append(newNodeC)
        if (newNodeD.evaluation != mineval+1 and minnode.value != 'D'):
            frontier.append(newNodeD)
        if (newNodeE.evaluation != mineval+1 and minnode.value != 'E'):
            frontier.append(newNodeE)
        
        frontier.remove(minnode)
        
    path = traceback(minnode)
    
    if (mileage == False):
        print("Path with minimum stops:")
    else:
        print("Path with least mileage:")
    pathstr = ""
    for n in path:
        pathstr+=n.value
    print(pathstr)
    if (mileage == False):
        print("Stops:",minnode.cost)
    else:
        print("Miles:",minnode.cost)
    print("Nodes expanded:", step)
Exemple #3
0
def mileage_chars(inp):
    summ=0
    for i in range(1,len(inp)):
        summ+=inc.get_miles(inp[i-1],inp[i])
#        print(inp[i-1]+"->"+inp[i],inc.get_miles(inp[i-1],inp[i]))
    return summ
Exemple #4
0
def solve(Widgets, mileage=False):
    # heuristic: average number of parts needed to
    # complete each widget

    if (mileage == False):
        print("Solving for minimum number of stops...")
    else:
        print("Solving for minimum mileage...")

    frontier = []  # this will be a list of nodes

    startNode1 = inc.Node('', Widgets, 0, None)

    frontier.append(startNode1)
    prevcost = 0
    step = 0

    lasttime = time()
    looptimes = [0, 0, 0, 0, 0]
    il = 0

    while len(frontier) > 0:
        loopstart = time()
        step += 1
        if (time() - lasttime > 5):
            lasttime = time()
            print("Frontier:", len(frontier))
            print("Expanded:", step)
            print("Avg loop time:", mean(looptimes))
            print()

        # decide what node to expand next
        mineval = 99999999
        minnode = None
        #        for n in frontier:
        #            if (n.cost < mineval):
        #                mineval = n.cost
        #                minnode = n
        minnode = frontier[0]
        mineval = minnode.cost
        frontier.pop(0)

        curWidgets = minnode.widgets

        if minnode.cost != prevcost:
            prevcost = minnode.cost
            #print("cost:",prevcost)

        # check if widgets are all done
        alldone = True
        for w in curWidgets:
            if w.done == False:
                alldone = False
                break
        if (alldone == True):
            break

        if (mileage == False):
            newcost = [
                minnode.cost + 1, minnode.cost + 1, minnode.cost + 1,
                minnode.cost + 1, minnode.cost + 1
            ]
        else:
            newcost = [
                minnode.cost + inc.get_miles(minnode.value, 'A'),
                minnode.cost + inc.get_miles(minnode.value, 'B'),
                minnode.cost + inc.get_miles(minnode.value, 'C'),
                minnode.cost + inc.get_miles(minnode.value, 'D'),
                minnode.cost + inc.get_miles(minnode.value, 'E')
            ]

        # generate new nodes

        newNodeA = inc.Node('A', add_component(curWidgets, 'A'), newcost[0],
                            minnode, mileage)
        newNodeB = inc.Node('B', add_component(curWidgets, 'B'), newcost[1],
                            minnode, mileage)
        newNodeC = inc.Node('C', add_component(curWidgets, 'C'), newcost[2],
                            minnode, mileage)
        newNodeD = inc.Node('D', add_component(curWidgets, 'D'), newcost[3],
                            minnode, mileage)
        newNodeE = inc.Node('E', add_component(curWidgets, 'E'), newcost[4],
                            minnode, mileage)

        pos = 0

        if (minnode.value != 'A' and
            ((mileage == False and inc.average_parts_needed(newNodeA.widgets)
              != inc.average_parts_needed(curWidgets)) or
             ((mileage == True and newNodeA.cost - minnode.cost <=
               inc.get_min_miles(minnode.value, 'A'))))):
            # save time
            pos = find_position(frontier, newNodeA.cost)
            frontier.insert(pos, newNodeA)
        if (minnode.value != 'B' and
            ((mileage == False and inc.average_parts_needed(newNodeB.widgets)
              != inc.average_parts_needed(curWidgets)) or
             ((mileage == True and newNodeB.cost - minnode.cost <=
               inc.get_min_miles(minnode.value, 'B'))))):
            pos = find_position(frontier, newNodeB.cost)
            frontier.insert(pos, newNodeB)
        if (minnode.value != 'C' and
            ((mileage == False and inc.average_parts_needed(newNodeC.widgets)
              != inc.average_parts_needed(curWidgets)) or
             ((mileage == True and newNodeC.cost - minnode.cost <=
               inc.get_min_miles(minnode.value, 'C'))))):
            pos = find_position(frontier, newNodeC.cost)
            frontier.insert(pos, newNodeC)
        if (minnode.value != 'D' and
            ((mileage == False and inc.average_parts_needed(newNodeD.widgets)
              != inc.average_parts_needed(curWidgets)) or
             ((mileage == True and newNodeD.cost - minnode.cost <=
               inc.get_min_miles(minnode.value, 'D'))))):
            pos = find_position(frontier, newNodeD.cost)
            frontier.insert(pos, newNodeD)
        if (minnode.value != 'E' and
            ((mileage == False and inc.average_parts_needed(newNodeE.widgets)
              != inc.average_parts_needed(curWidgets)) or
             ((mileage == True and newNodeE.cost - minnode.cost <=
               inc.get_min_miles(minnode.value, 'E'))))):
            pos = find_position(frontier, newNodeE.cost)
            frontier.insert(pos, newNodeE)

        looptimes[il] = time() - loopstart
        il += 1
        if (il > 4):
            il = 0

    path = traceback(minnode)

    if (mileage == False):
        print("Path with minimum stops:")
    else:
        print("Path with least mileage:")
    pathstr = ""
    for n in path:
        pathstr += n.value
    print(pathstr)
    if (mileage == False):
        print("Stops:", minnode.cost)
    else:
        print("Miles:", minnode.cost)
    print("Nodes expanded:", step)
    return step