-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnum_localities.py
130 lines (119 loc) · 5.32 KB
/
num_localities.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
from random import random, randrange, seed
import multiprocessing
from multiprocessing import Pool
from models import *
from methods import *
import argparse
num_professions = 2 # This is a constant; changing it requires
# further code modifications
num_agents = 100
prof1 = 50
prof2 = num_agents - prof1
professions = [0] * prof1 + [1] * prof2
random_samples = 1000
Real_evaluation_samples=10000
def _distribute_caps_and_jobs(num_localities):
assert num_localities <= num_agents
# Distribute caps adding up to `num_agents` over all localities,
# ensuring that each locality has at least one space
locality_caps = [1 for _ in range(num_localities)]
for _ in range(num_agents - num_localities):
locality_caps[randrange(len(locality_caps))] += 1
# Job numbers add up to the cap per locality, but `prof1` jobs for
# profession 1 and `prof2` jobs for profession 2 are randomly
# distributed inside these bounds.
prof1_jobs = prof1 # Remaining jobs to distribute
prof2_jobs = prof2
job_numbers = []
for cap in locality_caps:
p1, p2 = 0, 0
for _ in range(cap):
if random() < prof1_jobs / (prof1_jobs + prof2_jobs):
p1 += 1
prof1_jobs -= 1
assert prof1_jobs >= 0
else:
p2 += 1
prof2_jobs -= 1
assert prof2_jobs >= 0
job_numbers.append((p1, p2))
return locality_caps, job_numbers
class CorrectionFunctionWrapper():
def __init__(self, P):
self.P = P
def func(self, x):
return min(x, self.P)
def test_correction(num_localities):
locality_caps, job_numbers = \
_distribute_caps_and_jobs(num_localities)
qualification_probabilities = \
[[random()] * num_localities for _ in range(num_agents)]
correction_functions = []
for p1, p2 in job_numbers:
correction_functions.append((CorrectionFunctionWrapper(p1), CorrectionFunctionWrapper(p2)))
model = RetroactiveCorrectionModel(num_agents, locality_caps,
num_professions, professions,
qualification_probabilities,
correction_functions,
random_samples,Real_evaluation_samples)
return model
def test_interview(num_localities):
locality_caps, job_numbers = \
_distribute_caps_and_jobs(num_localities)
compatibility_probabilities = [random() for _ in range(num_agents)]
model = InterviewModel(num_agents, locality_caps, num_professions,
professions, job_numbers,
compatibility_probabilities, random_samples,Real_evaluation_samples)
return model
def test_coordination(num_localities):
locality_caps, job_numbers = \
_distribute_caps_and_jobs(num_localities)
locality_num_jobs = locality_caps
compatibility_probabilities = []
for _ in range(prof1):
competency = random()
compatibility_probabilities.append(
[[competency] * p1 + [0.] * p2 for p1, p2 in job_numbers])
for _ in range(prof2):
competency = random()
compatibility_probabilities.append(
[[0.] * p1 + [competency] * p2 for p1, p2 in job_numbers])
model = CoordinationModel(num_agents, locality_caps,
locality_num_jobs,
compatibility_probabilities,
random_samples,Real_evaluation_samples)
return model
def main(args):
settings = {"correction": test_correction, "interview": test_interview,
"coordination": test_coordination}
#重点
seed(args.seed)
m = settings[args.set](args.p)
print(args.variant)
if(args.variant=="True"):
flag=True
else:
flag=False
if args.alg == "add":
additive_optimization(m, args.dir, args.p, args.set, args.index)
elif args.alg == "greedy":
greedy_algorithm(m, args.dir, args.p, args.set, args.index)
elif args.alg == "nsga2":
nsga2_method(m, args.dir, args.p, args.set, args.index,False)
elif args.alg == "gsemo" or args.alg == "gsemo_col" or args.alg == "gsemo_row" or args.alg == "gsemo_row_col":
gsemo_algorithm(m, args.dir, args.p, args.set, args.index, args.alg, flag)
elif args.alg == "moead":
moead(m, args.dir, args.p, args.set, args.index)#这里没有加true 修复不可行解的代码直接在代码里加了
else :
print("wrong algorithm name")
if __name__ == '__main__':
argparser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
argparser.add_argument('-alg', type=str,default="moead")
argparser.add_argument('-dir', help="folder", type=str)
argparser.add_argument('-set', type=str, help="model name",default="coordination")
argparser.add_argument('-p', help="vary num of localities",type=int,default="10")
argparser.add_argument('-index', help="instance",type=int,default="0")
argparser.add_argument('-seed',help="seed_of_instance",type=float,default=0)
argparser.add_argument('-variant', type=str, default="False")
args = argparser.parse_args()
main(args)