Skip to content

Commit 53a0b56

Browse files
author
Alchemist dev
committed
first commit
0 parents  commit 53a0b56

File tree

7 files changed

+569
-0
lines changed

7 files changed

+569
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/1.jpg
2+
/backup12.14.2020.php
3+
/backup12.15.2020.php
4+
/backup12.16.2020.py
5+
/RSD Algorithm.zip

PHP-input-&-output-sample.JPG

51.5 KB
Loading

Persian readme .md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
سلام، اين پروژه الگوريتم RSD را محاسبه میکند.
2+
این پروژه در دو نسخه که شامل نسخه ی php و نسخه ی python میباشد کدنویسی شده است.
3+
شما میتوانید در بالای صفحه ی کد، متغیر عوامل الگوریتم را که شامل ایندکس هر عامل به عنوان کلید، و ترتیب ترجیات این عامل در قالب یک آرایه به عنوان
4+
مقدار این کلید می باشد، تعیین کنید (به عنوان نمونه در ابتدای کد، تعدادی از این متغیر تعریف و کامنت شده است).
5+
توجه: تعداد عامل ها باید بیشتر از 1 باشد و همچنین تعداد ترجیحات عوامل (به عنوان کالاهایی غیر تکراری برای هر کاربر) باید برابر با تعداد عوامل باشد.
6+
تفسیر خروجی الگوریتم به صورت زیر می باشد:
7+
در زبان پایتون یک دیکشنری (یا آرایه در PHP) که شامل : ایندکس عامل ها(کلید) => احتمال تخصیص کالا(یا خدمات) برای کاربر(مقدار).
8+
پیشنهاد میشود که برای مشاهده و تعریف راحت تر متغیر عامل ها از Visual Stutio Code IDE و جهت مشاهده ی خروجی الگوریتم از يكي از
9+
اکستنشن هاي همين IDE به اسم ((Code Runner)) استفاده کنید.
10+
این پروژه توسط محمد امین مشایخان ایجاد و توسعه داده شده است و کد های این پروژه با راهنمایی های استادیار جناب آقای مهدی فیضی،
11+
stackoverflowl.com , php2python و ... نوشده شده است.
12+
از توجه شما متشکرم.

Python-input-&-output-sample.jpg

76.7 KB
Loading

RSD_Algorithm .py

Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
from pprint import pprint
2+
# ---------------------------------------------------------------------------- #
3+
# samples #
4+
# ---------------------------------------------------------------------------- #
5+
6+
7+
# agents = {
8+
# 0 : [6, 3, 2, 5, 1, 7, 4, 0],
9+
# 1 : [6, 3, 1, 2, 5, 7, 0, 4],
10+
# 2 : [6, 0, 2, 1, 0, 7, 5, 4],
11+
# 3 : [6, 0, 1, 3, 2, 7, 4, 5],
12+
# 4 : [6, 5, 2, 0, 4, 7, 3, 1],
13+
# 5 : [6, 5, 2, 1, 4, 7, 3, 0],
14+
# 6 : [6, 5, 2, 1, 4, 7, 3, 0],
15+
# 7 : [6, 5, 2, 1, 4, 7, 3, 0],
16+
# }
17+
18+
# agents = {
19+
# 0 : [6, 3, 2, 5, 1, 4, 0],
20+
# 1 : [6, 3, 1, 2, 5, 0, 4],
21+
# 2 : [6, 0, 2, 1, 0, 5, 4],
22+
# 3 : [6, 0, 1, 3, 2, 4, 5],
23+
# 4 : [6, 5, 2, 0, 4, 3, 1],
24+
# 5 : [6, 5, 2, 1, 4, 3, 0],
25+
# 6 : [6, 5, 2, 1, 4, 3, 0],
26+
#}
27+
28+
# agents = {
29+
# 0 : [3, 2, 5, 1, 4, 0],
30+
# 1 : [3, 1, 2, 5, 0, 4],
31+
# 2 : [0, 2, 1, 0, 5, 4],
32+
# 3 : [0, 1, 3, 2, 4, 5],
33+
# 4 : [5, 2, 0, 4, 3, 1],
34+
# 5 : [5, 2, 1, 4, 3, 0],
35+
# }
36+
37+
# agents = {
38+
# 0 : [3, 2, 1, 4, 0],
39+
# 1 : [3, 1, 2, 0, 4],
40+
# 2 : [0, 2, 1, 0, 4],
41+
# 3 : [0, 1, 3, 2, 4],
42+
# 4 : [2, 0, 4, 3, 1],
43+
# }
44+
45+
agents = {
46+
0: [0, 1, 2, 3],
47+
1: [0, 1, 2, 3],
48+
2: [2, 0, 1, 3],
49+
3: [2, 3, 0, 1],
50+
}
51+
52+
# agents = {
53+
# 0 : [0, 1, 2],
54+
# 1 : [0, 1, 2],
55+
# 2 : [1, 2, 0],
56+
# }
57+
58+
# agents = {
59+
# 0 : [1, 0],
60+
# 1 : [1, 0],
61+
#}
62+
63+
# --------------------------------------------------------------------------- #
64+
65+
66+
# ------------------------------ #
67+
# for more pretty printing: #
68+
# ------------------------------ #
69+
def pretty(d, indent=0):
70+
for key, value in d.items():
71+
print('\t' * indent + str(key))
72+
if isinstance(value, dict):
73+
pretty(value, indent+1)
74+
else:
75+
print('\t' * (indent+1) + str(value))
76+
# ---------------------------------------------------------------------------- #
77+
78+
79+
def set_order_of_agents_items(agents_param, sorted_agents_param, current_order_param):
80+
global growth_RSD_items, last_RSD_item_number
81+
for agent_key in agents_param:
82+
order = current_order_param
83+
# ------------------------------------------------------- #
84+
# for each new allocating item, clear last sorted agent: #
85+
# ------------------------------------------------------- #
86+
sorted_agents_param[order] = {}
87+
if len(agents_param) > 1:
88+
sorted_agents_param[order][agent_key] = agents_param[agent_key]
89+
# --------------------------------------------------------- #
90+
# clear current sorted agent for next sorting step(s): #
91+
# --------------------------------------------------------- #
92+
result_of_current_step = agents_param.copy()
93+
del result_of_current_step[agent_key]
94+
next_order = order + 1
95+
set_order_of_agents_items(result_of_current_step, sorted_agents_param, next_order)
96+
# --------------------------------------------------------- #
97+
# sorting part for last agent in current sorting item: #
98+
# --------------------------------------------------------- #
99+
elif len(agents_param) == 1:
100+
# --------------------------------------------- #
101+
# sort last agent in current sorting item: #
102+
# --------------------------------------------- #
103+
sorted_agents_param[order][agent_key] = agents_param[agent_key].copy()
104+
# --------------------------------- #
105+
# store current sorting item : #
106+
# --------------------------------- #
107+
growth_RSD_items[last_RSD_item_number] = sorted_agents_param.copy()
108+
# ----------------------------------------------------- #
109+
# prepare the key for store the next sorting item: #
110+
# ----------------------------------------------------- #
111+
last_RSD_item_number += 1
112+
# ---------------------------------------------------------------------------- #
113+
114+
115+
def set_various_allocates(growth_RSD_items_param, goods_list_param):
116+
global random_allocated_matrices, number_of_growth_RSD_items
117+
# ------------------------------------------------------ #
118+
# here, number of allocated matrices begins from 0: #
119+
# ------------------------------------------------------ #
120+
index_of_allocated_matrix = 0
121+
for index_of_item in growth_RSD_items_param:
122+
random_allocated_matrices[index_of_allocated_matrix] = {}
123+
goods_list_for_each_steps = goods_list_param.copy()
124+
for order in growth_RSD_items_param[index_of_item]:
125+
agent = growth_RSD_items_param[index_of_item][order]
126+
# -------------------------------------------- #
127+
# getting key(agent index) of each agent: #
128+
# -------------------------------------------- #
129+
agent_key = list(agent.keys())[0]
130+
random_allocated_matrices[index_of_allocated_matrix][agent_key] = {}
131+
# ----------------------------------------------- #
132+
# here, agents tendency index begins from 0: #
133+
# ----------------------------------------------- #
134+
best_remained_good_index_for_agent = 0
135+
if len(goods_list_for_each_steps) > 1:
136+
if agent[agent_key][best_remained_good_index_for_agent] in goods_list_for_each_steps:
137+
# ----------------------------------------------------------- #
138+
# set the definitive allocating matrix for current agent #
139+
# ----------------------------------------------------------- #
140+
for i in range(len(goods_list_param)):
141+
if i == agent[agent_key][best_remained_good_index_for_agent]:
142+
# ------------------------------------------------------- #
143+
# if current good(index) allocated to current agent: #
144+
# ------------------------------------------------------- #
145+
random_allocated_matrices[index_of_allocated_matrix][agent_key][i] = 1
146+
del goods_list_for_each_steps[i]
147+
else:
148+
random_allocated_matrices[index_of_allocated_matrix][agent_key][i] = 0
149+
else:
150+
# -------------------------------------------------------------------------------------------------------------------------- #
151+
# if first best good for current agent does not available, now best_remained_good_index_for_agent+1, becomes equal to 1: #
152+
# -------------------------------------------------------------------------------------------------------------------------- #
153+
for i in range(1, len(goods_list_param), 1):
154+
if (agent[agent_key][i] in goods_list_for_each_steps):
155+
# ----------------------------------------------------------- #
156+
# set the definitive allocating matrix for current agent #
157+
# ----------------------------------------------------------- #
158+
for j in range(len(goods_list_param)):
159+
if j == agent[agent_key][i]:
160+
# ------------------------------------------------------- #
161+
# if current good(index) allocated to current agent: #
162+
# ------------------------------------------------------- #
163+
random_allocated_matrices[index_of_allocated_matrix][agent_key][j] = 1
164+
del goods_list_for_each_steps[j]
165+
else:
166+
random_allocated_matrices[index_of_allocated_matrix][agent_key][j] = 0
167+
break
168+
169+
# --------------------------------------------------------------------------------------------------- #
170+
# set last definitive allocating matrix (for last agent) in this random_allocated_matrices item: #
171+
# --------------------------------------------------------------------------------------------------- #
172+
elif len(goods_list_for_each_steps) == 1:
173+
# ----------------------------------------------------------- #
174+
# set the definitive allocating matrix for current agent #
175+
# ----------------------------------------------------------- #
176+
for i in range(len(goods_list_param)):
177+
if i == list(goods_list_for_each_steps.values())[0]:
178+
# ------------------------------------------------------- #
179+
# if current good(index) allocated to current agent: #
180+
# ------------------------------------------------------- #
181+
random_allocated_matrices[index_of_allocated_matrix][agent_key][i] = 1
182+
else:
183+
random_allocated_matrices[index_of_allocated_matrix][agent_key][i] = 0
184+
185+
# --------------------------------------------------------- #
186+
# prepare the key for store the next allocated matrix: #
187+
# --------------------------------------------------------- #
188+
index_of_allocated_matrix += 1
189+
# ---------------------------------------------------------------------------- #
190+
191+
192+
def calculate_final_matrix(matrices_param):
193+
global final_matrix, number_of_growth_RSD_items
194+
for key in matrices_param:
195+
matrix = matrices_param[key]
196+
for agent_index in matrix:
197+
agent = matrix[agent_index]
198+
# --------------------------------------------------------------------------------------------------- #
199+
# if final matrix had not prepared for current agent possibilities calculateing, then prepare it: #
200+
# --------------------------------------------------------------------------------------------------- #
201+
try:
202+
# --------------------------------- #
203+
# if this dictionary created? #
204+
# --------------------------------- #
205+
final_matrix[agent_index][0]
206+
except:
207+
# ------------------------------------------------------------------------------------------ #
208+
# if not, then prepare the dictionary to storing(summation) good indexes value of each agents: #
209+
# ------------------------------------------------------------------------------------------ #
210+
final_matrix[agent_index] = {}
211+
# ------------------------------------------------------------------- #
212+
# summation the result of each allocated matrices for each agent #
213+
# ------------------------------------------------------------------- #
214+
for good_index in agent:
215+
current_allocate_possibility = agent[good_index]
216+
if good_index in final_matrix[agent_index]:
217+
final_matrix[agent_index][good_index] += current_allocate_possibility
218+
else:
219+
final_matrix[agent_index][good_index] = current_allocate_possibility
220+
# --------------------------------------------------- #
221+
# finally, for special final possibility showing #
222+
# --------------------------------------------------- #
223+
for agent_index in final_matrix:
224+
agent = final_matrix[agent_index]
225+
for good_index in agent:
226+
acumulated_results = agent[good_index]
227+
if acumulated_results != 0:
228+
final_matrix[agent_index][good_index] = str(acumulated_results) + "/" + str(number_of_growth_RSD_items)
229+
# ---------------------------------------------------------------------------- #
230+
231+
232+
number_of_agents = len(agents)
233+
if number_of_agents > 1:
234+
growth_RSD_items = {}
235+
RSD_item_number = 0
236+
last_RSD_item_number = 0
237+
set_order_of_agents_items(agents, {}, 0)
238+
# ------------------------------------------------------------------------------------------------------------ #
239+
# dictionary => index of growth RSD items => order of each agents => agents index => agents preferences index: #
240+
# ------------------------------------------------------------------------------------------------------------ #
241+
# pretty(growth_RSD_items)
242+
243+
244+
# -------------------------------------------------------------------------- #
245+
# number of goods(services) that we want to allocate = number of agents #
246+
# -------------------------------------------------------------------------- #
247+
goods_list = {}
248+
for i in range(0, number_of_agents, 1):
249+
goods_list[i] = i
250+
251+
number_of_growth_RSD_items = len(growth_RSD_items)
252+
# ---------------------------------------------------- #
253+
# prepare a variable to store allocated matrices: #
254+
# ---------------------------------------------------- #
255+
random_allocated_matrices = {}
256+
set_various_allocates(growth_RSD_items, goods_list)
257+
# ------------------------------------------------------------------------------------------------------------------------------- #
258+
# dictionary => index of allocated {matrices} => order of each agents => agents index => agents good(service) allocated {matrix} : #
259+
# ------------------------------------------------------------------------------------------------------------------------------- #
260+
# pretty(random_allocated_matrices)
261+
262+
263+
# ------------------------------------------------------------------------------- #
264+
# prepare a variable to calculate good allocate possibility for each agents: #
265+
# ------------------------------------------------------------------------------- #
266+
final_matrix = {}
267+
calculate_final_matrix(random_allocated_matrices)
268+
# ----------------------------------------------------------------------- #
269+
# dictionary => agents index => agent good(service) allocate possibility: #
270+
# ----------------------------------------------------------------------- #
271+
# pprint(final_matrix)
272+
pretty(final_matrix)
273+
# ---------------------------------------------------------------------------- #

0 commit comments

Comments
 (0)