Skip to content

Commit

Permalink
电梯调度模拟1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
clos0710 committed Sep 14, 2023
0 parents commit 47c810c
Show file tree
Hide file tree
Showing 17 changed files with 770 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Elevators1.0</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
2 changes: 2 additions & 0 deletions .settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8
311 changes: 311 additions & 0 deletions README.md

Large diffs are not rendered by default.

Binary file added img/2passengers1elevator.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/2passengers2elevators(CurrentSolution).png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/2passengers2elevators.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/3passengers1elevator.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/Elevator.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
114 changes: 114 additions & 0 deletions src/ACTIONS/DispELev.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package ACTIONS;

import ITEMS.Elevator;
import ITEMS.People;
import TOOLS.PrinLog;

import java.io.IOException;
import java.io.PipedReader;
import java.util.Comparator;
import java.util.LinkedList;

public class DispELev implements Runnable {
private final LinkedList<People> PL;
private final LinkedList<Elevator> EL;
private final PipedReader READER;
private final PrinLog LOG;

public DispELev(LinkedList<People> peopleList, LinkedList<Elevator> elevatorList, PipedReader reader) {
PL = peopleList;
EL = elevatorList;
READER = reader;
LOG = new PrinLog("调度日志",350,400,1050,100);
}

@Override
public void run() {
try {
while (READER.read() != -1) {
chooseElev(PL.getLast());
}
READER.close();
for (int i = 0; i < EL.size(); i++) {
if (EL.get(i).getStatus() != 0) i -= 1;
}
LOG.print("●●●●路径统计●●●●\n");
int totalSteps = 0;
for (Elevator e : EL) {
totalSteps += e.getSteps();
LOG.print("电梯:" + e.getNO() + "号,总路径:" + e.getSteps() + "。\n");
}
LOG.print("总路径:" + totalSteps + ",平均路径:" + totalSteps/EL.size() + "。\n");
} catch (IOException e) {
e.getMessage();
}
}

private void chooseElev(People p) {
int min = 80;
int dist = 80;
Elevator elev = null;

LOG.print("◆◆◆◆决策开始◆◆◆◆\n");

for (Elevator e : EL) {
if (e.getStatus() == 0) {
dist = Math.abs(e.getNowFloor() - p.getNOWFLOOR());
} else if (e.getStatus() == 1) {
if (p.getSTATUS() == 1) {
if (p.getNOWFLOOR() >= e.getNowFloor()) {
dist = Math.abs(e.getNowFloor() - p.getNOWFLOOR());
} else {
dist = Math.abs(e.getHighest() - e.getNowFloor()) + Math.abs(e.getHighest() - e.getLowest()) + Math.abs(e.getLowest() - p.getNOWFLOOR());
}
} else if (p.getSTATUS() == -1) {
if (p.getNOWFLOOR() >= e.getHighest()) {
dist = Math.abs(e.getNowFloor() - p.getNOWFLOOR());
} else {
dist = Math.abs(e.getHighest() - e.getNowFloor()) + Math.abs(e.getHighest() - p.getNOWFLOOR());
}
}
} else {
if (p.getSTATUS() == -1) {
if (p.getNOWFLOOR() <= e.getNowFloor()) {
dist = Math.abs(e.getNowFloor() - p.getNOWFLOOR());
} else {
dist = Math.abs(e.getLowest() - e.getNowFloor()) + Math.abs(e.getHighest() - e.getLowest()) + Math.abs(e.getHighest() - p.getNOWFLOOR());
}
} else if (p.getSTATUS() == 1) {
if (p.getNOWFLOOR() <= e.getLowest()) {
dist = Math.abs(e.getNowFloor() - p.getNOWFLOOR());
} else {
dist = Math.abs(e.getLowest() - e.getNowFloor()) + Math.abs(e.getLowest() - p.getNOWFLOOR());
}
}
}
LOG.print("电梯:" + e.getNO() + "号,当前" + e.getNowFloor() + "层,路径" + dist + "。\n");
if (dist < min) {
min = dist;
elev = e;
}
}

if (p.getSTATUS() == 1) {
if (!elev.getUPREQ().contains(p.getNOWFLOOR())) {
elev.getUPREQ().add(p.getNOWFLOOR());
elev.getUPREQ().sort(Comparator.naturalOrder());
}
} else if (p.getSTATUS() == -1) {
if (!elev.getDOWNREQ().contains(p.getNOWFLOOR())) {
elev.getDOWNREQ().add(p.getNOWFLOOR());
elev.getDOWNREQ().sort(Comparator.reverseOrder());
}
}

if (elev.getStatus() == 0){
if (elev.getNowFloor() < p.getNOWFLOOR()) elev.setStatus(1);
else if (elev.getNowFloor() > p.getNOWFLOOR()) elev.setStatus(-1);
else elev.setStatus(p.getSTATUS());
}

LOG.print(p.getNO()+ "号乘客由" + elev.getNO() + "号电梯服务。\n");
LOG.print("◆◆◆◆决策结束◆◆◆◆\n\n");
}
}
107 changes: 107 additions & 0 deletions src/ACTIONS/RunElev.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package ACTIONS;

import ITEMS.Elevator;
import ITEMS.People;
import TOOLS.PrinLog;

import java.util.Comparator;
import java.util.LinkedList;

public class RunElev implements Runnable{
private final Integer RUNINTERVAL;
private final LinkedList<Elevator> EL;
private final LinkedList<People> PL;
private final PrinLog LOG;

public RunElev(Integer numOfElves, Integer floors, Integer runInterval, LinkedList<People> peopleList){
EL = new LinkedList<>();
for(int i = 0; i < numOfElves; i++) {
this.EL.add(new Elevator(i, floors));
}

RUNINTERVAL = runInterval;
PL = peopleList;

LOG = new PrinLog("运行日志",500, 400, 500, 100);
LOG.print("共" + numOfElves + "台电梯(均在1层,共享上下按钮)。楼高" + floors + "层,每隔" + RUNINTERVAL + "s电梯运行一层。\n\n");
}

@Override
public void run() {
try {
while (true) {
moveElev();
Thread.sleep(RUNINTERVAL * 1000);
}
} catch (InterruptedException e) {
e.getMessage();
}
}

private void moveElev() {
for (Elevator e : EL) {
if (e.getStatus() == 1) {
if (e.getUPREQ().contains(e.getNowFloor())) {
LOG.print("▲ 电梯:" + e.getNO() + "号,在" + e.getNowFloor() + "层停靠。\n");
servePeople(e);
e.getUPREQ().remove(e.getNowFloor());
LOG.print("▲ 电梯:" + e.getNO() + "号,向上:" + e.getUPREQ() + ",向下:" + e.getDOWNREQ() + "。\n\n");
}
if (e.getNowFloor() < e.getHighest()) {
e.incNowFloor();
}
else if (!e.getDOWNREQ().isEmpty()) {
e.setStatus(-1);
}
else {
e.setStatus(0);
LOG.print("■ 电梯:" + e.getNO() + "号,在" + e.getNowFloor() + "层闲置!\n\n");
}
} else if (e.getStatus() == -1) {
if (e.getDOWNREQ().contains(e.getNowFloor())) {
LOG.print("▼ 电梯:" + e.getNO() + "号,在" + e.getNowFloor() + "层停靠。\n");
servePeople(e);
e.getDOWNREQ().remove(e.getNowFloor());
LOG.print("▼ 电梯:" + e.getNO() + "号,向上:" + e.getUPREQ() + ",向下:" + e.getDOWNREQ() + "。\n\n");
}
if (e.getNowFloor() > e.getLowest()) {
e.decNowFloor();
}
else if (!e.getUPREQ().isEmpty()) {
e.setStatus(1);
}
else {
e.setStatus(0);
LOG.print("■ 电梯:" + e.getNO() + "号,在" + e.getNowFloor() + "层闲置!\n\n");
}
}
}
}
public LinkedList<Elevator> getEL() { return EL; }
private void servePeople(Elevator e) {
for (People p : PL) {
if (!p.isServed()) {
if (p.isWaiting() && p.getNOWFLOOR().equals(e.getNowFloor()) && p.getSTATUS().equals(e.getStatus())) {
p.setWaiting(false);
p.setElevNo(e.getNO());
if (p.getSTATUS() == 1) {
if (!e.getUPREQ().contains(p.getAIMFLOOR())) {
e.getUPREQ().add(p.getAIMFLOOR());
e.getUPREQ().sort(Comparator.naturalOrder());
}
} else {
if (!e.getDOWNREQ().contains(p.getAIMFLOOR())) {
e.getDOWNREQ().add(p.getAIMFLOOR());
e.getDOWNREQ().sort(Comparator.reverseOrder());
}
}
LOG.print("→ 乘客:" + p.getNO() + "号,进入" + e.getNO() + "号电梯(目标" + p.getAIMFLOOR() + "层)。\n");
}
if (p.getAIMFLOOR().equals(e.getNowFloor()) && !p.isWaiting() && p.getElevNo().equals(e.getNO())) {
p.setServed(true);
LOG.print("← 乘客:" + p.getNO() + "号,走出" + e.getNO() + "号电梯。\n");
}
}
}
}
}
49 changes: 49 additions & 0 deletions src/ACTIONS/SimuPeop.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package ACTIONS;

import ITEMS.People;
import TOOLS.PrinLog;

import java.io.IOException;
import java.io.PipedWriter;
import java.util.LinkedList;

public class SimuPeop implements Runnable{

private final Integer PEOPLE;
private final Integer MININTERVAL;
private final Integer MAXINTERVAL;
private final Integer FLOORS;
private final LinkedList<People> PL;
private final PipedWriter WRITER;
private final PrinLog LOG;

public SimuPeop(Integer People, Integer MinInterval, Integer MaxInterval, Integer floors, PipedWriter writer) {
PEOPLE = People;
MININTERVAL = MinInterval;
MAXINTERVAL = MaxInterval;
FLOORS = floors;
PL = new LinkedList<>();
WRITER = writer;
LOG = new PrinLog("乘客日志",350, 400, 100, 100);
}

@Override
public void run() {
try {
LOG.print("最少" + MININTERVAL + "s,最大" + MAXINTERVAL + "s到达一名乘客,共" + PEOPLE + "名乘客。\n\n");
for (int i = 0; i < PEOPLE; i++) {
Thread.sleep((long)(Math.random() * (MAXINTERVAL * 1000 - 1000) + MININTERVAL * 1000));
People p = new People(i, FLOORS);
PL.addLast(p);
if (p.getSTATUS() == 1) LOG.print(i + "号乘客:当前" + p.getNOWFLOOR() + "层,上行。\n");
else LOG.print(i + "号乘客:当前" + p.getNOWFLOOR() + "层,下行。\n");
WRITER.write(1);
}
WRITER.close();
} catch (InterruptedException | IOException e) {
e.getMessage();
}
}

public LinkedList<People> getPL() { return PL; }
}
44 changes: 44 additions & 0 deletions src/ITEMS/Elevator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package ITEMS;

import java.util.LinkedList;

public class Elevator {
private final Integer NO;
private final Integer FLOORS;
private Integer nowFloor;
private final LinkedList<Integer> UPREQ;
private final LinkedList<Integer> DOWNREQ;
private int status; //0代表闲置、1代表向上、-1代表向下
private int steps;


public Elevator(Integer no, Integer floors) {
NO = no;
FLOORS = floors;
nowFloor = 1;
UPREQ = new LinkedList<>();
DOWNREQ = new LinkedList<>();
status = 0;
steps = 0;
}

public Integer getNO() { return NO; }
public Integer getNowFloor() { return nowFloor; }
public LinkedList<Integer> getDOWNREQ() { return DOWNREQ; }
public LinkedList<Integer> getUPREQ() { return UPREQ; }
public int getStatus() { return status; }
public int getSteps() { return steps; }

public void setStatus(int status) { this.status = status; }
private void incSteps() { steps++; }
public void decNowFloor() {
if (nowFloor > 1) nowFloor--;
incSteps();
}
public void incNowFloor() {
if (nowFloor < FLOORS) nowFloor++;
incSteps();
}
public Integer getHighest() { return Math.max(UPREQ.isEmpty()?nowFloor:UPREQ.getLast(), DOWNREQ.isEmpty()?nowFloor:DOWNREQ.getFirst()); }
public Integer getLowest() { return Math.min(UPREQ.isEmpty()?nowFloor:UPREQ.getFirst(), DOWNREQ.isEmpty()?nowFloor:DOWNREQ.getLast()); }
}
42 changes: 42 additions & 0 deletions src/ITEMS/People.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package ITEMS;

import java.util.Random;

public class People {
private final Integer NO;
private final Integer NOWFLOOR;
private final Integer AIMFLOOR;
private final Integer STATUS;
private boolean isWaiting = true;
private boolean isServed = false;
private Integer elevNo = -1;

public People(Integer no, Integer ground) {
NO = no;
NOWFLOOR = (int)(Math.random() * ground + 1);

if (NOWFLOOR == 1) STATUS = 1;
else if (NOWFLOOR.equals(ground)) STATUS = -1;
else {
if (new Random().nextBoolean()) STATUS = 1;
else STATUS = -1;
}

if (STATUS == 1) {
AIMFLOOR = (int)(Math.random() * (ground - NOWFLOOR) + NOWFLOOR + 1);
} else {
AIMFLOOR = (int)(Math.random() * (NOWFLOOR - 1) + 1);
}
}

public Integer getNO() { return NO; }
public Integer getNOWFLOOR() { return NOWFLOOR; }
public Integer getAIMFLOOR() { return AIMFLOOR; }
public Integer getSTATUS() { return STATUS; }
public Integer getElevNo() { return elevNo; }
public boolean isWaiting() { return isWaiting; }
public boolean isServed() { return isServed; }
public void setWaiting(boolean waiting) { isWaiting = waiting; }
public void setServed(boolean served) { isServed = served; }
public void setElevNo(Integer no) { elevNo = no; }
}
Loading

0 comments on commit 47c810c

Please sign in to comment.