Example #1
0
    def calculate_efficiency(gene_nodes_pos, gene_edges_thickness, gene_adj_element, np_save_path=False):
        condition_nodes_pos, input_nodes, input_vectors, output_nodes, \
            output_vectors, frozen_nodes, condition_edges_indices, condition_edges_thickness\
            = make_main_node_edge_info(*condition(), condition_edge_thickness=0.2)

        # make edge_indices
        edges_indices = make_adj_triu_matrix(gene_adj_element, node_num, condition_edges_indices)

        # make nodes_pos
        nodes_pos = np.concatenate([condition_nodes_pos, gene_nodes_pos])

        # 条件ノードが含まれている部分グラフを抽出
        G = nx.Graph()
        G.add_nodes_from(np.arange(len(nodes_pos)))
        G.add_edges_from(edges_indices)
        condition_node_list = input_nodes + output_nodes + frozen_nodes

        trigger = 0  # 条件ノードが全て接続するグラフが存在するとき,トリガーを発動する
        for c in nx.connected_components(G):
            sg = G.subgraph(c)  # 部分グラフ
            if set(condition_node_list) <= set(sg.nodes):  # 条件ノードが全て含まれているか
                edges_indices = np.array(sg.edges)
                trigger = 1
                break
        if trigger == 0:  # ペナルティを発動する
            return -10.0

        # make edges_thickness
        edges_thickness = make_edge_thick_triu_matrix(gene_edges_thickness, node_num, condition_edges_indices, condition_edges_thickness, edges_indices)

        env = BarFemGym(nodes_pos, input_nodes, input_vectors,
                        output_nodes, output_vectors, frozen_nodes,
                        edges_indices, edges_thickness, frozen_nodes)
        env.reset()
        efficiency = env.calculate_simulation()
        if np_save_path:
            env.render(save_path=os.path.join(np_save_path, "image.png"))
            np.save(os.path.join(np_save_path, "nodes_pos.npy"), nodes_pos)
            np.save(os.path.join(np_save_path, "edges_indices.npy"), edges_indices)
            np.save(os.path.join(np_save_path, "edges_thickness.npy"), edges_thickness)

        return float(efficiency)
        save_graph_info(os.path.join(log_dir, 'graph_info'),
                        origin_nodes_positions, barfem_input_nodes,
                        origin_input_vectors, barfem_output_nodes,
                        origin_output_vectors, origin_frozen_nodes,
                        current_edges_indices, current_edges_thickness)
        # env.render(os.path.join(
        #     log_dir, 'render_image/{}.png'.format(epoch+1)))

    if gif:
        env = BarFemGym(origin_nodes_positions, barfem_input_nodes,
                        origin_input_vectors, barfem_output_nodes,
                        origin_output_vectors, origin_frozen_nodes,
                        current_edges_indices, current_edges_thickness,
                        origin_frozen_nodes)
        env.reset()
        env.render(os.path.join(log_dir, 'render_image/{}.png'.format(epoch)))

    history['epoch'].append(epoch + 1)
    history['result_efficiency'].append(current_efficiency)
    # 学習履歴を保存
    with open(os.path.join(log_dir, 'history.pkl'), 'wb') as f:
        pickle.dump(history, f)
    with open(os.path.join(log_dir, "progress.txt"), mode='a') as f:
        f.writelines('epoch %d,  result_efficiency: %.5f\n' %
                     (epoch + 1, current_efficiency))
    with open(os.path.join(log_dir, "represent_value.txt"), mode='w') as f:
        f.writelines('epoch %d,  best_efficiency: %.5f\n' %
                     (best_epoch + 1, best_efficiency))
    plot_efficiency_history(history,
                            os.path.join(log_dir, 'learning_effi_curve.png'))
error_num = 0

tmax = 10000  # 共益勾配法の最大試行回数
eps = 1e-8  # 共益勾配法の許容閾値

for i in tqdm(range(1000)):
    nodes_pos, edges_indices, edges_thickness,\
        input_nodes, input_vectors, frozen_nodes = \
        make_random_fem_condition_with_ER(max_node_num, max_edge_pos)
    output_nodes = [0]
    output_vectors = np.array([[1, 1]])
    result = compare_apdl_barfem(nodes_pos, edges_indices, edges_thickness,
                                 input_nodes, input_vectors, frozen_nodes, tmax, eps)

    if not result:
        error_num += 1
        log_dir = "check_fem_by_ansys/conditions/error{}".format(error_num)
        os.makedirs(log_dir, exist_ok=True)
        np.save(os.path.join(log_dir, 'nodes_pos.npy'), nodes_pos)
        np.save(os.path.join(log_dir, 'edges_indices.npy'), edges_indices)
        np.save(os.path.join(log_dir, 'edges_thickness.npy'), edges_thickness)
        np.save(os.path.join(log_dir, 'input_nodes.npy'), input_nodes)
        np.save(os.path.join(log_dir, 'input_vectors.npy'), input_vectors)
        np.save(os.path.join(log_dir, 'frozen_nodes.npy'), frozen_nodes)
        env = BarFemGym(nodes_pos, input_nodes, input_vectors,
                        output_nodes, output_vectors, frozen_nodes,
                        edges_indices, edges_thickness, frozen_nodes)
        env.reset()
        env.render(save_path=os.path.join(log_dir, "image.png"), display_number=True)
Example #4
0
                frozen_nodes)
env.reset()
efficiency = env.calculate_simulation()
#env.render(save_path="image/image_preprocess.png", display_number=True)

# 同じノード位置にあるものを排除する.
processed_nodes_pos, processed_edges_indices, processed_edges_thickness = preprocess_graph_info(
    nodes_pos, edges_indices, edges_thickness)
# 傾きが一致するものをグループ分けし,エッジ分割を行う.
processed_edges_indices, processed_edges_thickness = separate_same_line_procedure(
    processed_nodes_pos, processed_edges_indices, processed_edges_thickness)

processed_nodes_pos, processed_edges_indices, processed_edges_thickness =\
    seperate_cross_line_procedure(processed_nodes_pos, processed_edges_indices, processed_edges_thickness)

# 同じノード,[1,1]などのエッジの排除,エッジのソートなどを行う
processed_nodes_pos, processed_edges_indices, processed_edges_thickness = \
    preprocess_graph_info(processed_nodes_pos, processed_edges_indices, processed_edges_thickness)

input_nodes, output_nodes, frozen_nodes, processed_edges_thickness \
    = conprocess_seperate_edge_indice_procedure(input_nodes, output_nodes, frozen_nodes, condition_nodes_pos, condition_edges_indices, condition_edges_thickness,
                                                processed_nodes_pos, processed_edges_indices, processed_edges_thickness)

env = BarFemGym(processed_nodes_pos, input_nodes, input_vectors, output_nodes,
                output_vectors, frozen_nodes, processed_edges_indices,
                processed_edges_thickness, frozen_nodes)
env.reset()
efficiency = env.calculate_simulation()
env.render(save_path="image/image_proprocess.png", display_number=True)
print(efficiency)