-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgreedyalgo.py
63 lines (49 loc) · 2.04 KB
/
greedyalgo.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
import numpy as np
from itertools import product
class GreedyAlgorithm:
def __init__(self, data, func):
self.data = data
self.bounds = []
self.objective_function = func
self.constraints = []
def add_constraint(self, constraint):
self.constraints.append(constraint)
def add_bound(self, _min, _max):
self.bounds.append((_min, _max))
def set_bounds(self, bounds):
self.bounds = bounds
def generate_random_solution_space(self):
self.data = list(product(*(range(low, high+1) for low, high in self.bounds))) #[[i] for i in range(bounds[j][0], bounds[j][1]) for j in range(0, n)]
return self.data
def search_optimum(self):
valid_solutions = []
for solution in self.data:
is_valid = True
for constraint in self.constraints:
is_valid = is_valid and constraint(solution)
if is_valid:
valid_solutions.append(solution)
# Find the best solution
best_solution = min(valid_solutions, key=self.objective_function)
return best_solution
# Define the objective function and constraints
# def objective_function(params):
# return -params[0]**2 + params[1]**2 + params[2]**2
# def constraint_1(params):
# return sum(params) >= 0
# def constraint_2(params):
# return params[0] - params[1] >= 0
# def constraint_3(params):
# return all(element <= 10 for element in params)
# Generate all possible solutions
# (min0, max0) = (0, 10)
# (min1, max1) = (0, 9)
# (min2, max2) = (0, 7)
# bounds = [(min0, max0), (min1, max1), (min2, max2)] # Define the ranges for each element
# greedyAlgo = GreedyAlgorithm(None, objective_function)
# greedyAlgo.set_bounds(bounds)
# greedyAlgo.add_constraint(constraint_1)
# greedyAlgo.add_constraint(constraint_2)
# greedyAlgo.add_constraint(constraint_3)
# greedyAlgo.generate_random_solution_space()
# print(greedyAlgo.search_optimum())