Exemple #1
0
def get_fitness(genes: list, max_hop: int = 20, constructor=None):
    if constructor is None:
        raise Exception("Error: Must init constructor!")
    g = constructor.gen_graph(genes)
    graph = g.graph
    vertices = g.vertices
    all_values = itertools.chain.from_iterable(graph.values())
    adjacent = list(set(all_values))
    list_send = []
    list_send_receive = []

    params, _ = parse_config()

    for i in vertices:
        if i in adjacent:
            if len(graph[i]) != 0:
                list_send_receive.append(i)
            else:
                list_send.append(i)

    result = 0
    for i in list_send:
        for j in g.vertices:
            if i in graph[j]:
                result += params[
                    'E_TX'] + params['epsilon_fs'] * i.get_distance(j)**2
                break
    for i in list_send_receive:
        for j in g.vertices:
            if i in graph[j]:
                result += i.num_child * (
                    params['E_RX'] + params['E_DA']
                ) + params['epsilon_mp'] * i.get_distance(j)**4
                break
    result *= params['l']

    for i in vertices:
        if i.hop > max_hop:
            print(i.hop)
            return float('inf')
        # result += 9999 * max(i.hop - max_hop, 0)

    return result
Exemple #2
0
            if j == inp.num_of_relay_positions:
                print("|", end='|')
        print()
        if i == inp.num_of_relay_positions:
            print("---" * 20)

    print("========")

    print('optimal value = ', solver.Objective().Value())
    print()
    print("Time = ", solver.WallTime(), " milliseconds")
    return dict_constant["l"] * solver.Objective().Value(
    ), connect_matrix_result, solver.WallTime()


if __name__ == '__main__':
    _dict_constant, _data_path = parse_config()
    logger = init_log()
    paths = glob.glob(_data_path)
    print(paths)
    # paths.reverse()
    for path in paths:
        logger.info("input path %s: ", path)
        _inp, _is_adj_matrix, _distance_matrix = prepare(path)
        result, connect_matrix, t = solve_by_or_tools(_inp, _is_adj_matrix,
                                                      _distance_matrix,
                                                      _dict_constant)
        # logger.info("Connected Matrix: \n%s", connect_matrix)
        logger.info("Result: %s", result)
        logger.info("Time: %s", t)
Exemple #3
0
Fichier : GA.py Projet : ManhPP/WSN
    size = min(len(ind1), len(ind2))
    rate = random.random()
    if rate < rate_threshold:
        for i in range(num_positions):
            if random.random() < indpb:
                ind1[i], ind2[i] = ind2[i], ind1[i]
    else:
        for i in range(num_positions, size):
            if random.random() < indpb:
                ind1[i], ind2[i] = ind2[i], ind1[i]

    return ind1, ind2


if __name__ == '__main__':
    params, _data_path = parse_config()
    logger = init_log()
    init_rate = [(1, 0), (0, 1), (0, 0)]
    for rate_mst, rate_spt in init_rate:
        params["rate_mst"] = rate_mst
        params["rate_spt"] = rate_spt
        logger.info("info param: %s" % params)

        for path in glob.glob(_data_path):
            t = time.time()
            logger.info("input path: %s" % path)
            inp = WsnInput.from_file(path)
            try:
                run_ga(inp, params, logger)
            except Exception as e:
                logger.info("Error: %s" % e)
Exemple #4
0
    result *= params['l']

    for i in vertices:
        if i.hop > max_hop:
            print(i.hop)
            return float('inf')
        # result += 9999 * max(i.hop - max_hop, 0)

    return result


def get_hop(g: Graph, vertex: Vertex):
    if vertex not in g.vertices:
        raise Exception("Error vertices not in graph: ", vertex)
    graph = g.graph

    def cal(v):
        sys.setrecursionlimit(1500)
        if v == g.vertices[0]:
            return 0
        else:
            for i in g.vertices:
                if v in graph[i]:
                    return 1 + cal(i)

    return cal(vertex)


if __name__ == '__main__':
    print(parse_config())