Skip to content

Commit 61892e1

Browse files
committed
LRPS Algorithm v.1.0.1
The word elements was changed in all places to objects, as it is clearer and more descriptive; in addition, it is in accordance with the -in process- article. Also, the color on the plot lines was changed to purple instead of blue :3
1 parent 4a7301b commit 61892e1

File tree

3 files changed

+33
-33
lines changed

3 files changed

+33
-33
lines changed

Algorithm/experiments.py

+17-17
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,41 @@
55
#* repeating the selection experiment a lot of times:
66
times = int(input("\nRepetitions: "))
77

8-
#? Example list of 10 elements;
9-
#* The algorithm has to recieve this list and select randomly one element;
8+
#? Example list of 10 objects;
9+
#* The algorithm has to recieve this list and select randomly one object;
1010
#* By repeating the selection a lot of times, the result has to be that the
11-
#* first element "E1" is the most frecuent selected, and then "E2", etc.
11+
#* first object "Obj1" is the most frecuent selected, and then "Obj2", etc.
1212
#! IF YOU WANT TO MAKE SOME EXPERIMENTS WITH AN OWN IDEA OF SELECTION LIST
13-
#! YOU JUST HAVE TO PUT INTO THE LIST "elements" WHATEVER YOU WANT!
14-
elements: list = ["E1", "E2", "E3", "E4", "E5", "E6", "E7", "E8", "E9", "E10"]
13+
#! YOU JUST HAVE TO PUT INTO THE LIST "objects" WHATEVER YOU WANT!
14+
objects: list = ["Obj1", "Obj2", "Obj3", "Obj4", "Obj5", "Obj6", "Obj7", "Obj8", "Obj9", "Obj10"]
1515
counter = []
16-
n = len(elements)
16+
n = len(objects)
1717

1818
#? Fills a counter vector with 0's; it will contains the amount of times that
19-
#? each element of the list was selected:
19+
#? each object of the list was selected:
2020
for i in range(0, n):
2121
counter.append(0)
2222

2323
#/ Does all the repetitions of the experiment using the logrps function to select
24-
#/ something on the list; and counts the times that each element was selected.
24+
#/ something on the list; and counts the times that each object was selected:
2525
for _ in range(0, times):
26-
selected = logrps(elements)
27-
index = elements.index(selected)
26+
selected = logrps(objects)
27+
index = objects.index(selected)
2828
counter[index] += 1
2929

3030
#? Prints the data from the counter:
31-
print(f"Elements on list: {n}")
31+
print(f"Objects on list: {n}")
3232
print("\nSelected times:")
3333
for i in range(0, n):
34-
print(f"Element #{i+1} [{elements[i]}]: {counter[i]} times selected;")
34+
print(f"Object #{i+1} [{objects[i]}]: {counter[i]} times selected;")
3535

3636

3737
# Draws the line in a graph:
3838
plt.rcParams["figure.figsize"] = [10, 5]
3939
plt.rcParams["figure.autolayout"] = True
4040

4141
# Points with:
42-
# (element, amountOfSelections ):
42+
# (object, amountOfSelections ):
4343
# points: list = []
4444
x_values: list = []
4545
y_values: list = []
@@ -53,16 +53,16 @@
5353
# Shows the graphic:
5454
plt.title("LOGARITHMIC RANDOM PONDERATED SELECTOR")
5555
plt.ylabel("Amount of selected times")
56-
plt.xlabel("Number of element")
57-
info = f"For {n} elements;\nWith {times} choices;"
56+
plt.xlabel("Number of object")
57+
info = f"For {n} objects;\nWith {times} choices;"
5858
plt.annotate(info,
5959
xy=(0.96, 0.8), xytext=(0, 12),
6060
xycoords=("axes fraction", "figure fraction"),
6161
textcoords="offset points",
6262
size=10, ha="right", va="top",
63-
bbox=dict(boxstyle="square,pad=1.0", fc="white", ec="b", lw=2))
63+
bbox=dict(boxstyle="square,pad=1.0", fc="white", ec="#8B0880", lw=2))
6464

65-
plt.plot(x_values, y_values, "bo", linestyle="-")
65+
plt.plot(x_values, y_values, "o", c="#8B0880", mfc="#8B0880", linestyle="-")
6666
plt.savefig("logselection.png", dpi="figure")
6767
plt.show()
6868
print()

Algorithm/logrps.py

+16-16
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,39 @@
55
import random
66
import math
77

8-
def logrps(elements: list):
8+
def logrps(objects: list):
99

1010
'''
1111
Logarithmic Random Poderated Selector:
1212
13-
This algorithm gets a list of elements, and using
13+
This algorithm gets a list of objects, and using
1414
a logarithmic scale, selects randomly one member
1515
of the list giving a higher probability weight to
16-
the initial elements of the list.
16+
the initial objects of the list.
1717
1818
This algorithm selects with more frecuency the
1919
initial members of the list, based on the
2020
logarithmic/exponential curve form.
2121
'''
2222

23-
#* Amount of elements: "n":
24-
n = len(elements)
23+
#* Amount of objects: "n":
24+
n = len(objects)
2525

2626
#* Validation cases:
2727
#? When list is empty:
2828
if n == 0:
2929
return "List cannot be empty :c"
30-
#? When list has only one element:
30+
#? When list has only one object:
3131
if n == 1:
32-
return elements[0]
32+
return objects[0]
3333

34-
#* Throws a random float number based on the n elements:
34+
#* Throws a random float number based on the n objects:
3535
#? To operate the numbers, it will consider 1 as the
36-
#? first element; then when it selects an index of the
36+
#? first object; then when it selects an index of the
3737
#? list, it will take at 0-starting notation:
3838
#/ Random float between 1 and n with 4 decimals.
3939
#* This random represent a random point of selection
40-
#* in a linear axis in which all elements has the same
40+
#* in a linear axis in which all objects has the same
4141
#* space to be selected:
4242
randomLinearPoint = round(random.uniform(0, n), 4)
4343
# print(f"\nRandom Lineal Point: {randomLinearPoint}; ", end="")
@@ -46,19 +46,19 @@ def logrps(elements: list):
4646
This is the propuse:
4747
4848
* Having a linear scale between 1 - n, all the spaces
49-
between elements has the same size.
49+
between objects has the same size.
5050
5151
* Having a logarithmic scale between 1 - n, the spaces
52-
between elements are different; the first element has
52+
between objects are different; the first object has
5353
the biggest space, the second is smaller, etc. The
5454
differences between the spaces is based on logarithms.
5555
5656
* When the random is selected, you get the linear scale
57-
element; then it has to ponderate the space with the
57+
object; then it has to ponderate the space with the
5858
logarithmic scale, to select the equivalent.
5959
6060
Example:
61-
NOTE: E1, E2, ..., E6 are the 6 elements of the list.
61+
NOTE: E1, E2, ..., E6 are the 6 objects of the list.
6262
6363
LINEAR SCALE:
6464
E1 E2 E3 E4 E5 E6
@@ -89,6 +89,6 @@ def logrps(elements: list):
8989
for i in range(2, (n+1)+1):
9090
# print((n+1) * math.log(i, n+1), end=" ")
9191
if randomLinearPoint <= (n * math.log(i, n+1)):
92-
#! Returns the selected element:
92+
#! Returns the selected object:
9393
# print(f"Logarithmic point: {i-1};")
94-
return elements[(i-1)-1]
94+
return objects[(i-1)-1]

Algorithm/logselection.png

546 Bytes
Loading

0 commit comments

Comments
 (0)