예제 #1
0
def main():
    """
    Test Cohen-Sadeh algorithm

    Arguments:
        infile - project file
    """
    # Parse arguments and options
    parser = argparse.ArgumentParser()
    parser.add_argument('infile')
    args = parser.parse_args()

    # Open the input file collecting the required information.
    activities, _, _, _ = fileFormats.load_with_some_format(args.infile, [fileFormats.PPCProjectFileFormat(),
                                                                          fileFormats.PSPProjectFileFormat()])
    successors = dict(((act[1], act[2]) for act in activities))

    gg1 = cohen_sadeh(graph.successors2precedents(successors))
    #subgraph = gg1.focus(787, 875) # Error en Large Tavares

    window = graph.Test()
    window.add_image(graph.pert2image(gg1))
    graph.gtk.main()
    print gg1
    print validation.check_validation(successors, gg1)
    return 0
def main():
    """
    Test Syslo algorithm

    Arguments:
        infile - project file
    """
    # Parse arguments and options
    parser = argparse.ArgumentParser()
    parser.add_argument("infile")
    args = parser.parse_args()

    # Open the input file collecting the required information.
    activities, _, _, _ = fileFormats.load_with_some_format(
        args.infile, [fileFormats.PPCProjectFileFormat(), fileFormats.PSPProjectFileFormat()]
    )
    successors = dict(((act[1], act[2]) for act in activities))

    pert_graph = sysloPolynomial(graph.successors2precedents(successors))
    # subgraph = gg1.focus(787, 875) # Error en Large Tavares

    window = graph.Test()
    window.add_image(graph.pert2image(pert_graph))
    graph.gtk.main()
    print pert_graph
    print validation.check_validation(successors, pert_graph)
    return 0
예제 #3
0
def main():
    """
    Test Mouhoub algorithm

    Arguments:
        infile - project file
    """
    # Parse arguments and options
    parser = argparse.ArgumentParser()
    parser.add_argument('infile')
    args = parser.parse_args()

    # Open the input file collecting the required information.
    activities, _, _, _ = fileFormats.load_with_some_format(args.infile, [fileFormats.PPCProjectFileFormat(),
                                                                          fileFormats.PSPProjectFileFormat()])
    successors = dict(((act[1], act[2]) for act in activities))
    
    pert_graph = mouhoub(graph.successors2precedents(successors))
    
    window = graph.Test()
    window.add_image(graph.pert2image(pert_graph))
    graph.gtk.main()
    print pert_graph
    print validation.check_validation(successors, pert_graph)
    return 0   
예제 #4
0
def pertFinal(actividad):
    """
    Creacion del grafo Pert numerado en orden
    Valor de retorno: grafoRenumerado (grafo final)
    """
    successors = dict(((act[1], act[2]) for act in actividad))
    grafo = Pert()
    grafo = grafo.construct(graph.successors2precedents(successors))
    grafoRenumerado = grafo.renumerar()
    return grafoRenumerado
예제 #5
0
def pertFinal(actividad):
    """
    Creacion del grafo Pert numerado en orden
    Valor de retorno: grafoRenumerado (grafo final)
    """
    successors = dict(((act[1], act[2]) for act in actividad))
    grafo = Pert()
    grafo = grafo.construct(graph.successors2precedents(successors))
    grafoRenumerado = grafo.renumerar()
    return grafoRenumerado
예제 #6
0
def sysloOptimal(prelations):
    """
    Build a PERT graph using Syslo algorithm

    return p_graph pert.PertMultigraph()
    """
    # Adaptation to avoid multiple end nodes
    successors = graph.reversed_prelation_table(prelations)
    end_act = graph.ending_activities(successors)
    
    #Kahn1962.check_cycles(successors)
    prela = successors.copy()

    Columns = namedlist.namedlist('Columns', ['pre', 'blocked', 'dummy', 'suc', 'start_node', 'end_node'])
                            # [0 Predecesors,   1 Blocked, 2 Dummy, 3 Successors, 4 Start node, 5 End node]
                           #   Blocked = (False or Activity with same precedents)  

    
    #Step 0.
    grafo = {}
    alt = graph.successors2precedents(successors)
    grafo = graph.successors2precedents(syslo_table.syslo(prela, grafo, alt))

    #Step 1. Save the new prelation table in a work table
    work_table = {}
    for act, pre in grafo.items():
        if not act in prelations:
            work_table[act] = Columns(pre, False, True, None, None, None)
        else:
            work_table[act] = Columns(pre, False, False, None, None, None)


    #Step 2. Identify Dummy Activities And Identical Precedence Constraint of Diferent Activities
    visited_pred = {}
    for act, columns in work_table.items():
        pred = frozenset(columns.pre)
        if pred not in visited_pred:
            visited_pred[pred] = act
        else:
            columns.blocked = visited_pred[pred]


    #Step 3. Creating nodes
    # (a) find start nodes
    node = 0 # instead of 0, can start at 100 to avoid confusion with activities named with numbers when debugging
    for act, columns in work_table.items():
        if not columns.blocked:
            columns.start_node = node
            node += 1
        if columns.blocked:
            columns.start_node = work_table[columns.blocked].start_node
            
        # Associate activities with their end nodes
        for suc, suc_columns in work_table.items():
            if not suc_columns.blocked:
                if act in suc_columns.pre:
                    columns.suc = suc
                    break


    
    # (b) find end nodes
    graph_end_node = node # Reserve one node for graph end 
    node += 1
    for act, columns in work_table.items():
        suc = columns.suc
        if suc:
            columns.end_node = work_table[suc].start_node
        else:
            # Create needed end nodes, avoiding multiple graph end nodes (adaptation)
            if act in end_act:
                columns.end_node = graph_end_node
            else:
                columns.end_node = node 
                node += 1
    
    # Step 4. Remove redundancy of dummy activities
    vis = []
    for act, columns in work_table.items():
        if columns.dummy == False:
            for q in work_table[act].pre:
                    for w in work_table[act].pre:
                        if q in work_table and w in work_table:
                            if q != w and work_table[q].pre == work_table[w].pre and work_table[q].dummy==True and work_table[w].dummy==True:
                                if w not in vis:
                                    del work_table[w]
                                vis.append(q)
                     
    
    #Step 5. Generate the graph
    pm_graph = pert.PertMultigraph()
    for act, columns in work_table.items():
        _, _, dummy, _, start, end = columns
        pm_graph.add_arc((start, end), (act, dummy))

    p_graph = pm_graph.to_directed_graph()
    return p_graph


    return p_graph
def sysloPolynomial(prelations):

    # Adaptation to avoid multiple end nodes
    successors = graph.reversed_prelation_table(prelations)
    end_act = graph.ending_activities(successors)

    # Step 0. Construct work table with Immediate Predecessors
    Columns = namedlist.namedlist("Columns", ["pre", "blocked", "dummy", "suc", "start_node", "end_node"])
    # [0 Predecesors,   1 Blocked, 2 Dummy, 3 Successors, 4 Start node, 5 End node]
    #   Blocked = (False or Activity with same precedents)

    # Step 1. Create the improper covers
    work_table_pol = makeCover(prelations, successors)

    # Step 2. Syslo Polynomial algorithm
    final = successors.copy()
    visited = []

    for act, pred in prelations.items():
        for v in pred:
            for u in pred:
                if u != v and successors[v] != successors[u] and act not in visited:
                    # Find activity in the improper cover table
                    for key, value in work_table_pol.items():
                        if act in value.w:
                            w = value.w

                    # Find each row that belongs to the predecessors of activity
                    for key, value in work_table_pol.items():
                        if set(value.u).issubset(prelations[act]) and value.u:
                            vertex = set(value.u).pop()
                            # Compare successors of a row with the improper cover of the activity
                            if successors[vertex] != w:
                                for q in value.u:
                                    if final.has_key(q):
                                        final[q] = list(
                                            (set(final[q]) - set(w) | set([str(vertex) + separator + str(act)]))
                                            - set([act])
                                        )
                                    else:
                                        final[q] = list(
                                            set(successors[q]) - set(w) | set([str(vertex) + separator + str(act)])
                                        )
                                final[str(vertex) + separator + str(act)] = [act]

                                for l in w:
                                    visited.append(l)

    final = graph.successors2precedents(final)
    work_table = {}

    for act, pred in final.items():
        work_table[act] = Columns(pred, False, False, None, None, None)
        if act not in prelations:
            work_table[act].dummy = True

    # Step 3. Identify Dummy Activities And Identical Precedence Constraint of Diferent Activities
    visited_pred = {}
    for act, columns in work_table.items():
        pred = frozenset(columns.pre)
        if pred not in visited_pred:
            visited_pred[pred] = act
        else:
            columns.blocked = visited_pred[pred]

    # Step 4. Creating nodes
    # (a) find start nodes
    node = 0  # instead of 0, can start at 100 to avoid confusion with activities named with numbers when debugging
    for act, columns in work_table.items():
        if not columns.blocked:
            columns.start_node = node
            node += 1
        if columns.blocked:
            columns.start_node = work_table[columns.blocked].start_node

        # Associate activities with their end nodes
        for suc, suc_columns in work_table.items():
            if not suc_columns.blocked:
                if act in suc_columns.pre:
                    columns.suc = suc
                    break

    # (b) find end nodes
    graph_end_node = node  # Reserve one node for graph end
    node += 1
    pm_graph = pert.PertMultigraph()
    for act, columns in work_table.items():
        suc = columns.suc
        if suc:
            columns.end_node = work_table[suc].start_node
        else:
            # Create needed end nodes, avoiding multiple graph end nodes (adaptation)
            if act in end_act:
                columns.end_node = node
            else:
                columns.end_node = graph_end_node
                node += 1
        # Generate the graph
        _, _, dummy, _, start, end = columns
        pm_graph.add_arc((start, end), (act, dummy))

    p_graph = pm_graph.to_directed_graph()

    return p_graph
예제 #8
0
def sysloPolynomial(prelations):

    # Adaptation to avoid multiple end nodes
    successors = graph.reversed_prelation_table(prelations)
    end_act = graph.ending_activities(successors)

    #Step 0. Construct work table with Immediate Predecessors
    Columns = namedlist.namedlist('Columns', ['pre', 'blocked', 'dummy', 'suc', 'start_node', 'end_node'])
                            # [0 Predecesors,   1 Blocked, 2 Dummy, 3 Successors, 4 Start node, 5 End node]
                            #   Blocked = (False or Activity with same precedents)


    #Step 1. Create the improper covers
    work_table_pol = makeCover(prelations, successors)
          
   
    # Step 2. Syslo Polynomial algorithm
    final = successors.copy()
    visited = []
       
    for act, pred in prelations.items():
        for v in pred:
            for u in pred:
                if u != v and successors[v] != successors[u] and act not in visited:
                    # Find activity in the improper cover table
                    for key, value in work_table_pol.items():
                        if act in value.w:
                            w = value.w
                          
                    # Find each row that belongs to the predecessors of activity
                    for key, value in work_table_pol.items():
                        if set(value.u).issubset(prelations[act]) and value.u:
                            vertex = set(value.u).pop()
                            # Compare successors of a row with the improper cover of the activity
                            if successors[vertex] != w:
                                for q in value.u: 
                                    if final.has_key(q):
                                        final[q] = list((set(final[q]) - set(w) | set([str(vertex) + separator + str(act)])) - set([act]))       
                                    else:
                                        final[q] = list(set(successors[q]) - set(w) | set([str(vertex) + separator + str(act)]))
                                final[str(vertex) + separator + str(act)] = [act]

                                for l in w:
                                    visited.append(l)
 
        
    final = graph.successors2precedents(final)
    work_table = {}
    
    for act, pred in final.items():
        work_table[act] = Columns(pred, False, False, None, None, None)
        if act not in prelations:
            work_table[act].dummy = True


    #Step 3. Identify Dummy Activities And Identical Precedence Constraint of Diferent Activities
    visited_pred = {}
    for act, columns in work_table.items():
        pred = frozenset(columns.pre)
        if pred not in visited_pred:
            visited_pred[pred] = act
        else:
            columns.blocked = visited_pred[pred]


    #Step 4. Creating nodes
    # (a) find start nodes
    node = 0 # instead of 0, can start at 100 to avoid confusion with activities named with numbers when debugging
    for act, columns in work_table.items():
        if not columns.blocked:
            columns.start_node = node
            node += 1
        if columns.blocked:
            columns.start_node = work_table[columns.blocked].start_node
            
        # Associate activities with their end nodes
        for suc, suc_columns in work_table.items():
            if not suc_columns.blocked:
                if act in suc_columns.pre:
                    columns.suc = suc
                    break


    
    # (b) find end nodes
    graph_end_node = node # Reserve one node for graph end 
    node += 1
    pm_graph = pert.PertMultigraph()
    for act, columns in work_table.items():
        suc = columns.suc
        if suc:
            columns.end_node = work_table[suc].start_node
        else:
            # Create needed end nodes, avoiding multiple graph end nodes (adaptation)
            if act in end_act:
                columns.end_node = node 
            else:
                columns.end_node = graph_end_node
                node += 1 
        # Generate the graph
        _, _, dummy, _, start, end = columns
        pm_graph.add_arc((start, end), (act, dummy))

    p_graph = pm_graph.to_directed_graph()
    
    return p_graph
예제 #9
0
def simulated_annealing(asignation,resources,successors,activities,leveling,nu,phi,minTemperature,maxIteration,numIterations):
    """
    Try to find the best schedule of the project by
        the technique of simulated annealing

    Parameters: asignation (returned by resourcesPerActivity)
                resources (returned by resourcesAvailability)
                successors (the prelations of the activities)
                activities (dictionary with the name of activities and their characteristics)
                leveling (if 0 it will allocate else it will level)
                nu (parameter to configure simulated annealing algorithm)
                phi (parameter to configure simulated annealing algorithm)
                minTemperature (parameter to configure simulated annealing algorithm)
                maxIteration (parameter to configure simulated annealing algorithm)
                numIterations (parameter to configure simulated annealing algorithm)

    Returned value: sch (the best schedule found)
                    schEvaluated (loading sheet of schedule)
                    duration (duration of schedule)
                    alpha (parameter to configure simulated annealing algorithm)
                    tempAux (parameter to configure simulated annealing algorithm)
                    it (number of iterations done)
    """
    predecessors = successors2precedents(successors)

    for a in predecessors.copy():
        if predecessors[a] == []:
            del predecessors[a]

    # sch1 will store the best plannings
    schAux = sch1 = generate(asignation,resources.copy(),copy.deepcopy(predecessors),activities.copy(),leveling)
    sch1Evaluated, loadSheet1, duration1 = evaluate(sch1,leveling,asignation,resources)
    schAuxEvaluated = sch1Evaluated
    durationAux = duration1
    loadSheetAux = loadSheet1
    
    if duration1 == 0:
        return (sch1, sch1Evaluated, duration1, None, None, None)
    if sch1Evaluated == 0:
        return (sch1, sch1Evaluated, duration1, None, None, 0)
        
    tempAux = temperature = (nu / -log(phi)) * sch1Evaluated
    alpha = (minTemperature / temperature) ** (1 / maxIteration)
    if alpha >= 1:
        return (None,None,None,None,None,None)
    it=0
    numIterationsAux = numIterations
    
    while temperature > minTemperature and numIterations != 0:
        it += 1
        sch2 = modify(asignation,resources.copy(),copy.deepcopy(predecessors),activities.copy(),sch1,leveling)
        sch2Evaluated, loadSheet2, duration2 = evaluate(sch2,leveling,asignation,resources)
        if sch2Evaluated <= sch1Evaluated:
            loadSheet1 = loadSheet2
            duration1 = duration2
            sch1 = sch2
            sch1Evaluated = sch2Evaluated
            # Set numIterations to initial value
            numIterations = numIterationsAux 
        else:
            numIterations -= 1 
            r = random.random()
            m = exp(-(sch2Evaluated-sch1Evaluated) / temperature)
            if r < m:
                if schAuxEvaluated > sch1Evaluated:
                    loadSheetAux = loadSheet1
                    durationAux = duration1
                    schAux = sch1 
                    schAuxEvaluated = sch1Evaluated              
                sch1 = sch2
                sch1Evaluated = sch2Evaluated
                loadSheet1 = loadSheet2
                duration1 = duration2   
                    
        temperature = alpha * temperature

    if sch1Evaluated <= schAuxEvaluated:
        return (sch1, sch1Evaluated, duration1, alpha, tempAux, it)
    else:
        return (schAux, schAuxEvaluated, durationAux, alpha, tempAux, it)
예제 #10
0
def mouhoub(prelations):
    """
    Build a PERT graph using Mouhoub algorithm
    
    prelations = {'activity': ['predecesor1', 'predecesor2'...}

    return p_graph pert.PertMultigraph()
    """
    
    Columns = namedlist.namedlist('Columns', ['pre', 'su', 'blocked', 'dummy', 'suc', 'start_node', 'end_node', 'aux'])
                            # [0 Predecesors, 1 Successors, 2 Blocked, 3 Dummy, 4 Blocked successor, 5 Start node, 6 End node, 7 Auxiliar ]
                            # Blocked = (False or Activity with same precedents) 


    # Adaptation to avoid multiple end nodes
    successors = graph.reversed_prelation_table(prelations)
    successors_copy = graph.reversed_prelation_table(prelations.copy())
    end_act = graph.ending_activities(successors)

  
    # Step 0. Remove Z Configuration. Update the prelation table in complete_bipartite dictionary
    complete_bipartite = successors
    complete_bipartite.update(zConfiguration.zconf(successors))  
    
    
    # STEPS TO BUILD THE PERT GRAPH
    
    #Step 1. Save the prelations in the work table
    complete_bipartite = graph.successors2precedents(complete_bipartite) 
    
    work_table = {}
    for act, sucesores in complete_bipartite.items():
        work_table[act] = Columns(set(sucesores), successors[act], None, False, None, None, None, None)
        if act not in prelations:
            work_table[act].dummy = True
          
          
    #Step 2. Identify Identical Precedence Constraint of Diferent Activities
    visited_pred = {}
    for act, columns in work_table.items():
        pred = frozenset(columns.pre)
        if pred not in visited_pred:
            visited_pred[pred] = act
        else:
            columns.blocked = visited_pred[pred]
                   
            
    #Step 3. Creating nodes
    # (a) Find start nodes
    node = 0 # instead of 0, can start at 100 to avoid confusion with activities named with numbers when debugging
    for act, columns in work_table.items():
        if not columns.blocked:
            columns.start_node = node
            node += 1
        if columns.blocked:
            columns.start_node = work_table[columns.blocked].start_node
        
        # Associate activities with their end nodes
        for suc, suc_columns in work_table.items():
            if not suc_columns.blocked:
                if act in suc_columns.pre:
                    columns.suc = suc
                    break

    

    # (b) Find end nodes
    graph_end_node = node # Reserve one node for graph end 
    node += 1
    for act, columns in work_table.items():
        suc = columns.suc
        if suc:
            columns.end_node = work_table[suc].start_node
        else:
            # Create needed end nodes, avoiding multiple graph end nodes (adaptation)
            if act in end_act:
                columns.end_node = graph_end_node 
            else:
                columns.end_node = node
                node += 1   


    #Step 4. MOUHOUB algorithm rules to remove extra dummy activities
    
    mouhoubRules.rule_1(successors_copy, work_table)
    
    G2 = mouhoubRules.rule_2(prelations, work_table)
    
    G3 = mouhoubRules.rule_3(G2, work_table)
    
    G4 = mouhoubRules.rule_4(G3, work_table)
    
    G5_6 = mouhoubRules.rule_5_6(successors_copy, work_table, G4)
    
    G3a = mouhoubRules.rule_3(G5_6, work_table)
    
    G4a = mouhoubRules.rule_4(G3a, work_table)
    
    G7 =  mouhoubRules.rule_7(successors_copy, successors, G4a, node)
    
    work_table_final = {}
    for act, sucesores in G7.items():
        work_table_final[act] = Columns([], [], [], sucesores.dummy, sucesores.suc, sucesores.start_node, sucesores.end_node, [])
    
    
    #Step 5. Delete Dummy Cycles
    for act, sucesores in work_table_final.items():
        for act2, sucesores2 in work_table_final.items():
            if act != act2:
                if sucesores.end_node == sucesores2.end_node and sucesores.start_node == sucesores2.start_node:
                    if act not in successors:
                        del work_table_final[act]
                   
                   
    #Step 6. Generate the graph
    pm_graph = pert.PertMultigraph()
    for act, columns in work_table_final.items():
        _, _, _, dummy, _, start, end, _ = columns
        pm_graph.add_arc((start, end), (act, dummy))
    p_graph = pm_graph.to_directed_graph()
    
    return p_graph