topic description
algorithm description:
traverses the sublists in the list Users (in user1, user2 order) and matches the sublists in CSPs (using the fitfun function). After matching, delete the elements that match successfully in Users and CSPs, and continue to match the remaining elements.
sources of topics and their own ideas
algorithm design project
related codes
/ / Please paste the code text below (do not replace the code with pictures)
import numpy
G = []
def fitfun(user, csp):
fitness = round(numpy.sqrt(pow((user[0] - csp[0]), 2) + pow((user[2] - csp[1]), 2)),3)
return fitness
def allocation(Users, CSPs):
T = []
print("input Users Matrix:", Users)
print("input CSPs Matrix:", CSPs,"\n")
for i, user in enumerate(Users):
for j, csp in enumerate(CSPs):
if (csp[0] > user[0] and csp[1] < user[2]):
T.append(csp)
print("updated2 Users Matrix:", Users)
print("updated2 CSPs Matrix:", CSPs)
print("user", i, "suitCSPs:", T)
if T:
C = []
for suitcsp in T:
result = fitfun(user, suitcsp)
print("fitness:", result)
C.append([result, user, suitcsp])
print("suitable group:", C,"\n")
minindex = 0
for z, item in enumerate(C):
if item[0] < C[minindex][0]:
minindex = z
print("minindex:", minindex)
print("dealusercsp", [C[minindex][1],C[minindex][2]],"\n")
G.append([C[minindex][1],C[minindex][2]])
print("Users Matrix:", Users)
print("CSPs Matrix:", CSPs,"\n")
Users.remove(C[minindex][1])
CSPs.remove(C[minindex][2])
print("updated Users Matrix:", Users)
print("updated CSPs Matrix:", CSPs)
print("allocation matrix:", G,"\n")
def main():
Users = [[7, 24, 7, 13.6], [3, 30, 3, 5.0], [7, 41, 7, 9.4], [5, 34, 5, 7.4], [2, 18, 2, 3.8]]
CSPs = [[12, 8.4, 10.8], [12, 8.4, 8.2], [4, 2.8, 3.1], [5, 3.5, 2.0]]
round = 1
print("origin Users Matrix:", Users)
print("origin CSPs Matrix:", CSPs,"\n")
allocation(Users, CSPs)
print("round", round, "over\n\n")
if __name__ == "__main__":
main()
output
C:\Users\KING\PycharmProjects\originauction\venv\Scripts\python.exe C:/Users/KING/PycharmProjects/originauction/temp.py
origin Users Matrix: [[7, 24, 7, 13.6], [3, 30, 3, 5.0], [7, 41, 7, 9.4], [5, 34, 5, 7.4], [2, 18, 2, 3.8]]
origin CSPs Matrix: [[12, 8.4, 10.8], [12, 8.4, 8.2], [4, 2.8, 3.1], [5, 3.5, 2.0]]
input Users Matrix: [[7, 24, 7, 13.6], [3, 30, 3, 5.0], [7, 41, 7, 9.4], [5, 34, 5, 7.4], [2, 18, 2, 3.8]]
input CSPs Matrix: [[12, 8.4, 10.8], [12, 8.4, 8.2], [4, 2.8, 3.1], [5, 3.5, 2.0]]
updated2 Users Matrix: [[7, 24, 7, 13.6], [3, 30, 3, 5.0], [7, 41, 7, 9.4], [5, 34, 5, 7.4], [2, 18, 2, 3.8]]
updated2 CSPs Matrix: [[12, 8.4, 10.8], [12, 8.4, 8.2], [4, 2.8, 3.1], [5, 3.5, 2.0]]
user 0 suitCSPs: []
Traceback (most recent call last):
updated2 Users Matrix: [[7, 24, 7, 13.6], [3, 30, 3, 5.0], [7, 41, 7, 9.4], [5, 34, 5, 7.4], [2, 18, 2, 3.8]]
updated2 CSPs Matrix: [[12, 8.4, 10.8], [12, 8.4, 8.2], [4, 2.8, 3.1], [5, 3.5, 2.0]]
user 1 suitCSPs: [[4, 2.8, 3.1]]
fitness: 1.02
File "C:/Users/KING/PycharmProjects/originauction/temp.py", line 55, in <module>
suitable group: [[1.02, [3, 30, 3, 5.0], [4, 2.8, 3.1]]]
main()
minindex: 0
File "C:/Users/KING/PycharmProjects/originauction/temp.py", line 51, in main
allocation(Users, CSPs)
dealusercsp [[3, 30, 3, 5.0], [4, 2.8, 3.1]]
File "C:/Users/KING/PycharmProjects/originauction/temp.py", line 40, in allocation
CSPs.remove(C[minindex][2])
Users Matrix: [[7, 24, 7, 13.6], [3, 30, 3, 5.0], [7, 41, 7, 9.4], [5, 34, 5, 7.4], [2, 18, 2, 3.8]]
ValueError: list.remove(x): x not in list
CSPs Matrix: [[12, 8.4, 10.8], [12, 8.4, 8.2], [4, 2.8, 3.1], [5, 3.5, 2.0]]
updated Users Matrix: [[7, 24, 7, 13.6], [7, 41, 7, 9.4], [5, 34, 5, 7.4], [2, 18, 2, 3.8]]
updated CSPs Matrix: [[12, 8.4, 10.8], [12, 8.4, 8.2], [5, 3.5, 2.0]]
allocation matrix: [[[3, 30, 3, 5.0], [4, 2.8, 3.1]]]
updated2 Users Matrix: [[7, 24, 7, 13.6], [7, 41, 7, 9.4], [5, 34, 5, 7.4], [2, 18, 2, 3.8]]
updated2 CSPs Matrix: [[12, 8.4, 10.8], [12, 8.4, 8.2], [5, 3.5, 2.0]]
user 2 suitCSPs: [[4, 2.8, 3.1]]
fitness: 2.417
suitable group: [[2.417, [5, 34, 5, 7.4], [4, 2.8, 3.1]]]
minindex: 0
dealusercsp [[5, 34, 5, 7.4], [4, 2.8, 3.1]]
Users Matrix: [[7, 24, 7, 13.6], [7, 41, 7, 9.4], [5, 34, 5, 7.4], [2, 18, 2, 3.8]]
CSPs Matrix: [[12, 8.4, 10.8], [12, 8.4, 8.2], [5, 3.5, 2.0]]
Process finished with exit code 1
what result do you expect? What is the error message actually seen?
expect the sublist of CSPs list [4,2.8,3.1] to match, then delete it from the CSPs list, and no longer match it in the future
actually still matches after deletion