本帖最后由 流量之神 于 2018-7-13 21:35 编辑
算法描述:
遍历列表Users中的子列表(按照user1, user2顺序),和CSPs中的子列表匹配(用fitfun函数匹配),匹配完以后删除Users和CSPs中匹配成功的元素,剩下的元素继续匹配。
[4, 2.8, 3.1] 这个子列表删除好像出了问题啊呀,虽然删除了,但是user2还是会继续和这个子列表匹配。这个bug困扰我一天了,求斑竹慢移
------------------------------------------------------------------------------------------------------------------------------------------------
- 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()
复制代码
输出
- 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: []
- 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
- suitable group: [[1.02, [3, 30, 3, 5.0], [4, 2.8, 3.1]]]
- minindex: 0
- dealusercsp [[3, 30, 3, 5.0], [4, 2.8, 3.1]]
- 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]]
- 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]]
- Traceback (most recent call last):
- File "C:/Users/KING/PycharmProjects/originauction/temp.py", line 62, in <module>
- main()
- File "C:/Users/KING/PycharmProjects/originauction/temp.py", line 58, in main
- allocation(Users, CSPs)
- File "C:/Users/KING/PycharmProjects/originauction/temp.py", line 44, in allocation
- CSPs.remove(C[minindex][2])
- ValueError: list.remove(x): x not in list
- Process finished with exit code 1
复制代码
111.PNG
(63.78 KB, 下载次数: 1)
|