-
Notifications
You must be signed in to change notification settings - Fork 0
/
Strategy.py
164 lines (121 loc) · 6 KB
/
Strategy.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
"""
Strategy.py
--------------
This file includes the _points_distribution each student has.
Strategy is defined by a function, then defined to distribution of
all the points student can distribute over the _courses.
Written by: Linoy Palas (July 8th)
"""
import numpy as np
import random as rand
import enum
# --------
# IMPORTANT : If you want to add another strategy - you should add it both to the
# enum class and to the factory.
# --------
class Strategy(enum.Enum):
# Uniform distribution over the courses.
Const = 0
# Using the func f(x) = x^2.
SquaredPow = 1
# Using the func f(x) = e^x.
Exp = 2
# Using the func f(x) = x.
Linear = 3
# Randomize points distribution.
Random = 4
def strategy_factory(strategy, num_of_courses, points_to_share):
"""
This func is a factory of the strategies.
:param strategy: a _points_distribution type (enum).
:param num_of_courses: the number of _courses.
:param points_to_share: the number of points to distribute between _courses.
:return: array where in the i'th coord there is the number of points for the
i'th course in priority.
"""
if strategy == Strategy.Const:
return constant(num_of_courses, points_to_share)
elif strategy == Strategy.SquaredPow:
return power(num_of_courses, points_to_share)
elif strategy == Strategy.Exp:
return exp(num_of_courses, points_to_share)
elif strategy == Strategy.Linear:
return linear(num_of_courses, points_to_share)
elif strategy == Strategy.Random:
return random_selection(num_of_courses, points_to_share)
else:
raise Exception("No such type!")
def linear(num_of_courses, points_to_share):
vector_of_points_to_return = [0] * (num_of_courses + 1)
vector_of_points = [0] * (num_of_courses + 1) # this list holds the score based on position
for i in range(1, len(vector_of_points_to_return)): # lst is the list
vector_of_points[i] = i
sum_of_score = sum(vector_of_points) # this will be the sum of all the scores will used for
for i in range(1, len(vector_of_points_to_return)):
# Normalization of the vector
vector_of_points_to_return[i] = vector_of_points[i] * points_to_share / sum_of_score
# to have it in descending order:
vector_of_points_to_return.reverse()
return vector_of_points_to_return
def exp(num_of_courses, points_to_share):
vector_of_points_to_return = [0] * (num_of_courses + 1)
vector_of_points = [0] * (num_of_courses + 1) # this list holds the score based on position
for i in range(1, len(vector_of_points_to_return)): # lst is the list
vector_of_points[i] = np.math.exp(i)
sum_of_score = sum(vector_of_points) # this will be the sum of all the scores will used for
for i in range(1, len(vector_of_points_to_return)):
vector_of_points_to_return[i] = round(vector_of_points[i] * points_to_share / sum_of_score, 3)
# to have it in descending order:
vector_of_points_to_return.reverse()
return vector_of_points_to_return
def power(num_of_courses, points_to_share, power_by=2):
vector_of_points_to_return = [0] * (num_of_courses + 1)
vector_of_points = [0] * (num_of_courses + 1) # this list holds the score based on position
for i in range(1, len(vector_of_points_to_return)): # lst is the list
vector_of_points[i] = i ** power_by
sum_of_score = sum(vector_of_points) # this will be the sum of all the scores will used for
for i in range(1, len(vector_of_points_to_return)):
vector_of_points_to_return[i] = round(vector_of_points[i] * points_to_share / sum_of_score, 3)
# to have it in descending order:
vector_of_points_to_return.reverse()
return vector_of_points_to_return
def constant(num_of_courses, points_to_share):
vector_of_points_to_return = [0] * (num_of_courses + 1)
vector_of_points = [0] * (num_of_courses + 1) # this list holds the score based on position
for i in range(1, len(vector_of_points_to_return)): # lst is the list
vector_of_points[i] = 1
sum_of_score = sum(vector_of_points) # this will be the sum of all the scores will used for
for i in range(1, len(vector_of_points_to_return)):
vector_of_points_to_return[i] = round(vector_of_points[i] * points_to_share / sum_of_score, 3)
# to have it in descending order:
vector_of_points_to_return.reverse()
return vector_of_points_to_return
def random_selection(num_of_courses, points_to_share):
vector_of_points_to_return = [0] * (num_of_courses + 1)
vector_of_points = [0] * (num_of_courses + 1) # this list holds the score based on position
for i in range(1, len(vector_of_points_to_return)): # lst is the list
vector_of_points[i] = rand.uniform(0, 1)
sum_of_score = sum(vector_of_points) # this will be the sum of all the scores will used for
for i in range(1, len(vector_of_points_to_return)):
vector_of_points_to_return[i] = round(vector_of_points[i] * points_to_share / sum_of_score, 3)
vector_of_points_to_return.sort(reverse=True)
return vector_of_points_to_return
# for testing :
# if __name__ == '__main__':
# import matplotlib.pyplot as plt
# # Create an object with a number of desirable _courses and points.
# # In this case for example- 5 _courses and 100 points divided (change it if you want as your wish)
# _points_distribution = Strategies(5, 100)
# # a vector that returned from the function according to the desired point distribution (exp, const, lin,quadratic):
# vector_score_by_function = _points_distribution.linear()
#
# # a list that simply contains numbers from 1 to the number of _courses so that we can see in the graph the ratio:
# vector_of_courses = list(range(1, _points_distribution._courses+1))
#
# # plot the graph
# print(vector_of_courses)
# print(vector_score_by_function)
# plt.plot(vector_of_courses, vector_score_by_function[:_points_distribution._courses])
# plt.show()
# r = random_selection(8, 100)
# print(r)