-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
93 lines (69 loc) · 3.3 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# This file is the main file of the project.
# In this file all the functions of this project are called and executed.
# Executing this file will run the whole project.
#################################################
# Import the necessary functions and packages #
#################################################
from import_data import import_data
from model_optimize import model_optimize
from output_function import output_function
from f_c import f_c
from check_unique_ids import check_unique_ids
import numpy as np
import configparser
import os
# Get the config file
config = configparser.ConfigParser()
config.read("settings.ini")
#################################################
# Define file paths #
#################################################
### Define path of the import files
input_file = config["InputFiles"]["input_file_A"] # Data of the model A
input_file_prime = config["InputFiles"]["input_file_B"] # Data of the model B
#################################################
# Import the data #
#################################################
components, relations = import_data(input_file) # Components and relations of model A
components_prime, relations_prime = import_data(input_file_prime) # Components and relations of model B
#################################################
# Check for non unique IDs #
#################################################
components_unique = check_unique_ids(components)
relations_unique = check_unique_ids(relations)
components_prime_unique = check_unique_ids(components_prime)
relations_prime_unique = check_unique_ids(relations_prime)
##############################################################
# Set the parameters and functions of the optimization model #
##############################################################
f_c_matrix = f_c(components, components_prime) # Similarity function of the components
g_r = np.ones((len(relations), len(relations_prime))) # Similarity function of the relations
#################################################
# Optimize the model #
#################################################
sol_x, sol_z, objective_value, infeasible = model_optimize(components, relations, components_prime, relations_prime, f_c_matrix, g_r)
# account for numerical errors
if infeasible == False:
for i in range(len(sol_x)):
for j in range(len(sol_x[0])):
if sol_x[i][j] > 0.5:
sol_x[i][j] = 1
else:
sol_x[i][j] = 0
for i in range(len(sol_z)):
for j in range(len(sol_z[0])):
if sol_z[i][j] > 0.5:
sol_z[i][j] = 1
else:
sol_z[i][j] = 0
#################################################
# Output the data #
#################################################
# Outputfile path
# Check if the output folder exists and create it if it doesn't
path_exists = os.path.exists("output")
if not path_exists:
os.makedirs("output")
output_file_json = f"output/output.json"
# Output the data
output_function(components, components_prime, sol_x, input_file, input_file_prime, output_file_json, components_unique, relations_unique, components_prime_unique, relations_prime_unique, infeasible)