close

Вход

Забыли?

вход по аккаунту

?

отчет(5)

код для вставкиСкачать
?????????? ??????
??????????? ?????? ????????????? ???????????, ???????????? ? ?????? ?, ??????????????? ???????? ? ?????? B ? C. ??????????? ???????? ??????????, ??????????????? ???????? ??????, ?????????, ???????????? ??????? ? ??? ??? ????????? ????? ? ????????, ??????????????? ????????.
?? ?????? A ????? ?????????? ? ?????. ??????????? ?????? - 1 ??????? ??????, ??????????? ?????? - 3 ???????. ?????????? ????? ?? ???????? A-B-C-B-A, ????? A-C-B-C-A. ??? ???? ???? ??? ????????????? ????? ? ???????? ????? - ????????? ??????????????? ? ?????????????. ????? ? ???? ??? ??????? ???????? ???????????? ???????? (??. ?????).
????? ????????????? ???????? ?????????? ?? ????????? ???????? ??????? ? ???? ???????? ? ????????? ??????????.
???????? ???????
???????
MAS transport{
infrastructure: Centralised
environment: TransportEnv
executionControl: jason.control.ExecutionControl
agents: supplier; manager; loader;
}
import jason.asSyntax.Literal;
import jason.asSyntax.Structure;
import jason.environment.Environment;
public class TransportEnv extends Environment {
TransportManager transportManager = new TransportManager();
private boolean b = true;
// ????? ??? ??? ????????? (????/??? ??????)
Literal ready = Literal.parseLiteral("ready");
Literal nready = Literal.parseLiteral("~ready");
// ?????????????? ????? ? ???? (??? ??????? ????????)
Literal timecar = Literal.parseLiteral("timecar(4)");
Literal timetrain = Literal.parseLiteral("timetrain(3)");
// ???? ??? ???????, ???? ???????
// Literal isProduct = Literal.parseLiteral("isproduct");
// Literal productTo = Literal.parseLiteral("productto(car)");
@Override
public void init(String[] args) {
transportManager.addTransport(new Car("car"));
transportManager.addTransport(new Train("train"));
log();
addPercept(ready);
addPercept("manager", timecar);
addPercept("manager", timetrain);
}
private void step()
{
clearPercepts();
if (b)
{
timecar = Literal.parseLiteral("timecar(" + transportManager.transportByName("car").timeB + ")");
timetrain = Literal.parseLiteral("timetrain(" + transportManager.transportByName("train").timeB + ")");
}
else
{
timecar = Literal.parseLiteral("timecar(" + transportManager.transportByName("car").timeC + ")");
timetrain = Literal.parseLiteral("timetrain(" + transportManager.transportByName("train").timeC + ")");
}
if (transportManager.freeTransport().size() > 0)
{
addPercept(ready);
addPercept("manager", timecar);
addPercept("manager", timetrain);
}
else
{
addPercept(nready);
}
b = !b;
transportManager.step();
}
@Override
public boolean executeAction(String ag, Structure act) {
if (ag.equals("manager"))
{
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (ag.equals("loader"))
{
System.out.println("\nB: " + b);
System.out.println("Agent "+ag+" is doing "+act);
clearPercepts();
String s = "c";
if (b) s = "b";
boolean isPut = false;
for (Transport transport : transportManager.freeTransport()) {
if (transport.getName().equals(act))
{
transport.put(s);
isPut = true;
}
if (!isPut)
{
transportManager.freeTransport().getFirst().put(s);
}
}
step();
log();
}
return true;
}
public void log() {
System.out.println("Free: " + transportManager.freeTransport().size());
for (Transport transport : transportManager.freeTransport()) {
System.out.println(transport.getName() + "; loaded: " + transport.loaded + "; onWay: " + transport.onWay + "; waitTime: " + transport.getWaitTime());
}
System.out.println("All: " + transportManager.allTransport().size());
for (Transport transport : transportManager.allTransport()) {
System.out.println(transport.getName() + "; loaded: " + transport.loaded + "; onWay: " + transport.onWay + "; waitTime: " + transport.getWaitTime());
}
}
}
//package logic;
import java.util.LinkedList;
public class TransportManager {
private LinkedList<Transport> listTransports = new LinkedList<Transport>();
public TransportManager() {
}
public void addTransport(Transport t) {
listTransports.add(t);
}
/**
* ???? ?????????
* @return
*/
public LinkedList<Transport> allTransport() {
return listTransports;
}
/**
* ????????? ????????? ??? ????????
* @return
*/
public LinkedList<Transport> freeTransport() {
LinkedList<Transport> list = new LinkedList<>();
for (Transport transport : listTransports) {
if (!transport.isFull() && !transport.onWay)
{
list.add(transport);
}
}
return list;
}
/**
* ??? ???????
*/
public void step() {
for (Transport transport : listTransports) {
transport.step();
}
}
/**
* ????????? ?? ?????
* @param name
* @return
*/
public Transport transportByName(String name) {
for (Transport transport : listTransports) {
if (transport.getName().equals(name))
{
return transport;
}
}
return null;
}
}
//package logic;
public abstract class Transport {
private String name;
// ? ????
protected boolean onWay;
// ????? ? ????
protected int timeB;
protected int timeC;
// ???????????
protected int capacity;
// ?????????
protected int loaded;
// ????? ?????????? (????? ????????)
private int waitTime;
public Transport(String name) {
this.setName(name);
this.loaded = 0;
this.onWay = false;
this.waitTime = 0;
this.setParameters();
}
protected abstract void setParameters();
/**
* ????????? ??????? ?????
* @return
*/
public boolean put(String target) {
if (!isFull() && !onWay)
{
loaded++;
if (target.equals("b"))
{
if (timeB > waitTime)
{
waitTime = timeB;
}
}
else
{
if (timeC > waitTime)
{
waitTime = timeC;
}
}
return true;
}
else
{
return false;
}
}
/**
* ??? ??????? (????????? ??????? ??????????, ????????)
*/
public void step() {
if (this.onWay)
{
if (waitTime == 0)
{
onWay = false;
return;
}
else
{
waitTime--;
if (waitTime == 0)
{
onWay = false;
}
return;
}
}
else
{
if (isFull())
{
onWay = true;
loaded = 0;
return;
}
}
}
/**
* ???????? ???????? ?? ?????????
* @return
*/
public boolean isFull() {
if (loaded == capacity)
{
return true;
}
else
{
return false;
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getWaitTime() {
return waitTime;
}
public void setWaitTime(int waitTime) {
this.waitTime = waitTime;
}
}
//package logic;
public class Train extends Transport {
public Train(String name) {
super(name);
}
@Override
protected void setParameters() {
this.capacity = 3;
this.timeB = 4;
this.timeC = 2;
}
}
//package logic;
public class Car extends Transport {
public Car(String name) {
super(name);
}
@Override
protected void setParameters() {
this.capacity = 1;
this.timeB = 3;
this.timeC = 6;
}
}
+!car[source(manager)] <-
car.
+!train[source(manager)] <-
train. +!wait[source(manager)] <-
wait. +!ready[source(supplier)] <-
?timecar(X);
?timetrain(Y);
if (X > Y)
{ car; .send(loader,achieve,car); }
else
{ train; .send(loader,achieve,train); }.
+~ready : true
<- wait; .send(loader,achieve,wait);.
+ready : true
<- .send(manager,achieve,ready).
?????
????????????? ??????? ????????? ???????????? ???????? ???????????? ? ???????????? ?????.
1
Документ
Категория
Без категории
Просмотров
19
Размер файла
19 Кб
Теги
отчет
1/--страниц
Пожаловаться на содержимое документа