Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
H
HYH.APSJ
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
佟礼
HYH.APSJ
Commits
899a4be3
Commit
899a4be3
authored
Dec 01, 2025
by
Tong Li
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
遗传算法
parent
1173674e
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
102 additions
and
39 deletions
+102
-39
ResourceGanttController.java
...ava/com/aps/controller/gantt/ResourceGanttController.java
+10
-0
Chromosome.java
src/main/java/com/aps/entity/Algorithm/Chromosome.java
+8
-0
GeneticDecoder.java
src/main/java/com/aps/service/Algorithm/GeneticDecoder.java
+10
-0
ScheduleOperationService.java
...a/com/aps/service/Algorithm/ScheduleOperationService.java
+17
-4
MachineSchedulerService.java
...in/java/com/aps/service/plan/MachineSchedulerService.java
+1
-1
PlanResultService.java
src/main/java/com/aps/service/plan/PlanResultService.java
+44
-22
orders.json
src/main/resources/orders.json
+3
-3
products.json
src/main/resources/products.json
+9
-9
No files found.
src/main/java/com/aps/controller/gantt/ResourceGanttController.java
View file @
899a4be3
...
...
@@ -134,6 +134,16 @@ public class ResourceGanttController {
// 提取所有场景ID
return
scheduleChromosomes
;
}
@GetMapping
(
"/Move"
)
public
Chromosome
Move
()
{
// 调用 PlanResultService 获取 ScheduleChromosome 列表
Chromosome
scheduleChromosomes
=
planResultService
.
Move
(
"B571EF6682DB463AB2977B1055A74112"
);
// 提取所有场景ID
return
scheduleChromosomes
;
}
/**
* 将 ScheduleChromosome 转换为 ResourceGanttVO 列表
...
...
src/main/java/com/aps/entity/Algorithm/Chromosome.java
View file @
899a4be3
package
com
.
aps
.
entity
.
Algorithm
;
import
com.aps.entity.Algorithm.IDAndChildID.GroupResult
;
import
com.aps.entity.basic.Entry
;
import
com.aps.entity.basic.Machine
;
import
com.aps.entity.basic.Order
;
import
lombok.Data
;
import
java.time.LocalDateTime
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.UUID
;
import
java.util.stream.Collectors
;
...
...
@@ -57,6 +59,7 @@ public class Chromosome {
private
List
<
Entry
>
allOperations
;
private
List
<
Order
>
orders
;
private
List
<
Machine
>
InitMachines
;
private
List
<
GroupResult
>
OperatRel
;
/// <summary>
/// 适应度值
/// </summary>
...
...
@@ -72,6 +75,11 @@ public class Chromosome {
/// </summary>
private
List
<
GAScheduleResult
>
Result
;
/// <summary>
/// 解码后的调度结果
/// </summary>
private
List
<
GAScheduleResult
>
ResultOld
=
new
ArrayList
<>();
/// <summary>
/// 最早完工时间
/// </summary>
...
...
src/main/java/com/aps/service/Algorithm/GeneticDecoder.java
View file @
899a4be3
...
...
@@ -286,6 +286,13 @@ public class GeneticDecoder {
System
.
out
.
println
(
" 标准模式:安排主处理+"
+
setupTime
+
"分钟换型="
+
processingTimeTotal
+
"分钟"
);
}
GAScheduleResult
existingResult
=
chromosome
.
getResultOld
().
stream
().
filter
(
r
->
r
.
getOperationId
()
==
operation
.
Id
).
findFirst
().
orElse
(
null
);
if
(
existingResult
!=
null
)
{
earliestStartTime
=
Math
.
max
(
earliestStartTime
,
existingResult
.
getDesignatedStartTime
());
}
MachineCalculator
machineCalculator
=
new
MachineCalculator
(
baseTime
,
machines
,
machineScheduler
);
List
<
ScheduleResultDetail
>
geneDetails
=
machineCalculator
.
getNextAvailableTime
(
machine
,
earliestStartTime
,
-
1
,
processingTimeTotal
,
chromosome
.
getResult
(),
false
,
true
,
true
);
...
...
@@ -347,6 +354,9 @@ public class GeneticDecoder {
result
.
setStartTime
(
startTime
);
result
.
setEndTime
(
endTime
);
result
.
setTeardownTime
(
teardownTime
);
if
(
existingResult
!=
null
)
{
result
.
setDesignatedStartTime
(
existingResult
.
getDesignatedStartTime
());
}
result
.
setOneTime
(
processingTime
);
result
.
setProcessingTime
(
processingTimeTotal
);
...
...
src/main/java/com/aps/service/Algorithm/ScheduleOperationService.java
View file @
899a4be3
package
com
.
aps
.
service
.
Algorithm
;
import
com.aps.common.util.ProductionDeepCopyUtil
;
import
com.aps.entity.Algorithm.Chromosome
;
import
com.aps.entity.Algorithm.GAScheduleResult
;
import
com.aps.entity.Algorithm.GlobalOperationInfo
;
...
...
@@ -42,6 +43,16 @@ public class ScheduleOperationService {
.
findFirst
()
.
orElseThrow
(()
->
new
NoSuchElementException
(
"Operation not found: "
+
opId
));
int
machineOptionIndex
=
targetOp
.
getMachineOptions
().
stream
()
.
map
(
MachineOption:
:
getMachineId
)
.
collect
(
Collectors
.
toList
())
.
indexOf
(
newMachineId
)
+
1
;
if
(
machineOptionIndex
==
0
)
{
throw
new
NoSuchElementException
(
"Machine not found: "
+
newMachineId
);
}
// 设置约束
targetResult
.
setDesignatedStartTime
(
newStartTime
);
targetResult
.
setForcedMachineId
(
newMachineId
);
...
...
@@ -53,10 +64,9 @@ public class ScheduleOperationService {
.
map
(
GlobalOperationInfo:
:
getGlobalOpId
)
.
orElseThrow
(()
->
new
NoSuchElementException
(
"Global operation not found: "
+
opId
));
int
machineOptionIndex
=
targetOp
.
getMachineOptions
().
stream
()
.
map
(
MachineOption:
:
getMachineId
)
.
collect
(
Collectors
.
toList
())
.
indexOf
(
newMachineId
)
+
1
;
chromosome
.
getMachineSelection
().
set
(
globalOpIndex
,
machineOptionIndex
);
...
...
@@ -86,6 +96,9 @@ public class ScheduleOperationService {
MachineSchedulerService
machineScheduler
=
new
MachineSchedulerService
(
baseTime
);
GeneticDecoder
decoder
=
new
GeneticDecoder
(
globalParam
,
baseTime
,
chromosome
.
getInitMachines
(),
chromosome
.
getOrders
(),
null
,
machineScheduler
);
chromosome
.
setMachines
(
chromosome
.
getInitMachines
());
chromosome
.
setResultOld
(
ProductionDeepCopyUtil
.
deepCopyList
(
chromosome
.
getResult
()));
chromosome
.
getResult
().
clear
();
decoder
.
decode
(
chromosome
);
if
(
chromosome
.
getFitness
()==
0
)
{
...
...
src/main/java/com/aps/service/plan/MachineSchedulerService.java
View file @
899a4be3
...
...
@@ -207,7 +207,7 @@ public class MachineSchedulerService {
&&
containsDay
(
s
.
getDays
(),
date
.
getDayOfWeek
()))
.
collect
(
Collectors
.
toList
());
if
(
shifts
==
null
||
shifts
.
size
()==
0
)
{
LocalDate
sd
=
LocalDate
.
of
(
20
20
,
10
,
1
);
LocalDate
sd
=
LocalDate
.
of
(
20
00
,
1
,
1
);
shifts
=
machine
.
getShifts
().
stream
()
.
filter
(
s
->
sd
.
compareTo
(
s
.
getStartDate
().
toLocalDate
())==
0
&&
s
.
getDays
()
!=
null
&&
containsDay
(
s
.
getDays
(),
date
.
getDayOfWeek
()))
...
...
src/main/java/com/aps/service/plan/PlanResultService.java
View file @
899a4be3
...
...
@@ -163,7 +163,7 @@ public class PlanResultService {
}
}
ScheduleParams
param
=
new
ScheduleParams
();
param
.
setBaseTime
(
LocalDateTime
.
of
(
2025
,
1
0
,
1
,
0
,
0
,
0
));
param
.
setBaseTime
(
LocalDateTime
.
of
(
2025
,
1
1
,
1
,
0
,
0
,
0
));
param
.
setPopulationSize
(
50
);
param
.
setMaxIterations
(
100
);
...
...
@@ -193,6 +193,7 @@ public class PlanResultService {
int
id
=
1
;
for
(
Order
order
:
orders
)
{
order
.
setDueDate
(
LocalDateTime
.
of
(
2025
,
12
,
1
,
0
,
0
,
0
));
// 假设products是一个List<Product>,根据Product的Id查找对应的产品
Product
product
=
products
.
stream
()
.
filter
(
p
->
p
.
getId
()
==
order
.
getProductId
())
...
...
@@ -224,11 +225,11 @@ public class PlanResultService {
// 5. 执行调度算法
GeneticAlgorithm
scheduler
=
new
GeneticAlgorithm
(
globalParam
,
machines
,
orders
,
null
,
machineScheduler
);
//new GeneticAlgorithm(products, machines, orders, machineScheduler);
Chromosome
Chromosomes
=
scheduler
.
Run
(
param
,
allOperations
);
WriteScheduleSummary
(
Chromosomes
);
ScheduleOperationService
ScheduleOperation
=
new
ScheduleOperationService
();
LocalDateTime
ds
=
LocalDateTime
.
of
(
2025
,
12
,
7
,
23
,
59
);
ScheduleOperation
.
moveOperation
(
Chromosomes
,
3
,
(
int
)
ChronoUnit
.
SECONDS
.
between
(
param
.
getBaseTime
(),
ds
),
2
,
param
.
getBaseTime
(),
globalParam
);
//Chromosomes.forEach(this::WriteScheduleSummary
);
WriteScheduleSummary
(
Chromosomes
);
return
Chromosomes
;
...
...
@@ -276,29 +277,46 @@ public class PlanResultService {
orders
.
add
(
order
);
}
List
<
Entry
>
entrys
=
InitEntrys
(
SceneId
,
ProdEquipments
,
ProdLaunchOrders
);
Map
<
Integer
,
Object
>
list
=
InitEntrys
(
SceneId
,
ProdEquipments
,
ProdLaunchOrders
);
List
<
Entry
>
entrys
=(
List
<
Entry
>)
list
.
get
(
1
);
List
<
GroupResult
>
entryRel
=(
List
<
GroupResult
>)
list
.
get
(
2
);
GlobalParam
globalParam
=
new
GlobalParam
();
// 5. 执行调度算法
GeneticAlgorithm
scheduler
=
new
GeneticAlgorithm
(
globalParam
,
machines
,
orders
,
null
,
machineScheduler
);
//new GeneticAlgorithm(products, machines, orders, machineScheduler);
Chromosome
Chromosomes
=
scheduler
.
Run
(
param
,
entrys
);
Chromosome
chromosome
=
scheduler
.
Run
(
param
,
entrys
);
chromosome
.
setOperatRel
(
entryRel
);
_sceneService
.
saveChromosomeToFile
(
chromosome
,
SceneId
);
// Chromosomes.forEach(this::WriteScheduleSummary);
return
Chromosomes
;
return
chromosome
;
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
"调度执行失败"
,
e
);
}
}
public
Chromosome
Move
(
String
SceneId
)
{
ScheduleParams
param
=
new
ScheduleParams
();
param
.
setBaseTime
(
LocalDateTime
.
of
(
2025
,
11
,
1
,
0
,
0
,
0
));
GlobalParam
globalParam
=
new
GlobalParam
();
Chromosome
chromosome
=
_sceneService
.
loadChromosomeFromFile
(
SceneId
);
WriteScheduleSummary
(
chromosome
);
ScheduleOperationService
ScheduleOperation
=
new
ScheduleOperationService
();
LocalDateTime
ds
=
LocalDateTime
.
of
(
2025
,
12
,
7
,
23
,
59
);
ScheduleOperation
.
moveOperation
(
chromosome
,
2
,
(
int
)
ChronoUnit
.
SECONDS
.
between
(
param
.
getBaseTime
(),
ds
),
2
,
param
.
getBaseTime
(),
globalParam
);
WriteScheduleSummary
(
chromosome
);
// _sceneService.saveChromosomeToFile(chromosome, SceneId);
return
chromosome
;
}
public
Chromosome
schedule
(
String
SceneId
)
{
try
{
ScheduleParams
param
=
new
ScheduleParams
();
param
.
setBaseTime
(
LocalDateTime
.
of
(
2025
,
11
,
1
,
0
,
0
,
0
));
param
.
setPopulationSize
(
50
);
param
.
setMaxIterations
(
100
);
...
...
@@ -335,7 +353,10 @@ public class PlanResultService {
orders
.
add
(
order
);
}
List
<
Entry
>
entrys
=
InitEntrys
(
SceneId
,
ProdEquipments
,
ProdLaunchOrders
);
Map
<
Integer
,
Object
>
list
=
InitEntrys
(
SceneId
,
ProdEquipments
,
ProdLaunchOrders
);
List
<
Entry
>
entrys
=(
List
<
Entry
>)
list
.
get
(
1
);
List
<
GroupResult
>
entryRel
=(
List
<
GroupResult
>)
list
.
get
(
2
);
GlobalParam
globalParam
=
new
GlobalParam
();
...
...
@@ -344,7 +365,7 @@ public class PlanResultService {
Chromosome
chromosomes
=
scheduler
.
Run
(
param
,
entrys
);
chromosomes
.
setScenarioID
(
SceneId
);
chromosomes
.
setBaseTime
(
param
.
getBaseTime
());
chromosomes
.
setOperatRel
(
entryRel
);
// 保存chromosomes到文件
_sceneService
.
saveChromosomeToFile
(
chromosomes
,
SceneId
);
...
...
@@ -575,9 +596,9 @@ public class PlanResultService {
}
private
List
<
Entry
>
InitEntrys
(
String
SceneId
,
List
<
ProdEquipment
>
ProdEquipments
,
List
<
ProdLaunchOrder
>
ProdLaunchOrders
)
private
Map
<
Integer
,
Object
>
InitEntrys
(
String
SceneId
,
List
<
ProdEquipment
>
ProdEquipments
,
List
<
ProdLaunchOrder
>
ProdLaunchOrders
)
{
Map
<
Integer
,
Object
>
list
=
new
HashMap
<>();
List
<
ProdProcessExec
>
ProdProcessExecs
=
_prodProcessExecService
.
lambdaQuery
()
.
eq
(
ProdProcessExec:
:
getSceneId
,
SceneId
)
.
list
();
...
...
@@ -621,14 +642,14 @@ public class PlanResultService {
for
(
int
i
=
0
;
i
<
results
.
size
();
i
++)
{
GroupResult
groupResult
=
results
.
get
(
i
);
List
<
NodeInfo
>
nodeInfoList
=
groupResult
.
getNodeInfoList
();
System
.
out
.
println
(
"分组"
+
(
i
+
1
)
+
"顺序:"
+
nodeInfoList
);
//
System.out.println("分组" + (i + 1) + "顺序:" + nodeInfoList);
for
(
NodeInfo
nodeInfo
:
nodeInfoList
)
{
System
.
out
.
printf
(
"原始ID:%s → 全局序号:%d,分组内序号:%d,新父ID列表:%s,新子ID列表:%s%n"
,
nodeInfo
.
getOriginalId
(),
nodeInfo
.
getGlobalSerial
(),
nodeInfo
.
getGroupSerial
(),
nodeInfo
.
getNewParentIds
().
isEmpty
()
?
"无"
:
nodeInfo
.
getNewParentIds
(),
nodeInfo
.
getNewChildIds
());
//
System.out.printf("原始ID:%s → 全局序号:%d,分组内序号:%d,新父ID列表:%s,新子ID列表:%s%n",
//
nodeInfo.getOriginalId(),
//
nodeInfo.getGlobalSerial(),
//
nodeInfo.getGroupSerial(),
//
nodeInfo.getNewParentIds().isEmpty() ? "无" : nodeInfo.getNewParentIds(),
//
nodeInfo.getNewChildIds());
Entry
entry
=
new
Entry
();
...
...
@@ -675,9 +696,10 @@ public class PlanResultService {
System
.
out
.
println
(
"------------------------"
);
}
list
.
put
(
1
,
entrys
);
list
.
put
(
2
,
results
);
return
entrys
;
return
list
;
}
...
...
src/main/resources/orders.json
View file @
899a4be3
...
...
@@ -3,7 +3,7 @@
"id"
:
1
,
"productId"
:
1
,
"quantity"
:
50
,
"dueDate"
:
"2025-10-27T16:21:31.0756427+08:00"
,
"priority"
:
1
,
"canSplit"
:
false
,
"canInterrupt"
:
false
...
...
@@ -12,7 +12,7 @@
"id"
:
2
,
"productId"
:
1
,
"quantity"
:
100
,
"dueDate"
:
"2025-10-18T16:21:31.0756427+08:00"
,
"priority"
:
2
,
"canSplit"
:
false
,
"canInterrupt"
:
false
...
...
@@ -21,7 +21,7 @@
"id"
:
3
,
"productId"
:
2
,
"quantity"
:
100
,
"dueDate"
:
"2025-10-31T16:21:31.0756427+08:00"
,
"priority"
:
2
,
"canSplit"
:
false
,
"canInterrupt"
:
false
...
...
src/main/resources/products.json
View file @
899a4be3
...
...
@@ -10,7 +10,7 @@
"machineOptions"
:
[
{
"machineId"
:
2
,
"processingTime"
:
11
,
"processingTime"
:
660
,
"setupTime"
:
0
,
"teardownTime"
:
0
,
"preTime"
:
0
...
...
@@ -25,14 +25,14 @@
"machineOptions"
:
[
{
"machineId"
:
3
,
"processingTime"
:
5
,
"processingTime"
:
600
,
"setupTime"
:
0
,
"teardownTime"
:
0
,
"preTime"
:
0
},
{
"machineId"
:
5
,
"processingTime"
:
21
,
"processingTime"
:
1320
,
"setupTime"
:
0
,
"teardownTime"
:
0
,
"preTime"
:
0
...
...
@@ -47,7 +47,7 @@
"machineOptions"
:
[
{
"machineId"
:
5
,
"processingTime"
:
6
,
"processingTime"
:
360
,
"setupTime"
:
0
,
"teardownTime"
:
0
,
"preTime"
:
0
...
...
@@ -62,14 +62,14 @@
"machineOptions"
:
[
{
"machineId"
:
1
,
"processingTime"
:
1
9
,
"processingTime"
:
1
200
,
"setupTime"
:
0
,
"teardownTime"
:
0
,
"preTime"
:
0
},
{
"machineId"
:
4
,
"processingTime"
:
2
0
,
"processingTime"
:
116
0
,
"setupTime"
:
0
,
"teardownTime"
:
0
,
"preTime"
:
0
...
...
@@ -90,7 +90,7 @@
"machineOptions"
:
[
{
"machineId"
:
1
,
"processingTime"
:
5
,
"processingTime"
:
300
,
"setupTime"
:
0
,
"teardownTime"
:
0
,
"preTime"
:
0
...
...
@@ -105,7 +105,7 @@
"machineOptions"
:
[
{
"machineId"
:
2
,
"processingTime"
:
22
,
"processingTime"
:
1440
,
"setupTime"
:
0
,
"teardownTime"
:
0
,
"preTime"
:
0
...
...
@@ -127,7 +127,7 @@
},
{
"machineId"
:
5
,
"processingTime"
:
28
,
"processingTime"
:
1800
,
"setupTime"
:
0
,
"teardownTime"
:
0
,
"preTime"
:
0
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment