Esempio n. 1
0
def generateParallelChain(agent, task):
    total_event = ((2 * task) + 1) * agent + 1

    while True:
        new = STN()
        new.addVertex(0)

        for i in range(total_event):
            new.addVertex(i + 1)

        contingent = True
        for i in range(agent):
            start = ((2 * task) + 1) * i + 1
            end = ((2 * task) + 1) * (i + 1)
            new.addEdge(0, start, 0, 15)

            for j in range(start, end):
                type = 'stcu' if contingent else 'stc'
                contingent = not contingent

                if type == 'stcu':
                    # low = round(random.uniform(10, 20), 2)
                    # high = round(random.uniform(30, 40), 2)
                    low = random.randint(10, 20)
                    high = random.randint(30, 40)
                    new.addEdge(j, j + 1, low, high, type='stcu')
                else:
                    # low = round(random.uniform(5, 10), 2)
                    # high = round(random.uniform(30, 35), 2)
                    low = random.randint(5, 10)
                    high = random.randint(35, 40)
                    new.addEdge(j, j + 1, low, high)

            new.addEdge(end, total_event, -10, 10)

        num_activity = (2 * task) + 1
        max_length = max([e.Cij + e.Cji for e in list(new.edges.values())])
        up_bound = max_length * num_activity

        # low = round(random.uniform(0.35*up_bound, 0.45*up_bound), 2)
        # high = round(random.uniform(0.5*up_bound, 0.6*up_bound), 2)
        low = random.randint(int(0.45 * up_bound), int(0.53 * up_bound))
        high = random.randint(int(0.55 * up_bound), int(0.65 * up_bound))
        new.addEdge(0, total_event, low, high)

        print("\n\nChecking consistensy...")
        if not new.isConsistent():
            continue

        print("Checking Dynamic Controllability...")
        try:
            result, conflicts, bounds, weight = DC_Checker(new.copy(),
                                                           report=False)
        except Exception:
            continue

        if result:
            return new
Esempio n. 2
0
def set_dynamic_zeropoint(network: STN):
    network = network.copy()
    largish = 1000000.0

    if ZERO_ID not in network.verts:
        network.addVertex(ZERO_ID)

    adjacent_events = set(network.getAdjacent(ZERO_ID))
    for event in network.verts:
        if (event not in adjacent_events) and (event != ZERO_ID):
            network.addEdge(ZERO_ID, event, 0.0, largish)

    return network
Esempio n. 3
0
def loadSTNfromJSONobj(jsonSTN, using_PSTN=False):
    stn = STN()

    # Add the root vertex and put it in the T_x set
    stn.addVertex(0)

    # Add the vertices
    for v in jsonSTN['nodes']:
        stn.addVertex(v['node_id'])

    # Add the edges
    for e in jsonSTN['constraints']:
        if using_PSTN and 'distribution' in e:
            stn.addEdge(e['first_node'], e['second_node'],
                        float(e['min_duration']), float(e['max_duration']),
                        e['type'], e['distribution']['name'])
        else:
            stn.addEdge(e['first_node'], e['second_node'],
                        float(e['min_duration']), float(e['max_duration']),
                        e['type'])

    return stn
Esempio n. 4
0
def generateChain(task, free):
    totalEvent = 2 * (task + 1)

    while True:
        new = STN()
        for i in range(totalEvent):
            new.addVertex(i)

        L = [random.randint(0, 100) for i in range(task)]
        s = sum(L)
        L = [int(x / s * free) for x in L]
        diff = free - sum(L)
        L[-1] += diff

        bounds = []
        for i in range(totalEvent - 1):
            type = 'stcu' if i % 2 == 0 else 'stc'
            if type == 'stcu':
                lowBound = random.randint(0, 50)
                length = random.randint(1, 50)
                bounds.append((lowBound, lowBound + length))
                new.addEdge(i, i + 1, lowBound, lowBound + length, type='stcu')
            else:
                lowBound = random.randint(0, 100)
                length = L[int((i - 1) / 2)]
                bounds.append((lowBound, lowBound + length))
                new.addEdge(i, i + 1, lowBound, lowBound + length)

        low = sum([x[0] for x in bounds])
        high = sum([x[1] for x in bounds])
        S = sum([e.Cij + e.Cji for e in list(new.contingentEdges.values())])
        # makespan = random.randint(int(0.5*low), low)
        makespan = low + int(0.6 * S)
        print(low, makespan, high)
        new.addEdge(0, task * 2 + 1, 0, makespan)

        if new.isConsistent():
            return new
Esempio n. 5
0
def loadSTNfromJSONobj(jsonSTN, reduction=True, using_PSTN=True):
    stn = STN()

    # Add the root vertex and put it in the T_x set
    stn.addVertex(0, 0, None)
    # TODO: wtf? Why are we executing a point outside of a simulation in the
    # first place?
    stn.execute(0)
    agents = []

    # Add the vertices
    for v in jsonSTN['nodes']:
        # Accumulate a list of all the owners to retrieve the agents.
        if not v['owner_id'] in agents:
            agents.append(v['owner_id'])

        # We don't necessarily need a location, just set it to None if we don't.
        if not ('location' in v):
            v['location'] = None

        stn.addVertex(v['node_id'], v['local_id'], v['owner_id'],
                      v['location'])

        # TODO: Change edge adding to allow integers to refer to vertecies,
        # rather than actually connecting integers together. (silly legacy support)
        stn.addEdge(0, v['node_id'], float(v['min_domain']),
                    float(v['max_domain']))
        if 'executed' in v:
            if v['executed']:
                stn.execute(v['node_id'])

    # Add the edges
    for e in jsonSTN['constraints']:
        if 'distribution' in e and using_PSTN:
            stn.addEdge(e['first_node'], e['second_node'],
                        float(e['min_duration']), float(e['max_duration']),
                        e['distribution']['name'])
        else:
            stn.addEdge(e['first_node'], e['second_node'],
                        float(e['min_duration']), float(e['max_duration']))

    #numAgents = jsonSTN['num_agents']

    stn.numAgents = len(agents)  # NOTE: deprecated, agents replaces numAgents.
    stn.agents = agents

    if reduction:
        # Triangulate the STN
        stnCopy = stn.copy()
        agentWait = []
        # Set up the wait timer for agent load balancing
        for a in stn.agents:
            agentWait.append(0)

        # Perform the reduction
        while len(stnCopy.verts) > 1:
            for a in stn.agents:
                if agentWait[a] == 0:
                    created = stnreduce(stnCopy, a, stn)
                    agentWait[a] = created
                else:
                    agentWait[a] -= 1

    # Return back an dictionary for easy labelling of return types.
    # FIXME: Remove the deprecated numAgents value
    output_dict = {'stn': stn, 'agent_count': stn.numAgents}

    # Ideally, this would return a class, however, this is already butchering
    # quite a bit of legacy support, and a class would end up being even more
    # damaging.
    return output_dict
Esempio n. 6
0
def loadSTNfromJSONobj(jsonSTN, using_PSTN=True):
    stn = STN()

    # Add the root vertex and put it in the T_x set
    stn.addVertex(0)

    # Add the vertices
    for v in jsonSTN['nodes']:
        stn.addVertex(v['node_id'])
        if 'min_domain' in v:       
            stn.addEdge(0, v['node_id'], float(v['min_domain']),
                float(v['max_domain']))
        else:
            if not stn.edgeExists(0, v['node_id']):
                stn.addEdge(0,v['node_id'], float(0), float('inf'))


    # Add the edges
    for e in jsonSTN['constraints']:
        if stn.edgeExists(e['first_node'], e['second_node']):
            stn.updateEdge(e['first_node'], e['second_node'],float(e['max_duration']))
            stn.updateEdge(e['second_node'], e['first_node'],float(e['min_duration']))
        else:
            if using_PSTN and 'distribution' in e:
                stn.addEdge(e['first_node'], e['second_node'],
                            float(max(0,e['min_duration'])), float(e['max_duration']),
                            e['distribution']['type'], e['distribution']['name'])
            elif 'type' in e:
                if e['type'] == 'stcu':
                    dist = "U_"+str(e['min_duration']) + "_" + str(e['max_duration'])
                    stn.addEdge(e['first_node'], e['second_node'],
                        float(max(0,e['min_duration'])), float(e['max_duration']),
                        e['type'], dist)
                else:
                    stn.addEdge(e['first_node'], e['second_node'],
                                float(e['min_duration']), float(e['max_duration']),
                                e['type'])
            else:
                stn.addEdge(e['first_node'], e['second_node'],
                            float(e['min_duration']), float(e['max_duration']))

    return stn