/
TaskAssigningGenerator.java
284 lines (264 loc) · 12.2 KB
/
TaskAssigningGenerator.java
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.optaplanner.examples.taskassigning.persistence;
import java.io.File;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import org.optaplanner.examples.common.app.LoggingMain;
import org.optaplanner.examples.common.persistence.AbstractSolutionImporter;
import org.optaplanner.examples.common.persistence.SolutionDao;
import org.optaplanner.examples.common.persistence.StringDataGenerator;
import org.optaplanner.examples.taskassigning.domain.Affinity;
import org.optaplanner.examples.taskassigning.domain.Customer;
import org.optaplanner.examples.taskassigning.domain.Employee;
import org.optaplanner.examples.taskassigning.domain.Skill;
import org.optaplanner.examples.taskassigning.domain.Task;
import org.optaplanner.examples.taskassigning.domain.TaskAssigningSolution;
import org.optaplanner.examples.taskassigning.domain.TaskType;
public class TaskAssigningGenerator extends LoggingMain {
private static final StringDataGenerator skillNameGenerator = new StringDataGenerator()
.addPart(
"Problem",
"Team",
"Business",
"Risk",
"Creative",
"Strategic",
"Customer",
"Conflict",
"IT",
"Academic")
.addPart(
"Solving",
"Building",
"Storytelling",
"Management",
"Thinking",
"Planning",
"Service",
"Resolution",
"Engineering",
"Research");
private static final StringDataGenerator taskTypeNameGenerator = new StringDataGenerator()
.addPart(
"Improve",
"Expand",
"Shrink",
"Approve",
"Localize",
"Review",
"Clean",
"Merge",
"Double",
"Optimize")
.addPart(
"Sales",
"Tax",
"VAT",
"Legal",
"Cloud",
"Marketing",
"IT",
"Contract",
"Financial",
"Advertisement")
.addPart(
"Software",
"Development",
"Accounting",
"Management",
"Facilities",
"Writing",
"Productization",
"Lobbying",
"Engineering",
"Research");
private static final StringDataGenerator customerNameGenerator = StringDataGenerator.build1kCompanyNames();
private static final StringDataGenerator employeeNameGenerator = StringDataGenerator.build10kFullNames();
private static final int BASE_DURATION_MINIMUM = 20;
private static final int BASE_DURATION_MAXIMUM = 120;
private static final int SKILL_SET_SIZE_MINIMUM = 2;
private static final int SKILL_SET_SIZE_MAXIMUM = 7;
public static void main(String[] args) {
new TaskAssigningGenerator().generate();
}
protected final SolutionDao solutionDao;
protected final File outputDir;
protected Random random;
public TaskAssigningGenerator() {
solutionDao = new TaskAssigningDao();
outputDir = new File(solutionDao.getDataDir(), "unsolved");
}
public void generate() {
writeTaskAssigningSolution(50, 5);
writeTaskAssigningSolution(100, 5);
writeTaskAssigningSolution(500, 20);
writeTaskAssigningSolution(1000, 20);
}
private void writeTaskAssigningSolution(int taskListSize, int employeeListSize) {
int skillListSize = SKILL_SET_SIZE_MAXIMUM + (int) Math.log(employeeListSize);
int taskTypeListSize = taskListSize / 5;
int customerListSize = Math.min(taskTypeListSize, employeeListSize * 3);
String fileName = determineFileName(taskListSize, employeeListSize);
File outputFile = new File(outputDir, fileName + ".xml");
TaskAssigningSolution solution = createTaskAssigningSolution(fileName,
taskListSize, skillListSize, employeeListSize, taskTypeListSize, customerListSize);
solutionDao.writeSolution(solution, outputFile);
}
private String determineFileName(int taskListSize, int employeeListSize) {
return taskListSize + "tasks-" + employeeListSize + "employees";
}
public TaskAssigningSolution createTaskAssigningSolution(String fileName, int taskListSize, int skillListSize,
int employeeListSize, int taskTypeListSize, int customerListSize) {
random = new Random(37);
skillNameGenerator.reset();
taskTypeNameGenerator.reset();
customerNameGenerator.reset();
employeeNameGenerator.reset();
TaskAssigningSolution solution = new TaskAssigningSolution();
solution.setId(0L);
createSkillList(solution, skillListSize);
createCustomerList(solution, customerListSize);
createEmployeeList(solution, employeeListSize);
createTaskTypeList(solution, taskTypeListSize);
createTaskList(solution, taskListSize);
BigInteger possibleSolutionSize
= AbstractSolutionImporter.InputBuilder.factorial(taskListSize + employeeListSize - 1)
.divide(AbstractSolutionImporter.InputBuilder.factorial(employeeListSize - 1));
logger.info("TaskAssigningSolution {} has {} tasks, {} skills, {} employees, {} task types and {} customers with a search space of {}.",
fileName,
taskListSize,
skillListSize,
employeeListSize,
taskTypeListSize,
customerListSize,
AbstractSolutionImporter.getFlooredPossibleSolutionSize(possibleSolutionSize));
return solution;
}
private void createSkillList(TaskAssigningSolution solution, int skillListSize) {
List<Skill> skillList = new ArrayList<>(skillListSize);
for (int i = 0; i < skillListSize; i++) {
Skill skill = new Skill();
skill.setId((long) i);
String skillName = skillNameGenerator.generateNextValue();
skill.setName(skillName);
logger.trace("Created skill with skillName ({}).",skillName);
skillList.add(skill);
}
solution.setSkillList(skillList);
}
private void createCustomerList(TaskAssigningSolution solution, int customerListSize) {
List<Customer> customerList = new ArrayList<>(customerListSize);
for (int i = 0; i < customerListSize; i++) {
Customer customer = new Customer();
customer.setId((long) i);
String customerName = customerNameGenerator.generateNextValue();
customer.setName(customerName);
logger.trace("Created skill with customerName ({}).",customerName);
customerList.add(customer);
}
solution.setCustomerList(customerList);
}
private void createEmployeeList(TaskAssigningSolution solution, int employeeListSize) {
List<Skill> skillList = solution.getSkillList();
List<Customer> customerList = solution.getCustomerList();
Affinity[] affinities = Affinity.values();
List<Employee> employeeList = new ArrayList<>(employeeListSize);
int skillListIndex = 0;
for (int i = 0; i < employeeListSize; i++) {
Employee employee = new Employee();
employee.setId((long) i);
String fullName = employeeNameGenerator.generateNextValue();
employee.setFullName(fullName);
int skillSetSize = SKILL_SET_SIZE_MINIMUM + random.nextInt(SKILL_SET_SIZE_MAXIMUM - SKILL_SET_SIZE_MINIMUM);
if (skillSetSize > skillList.size()) {
skillSetSize = skillList.size();
}
Set<Skill> skillSet = new LinkedHashSet<>(skillSetSize);
for (int j = 0; j < skillSetSize; j++) {
skillSet.add(skillList.get(skillListIndex));
skillListIndex = (skillListIndex + 1) % skillList.size();
}
employee.setSkillSet(skillSet);
Map<Customer, Affinity> affinityMap = new LinkedHashMap<>(customerList.size());
for (Customer customer : customerList) {
affinityMap.put(customer, affinities[random.nextInt(affinities.length)]);
}
employee.setAffinityMap(affinityMap);
logger.trace("Created employee with fullName ({}).", fullName);
employeeList.add(employee);
}
solution.setEmployeeList(employeeList);
}
private void createTaskTypeList(TaskAssigningSolution solution, int taskTypeListSize) {
List<Employee> employeeList = solution.getEmployeeList();
List<TaskType> taskTypeList = new ArrayList<>(taskTypeListSize);
Set<String> codeSet = new HashSet<>(taskTypeListSize);
for (int i = 0; i < taskTypeListSize; i++) {
TaskType taskType = new TaskType();
taskType.setId((long) i);
String title = taskTypeNameGenerator.generateNextValue();
taskType.setTitle(title);
String code = title.replaceAll("(\\w)\\w* (\\w)\\w* (\\w)\\w*", "$1$2$3");
taskType.setCode(code);
if (!codeSet.add(code)) {
throw new IllegalStateException("The taskType code (" + codeSet.add(code) + ") already exists.");
}
taskType.setBaseDuration(
BASE_DURATION_MINIMUM + random.nextInt(BASE_DURATION_MAXIMUM - BASE_DURATION_MINIMUM));
Employee randomEmployee = employeeList.get(random.nextInt(employeeList.size()));
ArrayList<Skill> randomSkillList = new ArrayList<>(randomEmployee.getSkillSet());
Collections.shuffle(randomSkillList);
int requiredSkillListSize = 1 + random.nextInt(randomSkillList.size() - 1);
taskType.setRequiredSkillList(new ArrayList<>(randomSkillList.subList(0, requiredSkillListSize)));
logger.trace("Created taskType with title ({}).", title);
taskTypeList.add(taskType);
}
solution.setTaskTypeList(taskTypeList);
}
private void createTaskList(TaskAssigningSolution solution, int taskListSize) {
List<Task> taskList = new ArrayList<>(taskListSize);
List<TaskType> taskTypeList = solution.getTaskTypeList();
List<Customer> customerList = solution.getCustomerList();
Map<TaskType, Integer> maxIndexInTaskTypeMap = new HashMap<>(taskTypeList.size());
for (int i = 0; i < taskListSize; i++) {
Task task = new Task();
task.setId((long) i);
TaskType taskType = taskTypeList.get(random.nextInt(taskTypeList.size()));
task.setTaskType(taskType);
Integer indexInTaskType = maxIndexInTaskTypeMap.get(taskType);
if (indexInTaskType == null) {
indexInTaskType = 1;
} else {
indexInTaskType++;
}
task.setIndexInTaskType(indexInTaskType);
maxIndexInTaskTypeMap.put(taskType, indexInTaskType);
task.setCustomer(customerList.get(random.nextInt(customerList.size())));
taskList.add(task);
}
solution.setTaskList(taskList);
}
}