-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolve For Given Conditions.py
129 lines (111 loc) · 4.17 KB
/
Solve For Given Conditions.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import itertools
import re
import numpy as np
from sympy import primefactors
from time import sleep
import time
SEARCH_MODE = "max"
VARIABLES_FILE = "Variables.txt"
CONDITIONS_FILE = "Conditions.txt"
RESULT_FILE = "Result.txt"
class Solver():
def __init__(self):
self.mode = "max" #Default search mode
self.VarNames = list()
self.Combinations = None
self.ResultExpression = str()
self.Conditions = list()
self.Solutions = dict()
self.SolutionsSorted = list()
self.SolutionsOutput = str()
def __str__(self):
return self.SolutionsOutput
def load(self):
self.import_variables()
self.import_conditions()
def solve(self):
solutions = dict()
re = self.ResultExpression #Optimization reasons
check = self.check_conditions #Optimization reasons
for comb in self.Combinations:
try:
res = eval(re.format(*comb))
if check(comb):
solutions[comb] = res
except ZeroDivisionError:
continue
self.Solutions = solutions
def output(self):
self.apply_mode()
self.SolutionsOutput = str()
for solution in self.SolutionsSorted:
template = ""
VarValues = solution[0]
result = solution[1]
for _val,_name in zip(VarValues,self.VarNames): #Iterate over variable values and names
template += _name + "=" + str(_val) + " "
template += " Sonuç=" + str(result) + "\n"
self.SolutionsOutput += template
self.SolutionsOutput += f"\n{len(self.Solutions)} solutions were found."
def display(self):
print(self)
def save(self):
with open(RESULT_FILE,"w") as f:
f.write(self.SolutionsOutput)
def set_mode(self,mode):
self.mode = str(mode).lower()
def apply_mode(self):
if self.mode.lower() == "max":
self._mode_max()
if self.mode.lower() == "min":
self._mode_min()
if self.mode.lower() == "count":
self._mode_count()
def _mode_max(self):
self.SolutionsSorted = sorted(self.Solutions.items(), key=lambda x:x[1],reverse=False)
def _mode_min(self):
self.SolutionsSorted = sorted(self.Solutions.items(), key=lambda x:x[1],reverse=True)
def import_variables(self):
var_ranges = list()
var_names = list()
with open(VARIABLES_FILE) as vf:
lines = vf.readlines()
for line in lines:
_name,_range = line.split("|")[1:3]
var_names.append(_name)
_min,_max = _range.split(",")
var_ranges.append(range(int(_min),int(_max)+1))
self.VarNames = var_names
self.Combinations = itertools.product(*var_ranges)
def import_conditions(self):
conditions = list()
final_expression = str()
with open(CONDITIONS_FILE) as cf:
global SEARCH_MODE
lines = cf.readlines()
final_expression = self.convert_to_formattable_string(lines[0].split("|")[1],self.VarNames)
SEARCH_MODE = lines[0].split("|")[2].lower()
for line in lines[1:]:
conditions.append(self.convert_to_formattable_string(line.split("|")[1],self.VarNames))
self.ResultExpression = final_expression
self.Conditions = conditions
def convert_to_formattable_string(self,expression,var_names):
formatted_string = expression
for i,name in enumerate(var_names):
formatted_string = re.sub(name,"{"+str(i)+"}",formatted_string)
return formatted_string
def check_conditions(self,comb):
for c in self.Conditions:
if not eval(c.format(*comb)):
return False
return True
now = time.time()
S = Solver()
S.load()
S.solve()
S.set_mode(SEARCH_MODE)
S.output()
S.display()
S.save()
print((time.time()-now)*1000," ms")
input("\nProgram başarıyla sonuçlandı. İşlemi kapatmak için Enter'a basın.")