Commit 800d72ee authored by Vladislav Kiselev's avatar Vladislav Kiselev

Cleanup

parent 84bd0eb0
.idea/*
out/*
Conversion.iml
.idea/dictionaries/Vlad_kv.xml
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package inport;
/**
*
* @author topazh_ag
*/
public class Berth {
private int id;
private String name;
private boolean isRaid;
/**
* Get the value of name
*
* @return the value of name
*/
public String getName() {
return name;
}
/**
* Set the value of name
*
* @param name new value of name
*/
public void setName(String name) {
this.name = name;
}
/**
* Get the value of id
*
* @return the value of id
*/
public int getId() {
return id;
}
/**
* Set the value of id
*
* @param id new value of id
*/
public void setId(int id) {
this.id = id;
}
public boolean getIsRaid() {
return isRaid;
}
public void setIsRaid(boolean isRaid) {
this.isRaid = isRaid;
}
public Berth(int id, String name, boolean isRaid) {
this.id = id;
this.name = name;
this.isRaid = isRaid;
}
public Berth() {
}
@Override
public String toString() {
return id + ";" + name;
}
public Berth(String s) {
String[] tokens = s.split(";");
id = Integer.parseInt(tokens[0].trim());
name = tokens[1].trim();
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package inport;
/**
*
* @author topazh_ag
*/
public class Bunker extends MovingObject {
public Bunker(int id, String name) {
super(id, name);
}
public Bunker() {
super( );
}
@Override
public String toString() {
return getId() + ";" + getName();
}
public Bunker(String s) {
super(s);
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package inport;
/**
*
* @author topazh_ag
*/
public class Cargo {
private double cost;
private int id;
private String name;
/**
* Get the value of cost
*
* @return the value of cost
*/
public double getCost() {
return cost;
}
/**
* Set the value of cost
*
* @param cost new value of cost
*/
public void setCost(double cost) {
this.cost = cost;
}
/**
* Get the value of name
*
* @return the value of name
*/
public String getName() {
return name;
}
/**
* Set the value of name
*
* @param name new value of name
*/
public void setName(String name) {
this.name = name;
}
/**
* Get the value of id
*
* @return the value of id
*/
public int getId() {
return id;
}
/**
* Set the value of id
*
* @param id new value of id
*/
public void setId(int id) {
this.id = id;
}
public Cargo(double cost, int id, String name) {
this.cost = cost;
this.id = id;
this.name = name;
}
public Cargo() {
}
@Override
public String toString() {
return id + ";" + name + ";" + cost;
}
public Cargo(String s) {
String[] tokens = s.split(";");
id = Integer.parseInt(tokens[0].trim());
name = tokens[1].trim();
cost = 0; //Double.parseDouble(tokens[2]);
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package inport;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
*
* @author topazh_ag
*/
public class CargoFlow {
private Storage storage;
private Cargo cargo;
Map<Double, Double> flow;
public Map<Double, Double> getFlow() {
return flow;
}
public void setFlow(Map<Double, Double> flow) {
this.flow = flow;
}
/**
* Get the value of storage
*
* @return the value of storage
*/
public Storage getStorage() {
return storage;
}
/**
* Set the value of storage
*
* @param storage new value of storage
*/
public void setStorage(Storage storage) {
this.storage = storage;
}
public Cargo getCargo() {
return cargo;
}
public void setCargo(Cargo cargo) {
this.cargo = cargo;
}
public CargoFlow() {
this.flow = new HashMap<>();
}
public CargoFlow(Storage storage, Cargo cargo) {
this.storage = storage;
this.cargo = cargo;
this.flow = new HashMap<>();
}
public double getCurrentValue(double forTime)
{
double res = 0.0;
boolean isFound = false;
Set<Double> keyTimes = flow.keySet();
double prevKey = -1.0;
for (Double keyTime : keyTimes)
{
if (forTime>prevKey && forTime<keyTime)
{
res = flow.get(prevKey);
isFound = true;
break;
} else
prevKey = keyTime;
}
if (!isFound && forTime>prevKey)
res = flow.get(prevKey);
return res;
}
public double getTotalValue(double forTime)
{
double res = 0.0;
boolean isFound = false;
Set<Double> keyTimes = flow.keySet();
double prevKey = -1.0;
for (Double keyTime : keyTimes)
{
if (forTime>prevKey && forTime<keyTime)
{
res += flow.get(prevKey)*(forTime-prevKey);
isFound = true;
break;
} else
{
res += flow.get(prevKey)*(keyTime-prevKey);
prevKey = keyTime;
}
}
if (!isFound && forTime>prevKey)
res += flow.get(prevKey)*(forTime-prevKey);
return res;
}
@Override
public String toString() {
String res = "";
boolean first = true;
for(Double s : flow.keySet())
{
if (!first)
res += "," + s+ ":" + flow.get(s);
else
res += s + ":" + flow.get(s);
first = false;
}
return storage.getId() + ";[" + res +"]";
}
public CargoFlow(String s, Map<Integer, Storage> mp, Map<Integer, Cargo> cp) {
this.flow = new HashMap<>();
String[] tokens = s.split(";");
int key = Integer.parseInt(tokens[0].trim());
storage = mp.get(key);
key = Integer.parseInt(tokens[1].trim());
cargo = cp.get(key);
String[] rs = tokens[2].trim().replace("[", "").replace("]", "").split(",");
for (String crs : rs)
if (crs.length()>0)
{
String[] kv = crs.split(":");
Double key1 = Double.parseDouble(kv[0].trim());
Double value1 = Double.parseDouble(kv[1].trim());
flow.put(key1, value1);
}
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package inport;
//import com.google.gson.Gson;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Date;
import org.sat4j.core.Vec;
import org.sat4j.core.VecInt;
import org.sat4j.minisat.SolverFactory;
import org.sat4j.pb.IPBSolver;
import org.sat4j.pb.OPBStringSolver;
import org.sat4j.pb.ObjectiveFunction;
import org.sat4j.pb.reader.OPBEclipseReader2007;
import org.sat4j.reader.DimacsReader;
import org.sat4j.reader.ParseFormatException;
import org.sat4j.reader.Reader;
import org.sat4j.specs.ContradictionException;
import org.sat4j.specs.IProblem;
import org.sat4j.specs.ISolver;
import org.sat4j.specs.IVec;
import org.sat4j.specs.IVecInt;
// import pbsugar.pb.PBParser;
//import java.beans.XMLEncoder;
//import java.io.BufferedOutputStream;
//import java.io.FileNotFoundException;
//import java.io.FileOutputStream;
//import java.io.FileWriter;
//import java.io.IOException;
/**
*
* @author topazh_ag
*/
public class InPort {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
globalTest();
/*
// Создаем новую задачу
TaskCase task = new TaskCase();
// Читаем ее из файла первого аргумента
task.deserialize(args[0]);
// Подготвливаем для солвера и решаем
String fName = "task.opb";
int nOperations = task.pb_formalize(fName);
int[] res = pbSolverDemo(fName);
// Запись во все назначения
int nTimes = (int) (task.getPlanningInterval() + 1.0 - 1e-7);
if (res.length>1)
{
formResult(fName, res, nOperations, nTimes);
task.formSolutionFromPB(res);
task.serialize(args[0]);
}
*/
}
private static void globalTest() throws Exception
{
// Создаем новую задачу
TaskCase task = new TaskCase();
// Заполняем вручную
// fillByHandT(task);
// fillByHandT(task);
// fillByHand2(task);
// fillByHandE0(task);
// Пишем
// task.serialize("Task.txt");
// Читаем обратно
task.deserialize("ippStart.ipp");
// И пишем в другой файл для сравнения
task.serialize("TaskCopy.txt");
// Подготвливаем для солвера
String fName = "task.opb";
// int nOperations = task.pb_formalize(fName);
int nOperations = 17;
/*
pbsugar.PBSugar solver = new pbsugar.PBSugar();
solver.pbFileName = fName;
int variables = solver.encode();
Set<String> pbSolution = solver.solve(variables);
*/
int[] res = pbSolverDemo(fName);
// Запись в
int nTimes = (int) (task.getPlanningInterval() + 1.0 - 1e-7);
if (res.length>1)
{
formResult(fName, res, nOperations, nTimes);
task.formSolutionFromPB(res);
task.serialize("TaskResult.txt");
}
}
private static void formResult(String fileName, int[] res, int nOperations, int nTimes) throws IOException
{
// Решение - в конец файла задания
FileWriter writer = new FileWriter(fileName, true);
// Решение - в отдельный файл
// FileWriter writer = new FileWriter("D:\\KSRC\\GMT_Logistics\\Planning\\results.txt", false);
writer.write("\n");
writer.write("* Results: \n");
writer.write("\n");
int cVar = 0;
do
{
String clause = "Operation "+(cVar+1) + ": ";
int nSpaces = 6 - (int)(Math.log10(cVar+1));
for (int j=0; j<nSpaces; j++)
clause += " ";
for (int j=0; j<nTimes; j++)
{
int ind = cVar*nTimes+j;
if (res[ind]>0)
clause += "X";
else
clause += "-";
}
writer.write(clause+"\n");
cVar++;
} while (cVar<nOperations);
writer.flush();
writer.close();
}
private static void satSolverDemo()
{
ISolver solver = SolverFactory.newDefault();
solver.setTimeout(300);
Reader reader = new DimacsReader(solver);
PrintWriter out = new PrintWriter(System.out,true);
try {
IProblem problem = reader.parseInstance("D:\\KSRC\\Planing\\example.cnf");
if (problem.isSatisfiable()) {
System.out.println("Satisfiable !");
reader.decode(problem.model(),out);
int[] res = problem.model();
int a = 6;
} else {
System.out.println("Unsatisfiable !");
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
} catch (ParseFormatException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
} catch (ContradictionException e) {
System.out.println("Unsatisfiable (trivial)!");
} catch (org.sat4j.specs.TimeoutException ex) {
System.out.println("Timeout, sorry!");
}
}
private static int[] pbSolverDemo(String fileName) throws FileNotFoundException, ParseFormatException, ContradictionException, org.sat4j.specs.TimeoutException {
IPBSolver solver = org.sat4j.pb.SolverFactory.newEclipseP2();
solver.setTimeoutOnConflicts(100000000);
OPBEclipseReader2007 reader = new OPBEclipseReader2007(solver);
// CNF filename is given on the command line
FileReader fr = new FileReader(fileName);
Date start = new Date();
IProblem problem = reader.parseInstance(fr);
boolean rslt = problem.isSatisfiable();
Date finish = new Date();
long mtime = (finish.getTime() - start.getTime());
System.out.println("Solution time (msec) - " + mtime);
if (rslt)
{
System.out.println("Satisfiable !");
int[] res = problem.model();
return res;
}
else
{
System.out.println("Unsatisfiable !");
int[] res = new int[1];
res[0] = 0;
return res;
}
}
public static void pbStringSolverDemo() throws ContradictionException {
IPBSolver solver = new OPBStringSolver();
solver.newVar(3);
IVecInt clause = new VecInt();
clause.push(1).push(2).push(3);
solver.addClause(clause);
IVecInt vars = new VecInt();
vars.push(2).push(3);
IVec coeffs = new Vec();
coeffs.push(BigInteger.TEN).push(BigInteger.valueOf(32));
ObjectiveFunction obj = new ObjectiveFunction(vars, coeffs);
solver.setObjectiveFunction(obj);
String sv = solver.toString();
int a = 6;
}
// Кейс тупой 1
public static void fillByHandE1(TaskCase task)
{
// Статические элементы
// Грузы
Cargo cargo0 = new Cargo(1.0, 0, "LNG");
task.getCargoes().add(cargo0);
// Причалы
Berth berth0 = new Berth(1, "Raid", true);
task.getBerths().add(berth0);
Berth berth1 = new Berth(2, "Pier 1", false);
task.getBerths().add(berth1);
// Хранилища
Storage storage1 = new Storage(3, "Storage 1", 10000.0, cargo0);
task.getStorages().add(storage1);
// Суда
TransportShip ship1 = new TransportShip(4, "Ship 1", 200);
task.getShips().add(ship1);
// Шаблоны операций
// Перемещения
MovingTemplate move101 = new MovingTemplate(ship1,berth0,berth1,1.0,5+task.getTemplates().size());
task.getTemplates().add(move101);
MovingTemplate move110 = new MovingTemplate(ship1,berth1,berth0,1.0,5+task.getTemplates().size());
task.getTemplates().add(move110);
// Погрузки
LoadingTemplate load11 = new LoadingTemplate(ship1,berth1,storage1,100.0,5+task.getTemplates().size());
task.getTemplates().add(load11);
// Начальное состояние
MovingObjectState sini1 = new MovingObjectState(ship1, berth0);
task.getVesselInitialState().add(sini1);
StorageState stini1 = new StorageState(ship1,cargo0, 0.0);
task.getStorageInitialState().add(stini1);
StorageState tini1 = new StorageState(storage1, cargo0, 10000.0);
task.getStorageInitialState().add(tini1);
// Конечное состояние
MovingObjectState send1 = new MovingObjectState(ship1, berth0);
task.getVesselInitialState().add(send1);
StorageState stend1 = new StorageState(ship1,cargo0, 1000.0);
task.getStorageInitialState().add(stend1);
// Горизонт планирования
task.setPlanningInterval(18.0);
}
// Кейс тупой-претупой 0
public static void fillByHandE0(TaskCase task)
{
// Статические элементы
// Грузы
Cargo cargo0 = new Cargo(1.0, 1, "LNG");
task.getCargoes().add(cargo0);
// Причалы
Berth berth0 = new Berth(2, "Raid", true);
task.getBerths().add(berth0);
Berth berth1 = new Berth(3, "Pier 1", false);
task.getBerths().add(berth1);
// Хранилища
Storage storage1 = new Storage(4, "Storage 1", 10000.0, cargo0);
task.getStorages().add(storage1);
// Суда
TransportShip ship1 = new TransportShip(5, "Ship 1", 200);
task.getShips().add(ship1);
// Шаблоны операций
// Перемещения
MovingTemplate move101 = new MovingTemplate(ship1,berth0,berth1,1.0,6+task.getTemplates().size());
task.getTemplates().add(move101);
MovingTemplate move110 = new MovingTemplate(ship1,berth1,berth0,1.0,6+task.getTemplates().size());
task.getTemplates().add(move110);
// Погрузки
LoadingTemplate load11 = new LoadingTemplate(ship1,berth1,storage1,100.0,6+task.getTemplates().size());
task.getTemplates().add(load11);
// Начальное состояние
MovingObjectState sini1 = new MovingObjectState(ship1, berth0);
task.getVesselInitialState().add(sini1);
StorageState stini1 = new StorageState(ship1,cargo0, 0.0);
task.getStorageInitialState().add(stini1);
StorageState tini1 = new StorageState(storage1, cargo0, 1000.0);
task.getStorageInitialState().add(tini1);
// Конечное состояние
MovingObjectState send1 = new MovingObjectState(ship1, berth0);
task.getVesselEndState().add(send1);
StorageState stend1 = new StorageState(ship1, cargo0, 100.0);
task.getStorageEndState().add(stend1);
// Горизонт планирования
task.setPlanningInterval(4.0);
}
/*
// Кейс два судна-причал
public static void fillByHandE2V1B(TaskCase task)
{
// Статические элементы
// Грузы
Cargo cargo0 = new Cargo(1.0, 0, "LNG");
task.getCargoes().add(cargo0);
// Причалы
Berth berth0 = new Berth(0, "Raid", true);
task.getBerths().add(berth0);
Berth berth1 = new Berth(1, "Pier 1", false);
task.getBerths().add(berth1);
// Хранилища
Storage storage1 = new Storage(1, "Storage 1", 10000.0, cargo0);
task.getStorages().add(storage1);
// Суда
TransportShip ship1 = new TransportShip(1, "Ship 1", 300);
task.getShips().add(ship1);
TransportShip ship2 = new TransportShip(2, "Ship 2", 300);
task.getShips().add(ship2);
// Шаблоны операций
// Перемещения
// 1
MovingTemplate move101 = new MovingTemplate(ship1,berth0,berth1,1.0,task.getTemplates().size());
task.getTemplates().add(move101);
// 2
MovingTemplate move110 = new MovingTemplate(ship1,berth1,berth0,1.0,task.getTemplates().size());
task.getTemplates().add(move110);
// 3
MovingTemplate move201 = new MovingTemplate(ship2,berth0,berth1,1.0,task.getTemplates().size());
task.getTemplates().add(move201);
// 4
MovingTemplate move210 = new MovingTemplate(ship2,berth1,berth0,1.0,task.getTemplates().size());
task.getTemplates().add(move210);
// Швартовки
// 5
MooringTemplate mr11 = new MooringTemplate(ship1, berth1, 1.0, true, task.getTemplates().size());
task.getTemplates().add(mr11);
// 6
MooringTemplate mr110 = new MooringTemplate(ship1, berth1, 1.0, false, task.getTemplates().size());
task.getTemplates().add(mr110);
// 7
MooringTemplate mr21 = new MooringTemplate(ship2, berth1, 1.0, true, task.getTemplates().size());
task.getTemplates().add(mr21);
// 8
MooringTemplate mr210 = new MooringTemplate(ship2, berth1, 1.0, false, task.getTemplates().size());
task.getTemplates().add(mr210);
// Погрузки
// 9
LoadingTemplate load11 = new LoadingTemplate(ship1,berth1,storage1,100.0,task.getTemplates().size());
task.getTemplates().add(load11);
// 10
LoadingTemplate load21 = new LoadingTemplate(ship2,berth1,storage1,100.0,task.getTemplates().size());
task.getTemplates().add(load21);
// Начальное состояние
TransportShipState sini1 = new TransportShipState(0.0, ship1, berth0, false); sini1.getCargoState().put(cargo0, 0.0);
task.getVesselInitialState().add(sini1);
TransportShipState sini2 = new TransportShipState(0.0, ship2, berth0, false); sini2.getCargoState().put(cargo0, 0.0);
task.getVesselInitialState().add(sini2);
StorageState tini1 = new StorageState(storage1, 1000.0);
task.getStorageInitialState().add(tini1);
// Конечное состояние
TransportShipState send1 = new TransportShipState(0.0, ship1, berth0, false); send1.getCargoState().put(cargo0, 200.0);
task.getVesselEndState().add(send1);
TransportShipState send2 = new TransportShipState(0.0, ship2, berth0, false); send2.getCargoState().put(cargo0, 200.0);
task.getVesselEndState().add(send2);
// Горизонт планирования
task.setPlanningInterval(12.0);
}
*/
// Кейс имени Топажа с нетривиальным решением (1->7->1->6->1)
public static void fillByHandT(TaskCase task)
{
// Статические элементы
// Грузы
Cargo cargo0 = new Cargo(1.0, 0, "LNG");
task.getCargoes().add(cargo0);
// Причалы
Berth berth0 = new Berth(1, "Raid", true);
task.getBerths().add(berth0);
Berth berth1 = new Berth(2, "Pier 1", false);
task.getBerths().add(berth1);
Berth berth2 = new Berth(3, "Pier 2", false);
task.getBerths().add(berth2);
// Хранилища
Storage storage1 = new Storage(4, "Storage 1", 10000.0, cargo0);
task.getStorages().add(storage1);
// Суда
TransportShip ship1 = new TransportShip(5, "Ship 1", 2000);
task.getShips().add(ship1);
TransportShip ship2 = new TransportShip(6, "Ship 2", 2000);
task.getShips().add(ship2);
// Шаблоны операций
// Перемещения
//1
MovingTemplate move101 = new MovingTemplate(ship1,berth0,berth1,1.0,7+task.getTemplates().size());
task.getTemplates().add(move101);
//2
MovingTemplate move110 = new MovingTemplate(ship1,berth1,berth0,1.0,7+task.getTemplates().size());
task.getTemplates().add(move110);
//3
MovingTemplate move102 = new MovingTemplate(ship1,berth0,berth2,1.0,7+task.getTemplates().size());
task.getTemplates().add(move102);
//4
MovingTemplate move120 = new MovingTemplate(ship1,berth2,berth0,1.0,7+task.getTemplates().size());
task.getTemplates().add(move120);
//5
MovingTemplate move112 = new MovingTemplate(ship1,berth1,berth2,1.0,7+task.getTemplates().size());
task.getTemplates().add(move112);
//6
MovingTemplate move121 = new MovingTemplate(ship1,berth2,berth1,1.0,7+task.getTemplates().size());
task.getTemplates().add(move121);
//7
MovingTemplate move201 = new MovingTemplate(ship2,berth0,berth1,1.0,7+task.getTemplates().size());
task.getTemplates().add(move201);
//8
MovingTemplate move210 = new MovingTemplate(ship2,berth1,berth0,1.0,7+task.getTemplates().size());
task.getTemplates().add(move210);
//9
MovingTemplate move202 = new MovingTemplate(ship2,berth0,berth2,1.0,7+task.getTemplates().size());
task.getTemplates().add(move202);
//10
MovingTemplate move220 = new MovingTemplate(ship2,berth2,berth0,1.0,7+task.getTemplates().size());
task.getTemplates().add(move220);
//11
MovingTemplate move212 = new MovingTemplate(ship2,berth1,berth2,1.0,7+task.getTemplates().size());
task.getTemplates().add(move212);
//12
MovingTemplate move221 = new MovingTemplate(ship2,berth2,berth1,1.0,7+task.getTemplates().size());
task.getTemplates().add(move221);
// Погрузки
//13
LoadingTemplate load11 = new LoadingTemplate(ship1,berth1,storage1,100.0,7+task.getTemplates().size());
task.getTemplates().add(load11);
//14
LoadingTemplate load12 = new LoadingTemplate(ship1,berth2,storage1,50.0,7+task.getTemplates().size());
task.getTemplates().add(load12);
//15
LoadingTemplate load21 = new LoadingTemplate(ship2,berth1,storage1,100.0,7+task.getTemplates().size());
task.getTemplates().add(load21);
//16
LoadingTemplate load22 = new LoadingTemplate(ship2,berth2,storage1,50.0,7+task.getTemplates().size());
task.getTemplates().add(load22);
// Начальное состояние
MovingObjectState sini1 = new MovingObjectState(ship1, berth0);
task.getVesselInitialState().add(sini1);
StorageState stini1 = new StorageState(ship1,cargo0, 0.0);
task.getStorageInitialState().add(stini1);
MovingObjectState sini2 = new MovingObjectState(ship2, berth0);
task.getVesselInitialState().add(sini2);
StorageState stini2 = new StorageState(ship2,cargo0, 0.0);
task.getStorageInitialState().add(stini2);
StorageState tini1 = new StorageState(storage1, cargo0, 10000.0);
task.getStorageInitialState().add(tini1);
// Конечное состояние
MovingObjectState send1 = new MovingObjectState(ship1, berth0);
task.getVesselEndState().add(send1);
StorageState stend1 = new StorageState(ship1,cargo0, 1000.0);
task.getStorageEndState().add(stend1);
MovingObjectState send2 = new MovingObjectState(ship2, berth0);
task.getVesselEndState().add(send2);
StorageState stend2 = new StorageState(ship2, cargo0, 1000.0);
task.getStorageEndState().add(stend2);
// Горизонт планирования
task.setPlanningInterval(24.0);
}
/*
// Кейс имени Топажа с нетривиальным решением (1->7->1->6->1) и окном непогоды
public static void fillByHandTTW(TaskCase task)
{
// Статические элементы
// Грузы
Cargo cargo0 = new Cargo(1.0, 0, "LNG");
task.getCargoes().add(cargo0);
// Причалы
Berth berth0 = new Berth(0, "Raid", true);
task.getBerths().add(berth0);
Berth berth1 = new Berth(1, "Pier 1", false);
task.getBerths().add(berth1);
Berth berth2 = new Berth(2, "Pier 2", false);
task.getBerths().add(berth2);
// Хранилища
Storage storage1 = new Storage(1, "Storage 1", 10000.0, cargo0);
task.getStorages().add(storage1);
// Суда
TransportShip ship1 = new TransportShip(2, "Ship 1", 2000);
task.getShips().add(ship1);
TransportShip ship2 = new TransportShip(3, "Ship 2", 2000);
task.getShips().add(ship2);
// Шаблоны операций
// Перемещения
//1
MovingTemplate move101 = new MovingTemplate(ship1,berth0,berth1,1.0,task.getTemplates().size());
task.getTemplates().add(move101);
//2
MovingTemplate move110 = new MovingTemplate(ship1,berth1,berth0,1.0,task.getTemplates().size());
task.getTemplates().add(move110);
//3
MovingTemplate move102 = new MovingTemplate(ship1,berth0,berth2,1.0,task.getTemplates().size());
task.getTemplates().add(move102);
//4
MovingTemplate move120 = new MovingTemplate(ship1,berth2,berth0,1.0,task.getTemplates().size());
task.getTemplates().add(move120);
//5
MovingTemplate move112 = new MovingTemplate(ship1,berth1,berth2,1.0,task.getTemplates().size());
task.getTemplates().add(move112);
//6
MovingTemplate move121 = new MovingTemplate(ship1,berth2,berth1,1.0,task.getTemplates().size());
task.getTemplates().add(move121);
//7
MovingTemplate move201 = new MovingTemplate(ship2,berth0,berth1,1.0,task.getTemplates().size());
task.getTemplates().add(move201);
//8
MovingTemplate move210 = new MovingTemplate(ship2,berth1,berth0,1.0,task.getTemplates().size());
task.getTemplates().add(move210);
//9
MovingTemplate move202 = new MovingTemplate(ship2,berth0,berth2,1.0,task.getTemplates().size());
task.getTemplates().add(move202);
//10
MovingTemplate move220 = new MovingTemplate(ship2,berth2,berth0,1.0,task.getTemplates().size());
task.getTemplates().add(move220);
//11
MovingTemplate move212 = new MovingTemplate(ship2,berth1,berth2,1.0,task.getTemplates().size());
task.getTemplates().add(move212);
//12
MovingTemplate move221 = new MovingTemplate(ship2,berth2,berth1,1.0,task.getTemplates().size());
task.getTemplates().add(move221);
// Погрузки
//13
LoadingTemplate load11 = new LoadingTemplate(ship1,berth1,storage1,100.0,task.getTemplates().size());
task.getTemplates().add(load11);
//14
LoadingTemplate load12 = new LoadingTemplate(ship1,berth2,storage1,50.0,task.getTemplates().size());
task.getTemplates().add(load12);
//15
LoadingTemplate load21 = new LoadingTemplate(ship2,berth1,storage1,100.0,task.getTemplates().size());
task.getTemplates().add(load21);
//16
LoadingTemplate load22 = new LoadingTemplate(ship2,berth2,storage1,50.0,task.getTemplates().size());
task.getTemplates().add(load22);
TimeWindow tw = new TimeWindow(load11);
tw.getBanWindows().put(2.0,2.0);
task.getTimeWindowConstraints().add(tw);
tw = new TimeWindow(load21);
tw.getBanWindows().put(2.0,2.0);
task.getTimeWindowConstraints().add(tw);
tw = new TimeWindow(load12);
tw.getBanWindows().put(3.0,2.0);
task.getTimeWindowConstraints().add(tw);
tw = new TimeWindow(load22);
tw.getBanWindows().put(3.0,2.0);
task.getTimeWindowConstraints().add(tw);
// Начальное состояние
TransportShipState sini1 = new TransportShipState(0.0, ship1, berth0, false); sini1.getCargoState().put(cargo0, 0.0);
task.getVesselInitialState().add(sini1);
TransportShipState sini2 = new TransportShipState(0.0, ship2, berth0, false); sini2.getCargoState().put(cargo0, 0.0);
task.getVesselInitialState().add(sini2);
StorageState tini1 = new StorageState(storage1, 10000.0);
task.getStorageInitialState().add(tini1);
// Конечное состояние
TransportShipState send1 = new TransportShipState(0.0, ship1, berth0, false); send1.getCargoState().put(cargo0, 1000.0);
task.getVesselEndState().add(send1);
TransportShipState send2 = new TransportShipState(0.0, ship2, berth0, false); send2.getCargoState().put(cargo0, 1000.0);
task.getVesselEndState().add(send2);
// Горизонт планирования
task.setPlanningInterval(24.0);
}
*/
/*
// Первый реальный кейс с не полностью тривиальным решением (ездят с буксиром, швартуются сами)
public static void fillByHand1(TaskCase task)
{
// Статические элементы
// Грузы
Cargo cargo0 = new Cargo(1.0, 0, "LNG");
task.getCargoes().add(cargo0);
Cargo cargo1 = new Cargo(1.0, 1, "Excrement");
task.getCargoes().add(cargo1);
// Причалы
Berth berth0 = new Berth(0, "Raid", true);
task.getBerths().add(berth0);
Berth berth1 = new Berth(1, "Pier 1", false);
task.getBerths().add(berth1);
Berth berth2 = new Berth(2, "Pier 2", false);
task.getBerths().add(berth2);
// Хранилища
Storage storage1 = new Storage(1, "Storage 1", 1000.0, cargo1);
task.getStorages().add(storage1);
Storage storage2 = new Storage(2, "Storage 2", 1000.0, cargo1);
task.getStorages().add(storage2);
// Буксиры
Tow tow1 = new Tow(1, "Tow 1");
task.getTows().add(tow1);
// Бункеровщики
// Суда
TransportShip ship1 = new TransportShip(2, "Ship 1", 2000);
task.getShips().add(ship1);
TransportShip ship2 = new TransportShip(3, "Ship 2", 2000);
task.getShips().add(ship2);
// Шаблоны операций
// Перемещения
// 1
MovingTemplate move101 = new MovingTemplate(ship1,berth0,berth1,8.0,task.getTemplates().size());
task.getTemplates().add(move101);
// 2
MovingTemplate move110 = new MovingTemplate(ship1,berth1,berth0,8.0,task.getTemplates().size());
task.getTemplates().add(move110);
// 3
MovingTemplate move101b = new MovingTemplate(ship1,berth0,berth1,1.0,task.getTemplates().size()); move101b.getResources().add(tow1);
task.getTemplates().add(move101b);
// 4
MovingTemplate move110b = new MovingTemplate(ship1,berth1,berth0,1.0,task.getTemplates().size()); move110b.getResources().add(tow1);
task.getTemplates().add(move110b);
// 5
MovingTemplate move102 = new MovingTemplate(ship1,berth0,berth2,8.0,task.getTemplates().size());
task.getTemplates().add(move102);
// 6
MovingTemplate move120 = new MovingTemplate(ship1,berth2,berth0,8.0,task.getTemplates().size());
task.getTemplates().add(move120);
// 7
MovingTemplate move102b = new MovingTemplate(ship1,berth0,berth2,1.0,task.getTemplates().size()); move102b.getResources().add(tow1);
task.getTemplates().add(move102b);
// 8
MovingTemplate move120b = new MovingTemplate(ship1,berth2,berth0,1.0,task.getTemplates().size()); move120b.getResources().add(tow1);
task.getTemplates().add(move120b);
// 9
MovingTemplate move112b = new MovingTemplate(ship1,berth1,berth2,1.0,task.getTemplates().size()); move112b.getResources().add(tow1);
task.getTemplates().add(move112b);
// 10
MovingTemplate move121b = new MovingTemplate(ship1,berth2,berth1,1.0,task.getTemplates().size()); move121b.getResources().add(tow1);
task.getTemplates().add(move121b);
// 11
MovingTemplate move201 = new MovingTemplate(ship2,berth0,berth1,8.0,task.getTemplates().size());
task.getTemplates().add(move201);
// 12
MovingTemplate move210 = new MovingTemplate(ship2,berth1,berth0,8.0,task.getTemplates().size());
task.getTemplates().add(move210);
// 13
MovingTemplate move201b = new MovingTemplate(ship2,berth0,berth1,1.0,task.getTemplates().size()); move201b.getResources().add(tow1);
task.getTemplates().add(move201b);
// 14
MovingTemplate move210b = new MovingTemplate(ship2,berth1,berth0,1.0,task.getTemplates().size()); move210b.getResources().add(tow1);
task.getTemplates().add(move210b);
// 15
MovingTemplate move202 = new MovingTemplate(ship2,berth0,berth2,8.0,task.getTemplates().size());
task.getTemplates().add(move202);
// 16
MovingTemplate move220 = new MovingTemplate(ship2,berth2,berth0,8.0,task.getTemplates().size());
task.getTemplates().add(move220);
// 17
MovingTemplate move202b = new MovingTemplate(ship2,berth0,berth2,1.0,task.getTemplates().size()); move202b.getResources().add(tow1);
task.getTemplates().add(move202b);
// 18
MovingTemplate move220b = new MovingTemplate(ship2,berth2,berth0,1.0,task.getTemplates().size()); move220b.getResources().add(tow1);
task.getTemplates().add(move220b);
// 19
MovingTemplate move212b = new MovingTemplate(ship2,berth1,berth2,1.0,task.getTemplates().size()); move212b.getResources().add(tow1);
task.getTemplates().add(move212b);
// 20
MovingTemplate move221b = new MovingTemplate(ship2,berth2,berth1,1.0,task.getTemplates().size()); move221b.getResources().add(tow1);
task.getTemplates().add(move221b);
// 21
MovingTemplate moveb01 = new MovingTemplate(tow1,berth0,berth1,1.0,task.getTemplates().size());
task.getTemplates().add(moveb01);
// 22
MovingTemplate moveb10 = new MovingTemplate(tow1,berth1,berth0,1.0,task.getTemplates().size());
task.getTemplates().add(moveb10);
// 23
MovingTemplate moveb02 = new MovingTemplate(tow1,berth0,berth2,1.0,task.getTemplates().size());
task.getTemplates().add(moveb02);
// 24
MovingTemplate moveb20 = new MovingTemplate(tow1,berth2,berth0,1.0,task.getTemplates().size());
task.getTemplates().add(moveb20);
// 25
MovingTemplate moveb12b = new MovingTemplate(tow1,berth1,berth2,1.0,task.getTemplates().size());
task.getTemplates().add(moveb12b);
// 26
MovingTemplate moveb21b = new MovingTemplate(tow1,berth2,berth1,1.0,task.getTemplates().size());
task.getTemplates().add(moveb21b);
// Швартовки
// 27
MooringTemplate moore11p = new MooringTemplate(ship1,berth1,1.0,true,task.getTemplates().size());
task.getTemplates().add(moore11p);
// 28
MooringTemplate moore11m = new MooringTemplate(ship1,berth1,1.0,false,task.getTemplates().size());
task.getTemplates().add(moore11m);
// 29
MooringTemplate moore12p = new MooringTemplate(ship1,berth2,1.0,true,task.getTemplates().size());
task.getTemplates().add(moore12p);
// 30
MooringTemplate moore12m = new MooringTemplate(ship1,berth2,1.0,false,task.getTemplates().size());
task.getTemplates().add(moore12m);
// 31
MooringTemplate moore21p = new MooringTemplate(ship2,berth1,1.0,true,task.getTemplates().size());
task.getTemplates().add(moore21p);
// 32
MooringTemplate moore21m = new MooringTemplate(ship2,berth1,1.0,false,task.getTemplates().size());
task.getTemplates().add(moore21m);
// 33
MooringTemplate moore22p = new MooringTemplate(ship2,berth2,1.0,true,task.getTemplates().size());
task.getTemplates().add(moore22p);
// 34
MooringTemplate moore22m = new MooringTemplate(ship2,berth2,1.0,false,task.getTemplates().size());
task.getTemplates().add(moore22m);
// Погрузки
// 35
LoadingTemplate load11p = new LoadingTemplate(ship1,berth1,storage1,200.0,task.getTemplates().size());
task.getTemplates().add(load11p);
// 36
LoadingTemplate load11m = new LoadingTemplate(ship1,berth1,storage1,-200.0,task.getTemplates().size());
task.getTemplates().add(load11m);
// 37
LoadingTemplate load12p = new LoadingTemplate(ship1,berth2,storage2,200.0,task.getTemplates().size());
task.getTemplates().add(load12p);
// 38
LoadingTemplate load12m = new LoadingTemplate(ship1,berth2,storage2,-200.0,task.getTemplates().size());
task.getTemplates().add(load12m);
// 39
LoadingTemplate load21p = new LoadingTemplate(ship2,berth1,storage1,200.0,task.getTemplates().size());
task.getTemplates().add(load21p);
// 40
LoadingTemplate load21m = new LoadingTemplate(ship2,berth1,storage1,-200.0,task.getTemplates().size());
task.getTemplates().add(load21m);
// 41
LoadingTemplate load22p = new LoadingTemplate(ship2,berth2,storage2,200.0,task.getTemplates().size());
task.getTemplates().add(load22p);
// 42
LoadingTemplate load22m = new LoadingTemplate(ship2,berth2,storage2,-200.0,task.getTemplates().size());
task.getTemplates().add(load22m);
// Начальное состояние
TransportShipState sini1 = new TransportShipState(0.0, ship1, berth0, false); sini1.getCargoState().put(cargo1, 1000.0);
task.getVesselInitialState().add(sini1);
TransportShipState sini2 = new TransportShipState(0.0, ship2, berth0, false); sini2.getCargoState().put(cargo1, 0.0);
task.getVesselInitialState().add(sini2);
MovingObjectState sini3 = new MovingObjectState(tow1,berth0);
task.getVesselInitialState().add(sini3);
StorageState tini1 = new StorageState(storage1, 200.0);
task.getStorageInitialState().add(tini1);
StorageState tini2 = new StorageState(storage2, 800.0);
task.getStorageInitialState().add(tini2);
// Конечное состояние
TransportShipState send1 = new TransportShipState(0.0, ship1, berth0, false); send1.getCargoState().put(cargo1, 0.0);
task.getVesselEndState().add(send1);
TransportShipState send2 = new TransportShipState(0.0, ship2, berth0, false); send2.getCargoState().put(cargo1, 1000.0);
task.getVesselEndState().add(send2);
// Горизонт планирования
task.setPlanningInterval(18.0);
}
*/
/*
// Второй реальный кейс с не полностью тривиальным решением (ездят с буксиром, швартуются с буксиром)
public static void fillByHand1a(TaskCase task)
{
// Статические элементы
// Грузы
Cargo cargo0 = new Cargo(1.0, 0, "LNG");
task.getCargoes().add(cargo0);
Cargo cargo1 = new Cargo(1.0, 1, "Excrement");
task.getCargoes().add(cargo1);
// Причалы
Berth berth0 = new Berth(0, "Raid", true);
task.getBerths().add(berth0);
Berth berth1 = new Berth(1, "Pier 1", false);
task.getBerths().add(berth1);
Berth berth2 = new Berth(2, "Pier 2", false);
task.getBerths().add(berth2);
// Хранилища
Storage storage1 = new Storage(1, "Storage 1", 1000.0, cargo1);
task.getStorages().add(storage1);
Storage storage2 = new Storage(2, "Storage 2", 1000.0, cargo1);
task.getStorages().add(storage2);
// Буксиры
Tow tow1 = new Tow(1, "Tow 1");
task.getTows().add(tow1);
// Бункеровщики
// Суда
TransportShip ship1 = new TransportShip(2, "Ship 1", 2000);
task.getShips().add(ship1);
TransportShip ship2 = new TransportShip(3, "Ship 2", 2000);
task.getShips().add(ship2);
// Шаблоны операций
// Перемещения
// 1
MovingTemplate move101 = new MovingTemplate(ship1,berth0,berth1,8.0,task.getTemplates().size());
task.getTemplates().add(move101);
// 2
MovingTemplate move110 = new MovingTemplate(ship1,berth1,berth0,8.0,task.getTemplates().size());
task.getTemplates().add(move110);
// 3
MovingTemplate move101b = new MovingTemplate(ship1,berth0,berth1,1.0,task.getTemplates().size()); move101b.getResources().add(tow1);
task.getTemplates().add(move101b);
// 4
MovingTemplate move110b = new MovingTemplate(ship1,berth1,berth0,1.0,task.getTemplates().size()); move110b.getResources().add(tow1);
task.getTemplates().add(move110b);
// 5
MovingTemplate move102 = new MovingTemplate(ship1,berth0,berth2,8.0,task.getTemplates().size());
task.getTemplates().add(move102);
// 6
MovingTemplate move120 = new MovingTemplate(ship1,berth2,berth0,8.0,task.getTemplates().size());
task.getTemplates().add(move120);
// 7
MovingTemplate move102b = new MovingTemplate(ship1,berth0,berth2,1.0,task.getTemplates().size()); move102b.getResources().add(tow1);
task.getTemplates().add(move102b);
// 8
MovingTemplate move120b = new MovingTemplate(ship1,berth2,berth0,1.0,task.getTemplates().size()); move120b.getResources().add(tow1);
task.getTemplates().add(move120b);
// 9
MovingTemplate move112b = new MovingTemplate(ship1,berth1,berth2,1.0,task.getTemplates().size()); move112b.getResources().add(tow1);
task.getTemplates().add(move112b);
// 10
MovingTemplate move121b = new MovingTemplate(ship1,berth2,berth1,1.0,task.getTemplates().size()); move121b.getResources().add(tow1);
task.getTemplates().add(move121b);
// 11
MovingTemplate move201 = new MovingTemplate(ship2,berth0,berth1,8.0,task.getTemplates().size());
task.getTemplates().add(move201);
// 12
MovingTemplate move210 = new MovingTemplate(ship2,berth1,berth0,8.0,task.getTemplates().size());
task.getTemplates().add(move210);
// 13
MovingTemplate move201b = new MovingTemplate(ship2,berth0,berth1,1.0,task.getTemplates().size()); move201b.getResources().add(tow1);
task.getTemplates().add(move201b);
// 14
MovingTemplate move210b = new MovingTemplate(ship2,berth1,berth0,1.0,task.getTemplates().size()); move210b.getResources().add(tow1);
task.getTemplates().add(move210b);
// 15
MovingTemplate move202 = new MovingTemplate(ship2,berth0,berth2,8.0,task.getTemplates().size());
task.getTemplates().add(move202);
// 16
MovingTemplate move220 = new MovingTemplate(ship2,berth2,berth0,8.0,task.getTemplates().size());
task.getTemplates().add(move220);
// 17
MovingTemplate move202b = new MovingTemplate(ship2,berth0,berth2,1.0,task.getTemplates().size()); move202b.getResources().add(tow1);
task.getTemplates().add(move202b);
// 18
MovingTemplate move220b = new MovingTemplate(ship2,berth2,berth0,1.0,task.getTemplates().size()); move220b.getResources().add(tow1);
task.getTemplates().add(move220b);
// 19
MovingTemplate move212b = new MovingTemplate(ship2,berth1,berth2,1.0,task.getTemplates().size()); move212b.getResources().add(tow1);
task.getTemplates().add(move212b);
// 20
MovingTemplate move221b = new MovingTemplate(ship2,berth2,berth1,1.0,task.getTemplates().size()); move221b.getResources().add(tow1);
task.getTemplates().add(move221b);
// 21
MovingTemplate moveb01 = new MovingTemplate(tow1,berth0,berth1,1.0,task.getTemplates().size());
task.getTemplates().add(moveb01);
// 22
MovingTemplate moveb10 = new MovingTemplate(tow1,berth1,berth0,1.0,task.getTemplates().size());
task.getTemplates().add(moveb10);
// 23
MovingTemplate moveb02 = new MovingTemplate(tow1,berth0,berth2,1.0,task.getTemplates().size());
task.getTemplates().add(moveb02);
// 24
MovingTemplate moveb20 = new MovingTemplate(tow1,berth2,berth0,1.0,task.getTemplates().size());
task.getTemplates().add(moveb20);
// 25
MovingTemplate moveb12b = new MovingTemplate(tow1,berth1,berth2,1.0,task.getTemplates().size());
task.getTemplates().add(moveb12b);
// 26
MovingTemplate moveb21b = new MovingTemplate(tow1,berth2,berth1,1.0,task.getTemplates().size());
task.getTemplates().add(moveb21b);
// Швартовки
// 27
MooringTemplate moore11p = new MooringTemplate(ship1,berth1,1.0,true,task.getTemplates().size()); moore11p.getResources().add(tow1);
task.getTemplates().add(moore11p);
// 28
MooringTemplate moore11m = new MooringTemplate(ship1,berth1,1.0,false,task.getTemplates().size()); moore11m.getResources().add(tow1);
task.getTemplates().add(moore11m);
// 29
MooringTemplate moore12p = new MooringTemplate(ship1,berth2,1.0,true,task.getTemplates().size()); moore12p.getResources().add(tow1);
task.getTemplates().add(moore12p);
// 30
MooringTemplate moore12m = new MooringTemplate(ship1,berth2,1.0,false,task.getTemplates().size()); moore12m.getResources().add(tow1);
task.getTemplates().add(moore12m);
// 31
MooringTemplate moore21p = new MooringTemplate(ship2,berth1,1.0,true,task.getTemplates().size()); moore21p.getResources().add(tow1);
task.getTemplates().add(moore21p);
// 32
MooringTemplate moore21m = new MooringTemplate(ship2,berth1,1.0,false,task.getTemplates().size()); moore21m.getResources().add(tow1);
task.getTemplates().add(moore21m);
// 33
MooringTemplate moore22p = new MooringTemplate(ship2,berth2,1.0,true,task.getTemplates().size()); moore22p.getResources().add(tow1);
task.getTemplates().add(moore22p);
// 34
MooringTemplate moore22m = new MooringTemplate(ship2,berth2,1.0,false,task.getTemplates().size()); moore22m.getResources().add(tow1);
task.getTemplates().add(moore22m);
// Погрузки
// 35
LoadingTemplate load11p = new LoadingTemplate(ship1,berth1,storage1,200.0,task.getTemplates().size());
task.getTemplates().add(load11p);
// 36
LoadingTemplate load11m = new LoadingTemplate(ship1,berth1,storage1,-200.0,task.getTemplates().size());
task.getTemplates().add(load11m);
// 37
LoadingTemplate load12p = new LoadingTemplate(ship1,berth2,storage2,200.0,task.getTemplates().size());
task.getTemplates().add(load12p);
// 38
LoadingTemplate load12m = new LoadingTemplate(ship1,berth2,storage2,-200.0,task.getTemplates().size());
task.getTemplates().add(load12m);
// 39
LoadingTemplate load21p = new LoadingTemplate(ship2,berth1,storage1,200.0,task.getTemplates().size());
task.getTemplates().add(load21p);
// 40
LoadingTemplate load21m = new LoadingTemplate(ship2,berth1,storage1,-200.0,task.getTemplates().size());
task.getTemplates().add(load21m);
// 41
LoadingTemplate load22p = new LoadingTemplate(ship2,berth2,storage2,200.0,task.getTemplates().size());
task.getTemplates().add(load22p);
// 42
LoadingTemplate load22m = new LoadingTemplate(ship2,berth2,storage2,-200.0,task.getTemplates().size());
task.getTemplates().add(load22m);
// Начальное состояние
TransportShipState sini1 = new TransportShipState(0.0, ship1, berth0, false); sini1.getCargoState().put(cargo1, 1000.0);
task.getVesselInitialState().add(sini1);
TransportShipState sini2 = new TransportShipState(0.0, ship2, berth0, false); sini2.getCargoState().put(cargo1, 0.0);
task.getVesselInitialState().add(sini2);
MovingObjectState sini3 = new MovingObjectState(tow1,berth0);
task.getVesselInitialState().add(sini3);
StorageState tini1 = new StorageState(storage1, 200.0);
task.getStorageInitialState().add(tini1);
StorageState tini2 = new StorageState(storage2, 800.0);
task.getStorageInitialState().add(tini2);
// Конечное состояние
TransportShipState send1 = new TransportShipState(0.0, ship1, berth0, false); send1.getCargoState().put(cargo1, 0.0);
task.getVesselEndState().add(send1);
TransportShipState send2 = new TransportShipState(0.0, ship2, berth0, false); send2.getCargoState().put(cargo1, 1000.0);
task.getVesselEndState().add(send2);
// Горизонт планирования
task.setPlanningInterval(20.0);
}
*/
/*
// Третий реальный кейс с не полностью тривиальным решением (ездят сами, швартуются с буксиром)
public static void fillByHand1b(TaskCase task)
{
// Статические элементы
// Грузы
Cargo cargo0 = new Cargo(1.0, 0, "LNG");
task.getCargoes().add(cargo0);
Cargo cargo1 = new Cargo(1.0, 1, "Excrement");
task.getCargoes().add(cargo1);
// Причалы
Berth berth0 = new Berth(0, "Raid", true);
task.getBerths().add(berth0);
Berth berth1 = new Berth(1, "Pier 1", false);
task.getBerths().add(berth1);
Berth berth2 = new Berth(2, "Pier 2", false);
task.getBerths().add(berth2);
// Хранилища
Storage storage1 = new Storage(1, "Storage 1", 1000.0, cargo1);
task.getStorages().add(storage1);
Storage storage2 = new Storage(2, "Storage 2", 1000.0, cargo1);
task.getStorages().add(storage2);
// Буксиры
Tow tow1 = new Tow(1, "Tow 1");
task.getTows().add(tow1);
// Бункеровщики
// Суда
TransportShip ship1 = new TransportShip(2, "Ship 1", 2000);
task.getShips().add(ship1);
TransportShip ship2 = new TransportShip(3, "Ship 2", 2000);
task.getShips().add(ship2);
// Шаблоны операций
// Перемещения
// 1
MovingTemplate move101 = new MovingTemplate(ship1,berth0,berth1,1.0,task.getTemplates().size());
task.getTemplates().add(move101);
// 2
MovingTemplate move110 = new MovingTemplate(ship1,berth1,berth0,1.0,task.getTemplates().size());
task.getTemplates().add(move110);
// 3
MovingTemplate move101b = new MovingTemplate(ship1,berth0,berth1,8.0,task.getTemplates().size()); move101b.getResources().add(tow1);
task.getTemplates().add(move101b);
// 4
MovingTemplate move110b = new MovingTemplate(ship1,berth1,berth0,8.0,task.getTemplates().size()); move110b.getResources().add(tow1);
task.getTemplates().add(move110b);
// 5
MovingTemplate move102 = new MovingTemplate(ship1,berth0,berth2,1.0,task.getTemplates().size());
task.getTemplates().add(move102);
// 6
MovingTemplate move120 = new MovingTemplate(ship1,berth2,berth0,1.0,task.getTemplates().size());
task.getTemplates().add(move120);
// 7
MovingTemplate move102b = new MovingTemplate(ship1,berth0,berth2,8.0,task.getTemplates().size()); move102b.getResources().add(tow1);
task.getTemplates().add(move102b);
// 8
MovingTemplate move120b = new MovingTemplate(ship1,berth2,berth0,8.0,task.getTemplates().size()); move120b.getResources().add(tow1);
task.getTemplates().add(move120b);
// 9
MovingTemplate move112b = new MovingTemplate(ship1,berth1,berth2,8.0,task.getTemplates().size()); move112b.getResources().add(tow1);
task.getTemplates().add(move112b);
// 10
MovingTemplate move121b = new MovingTemplate(ship1,berth2,berth1,8.0,task.getTemplates().size()); move121b.getResources().add(tow1);
task.getTemplates().add(move121b);
// 11
MovingTemplate move201 = new MovingTemplate(ship2,berth0,berth1,1.0,task.getTemplates().size());
task.getTemplates().add(move201);
// 12
MovingTemplate move210 = new MovingTemplate(ship2,berth1,berth0,1.0,task.getTemplates().size());
task.getTemplates().add(move210);
// 13
MovingTemplate move201b = new MovingTemplate(ship2,berth0,berth1,8.0,task.getTemplates().size()); move201b.getResources().add(tow1);
task.getTemplates().add(move201b);
// 14
MovingTemplate move210b = new MovingTemplate(ship2,berth1,berth0,8.0,task.getTemplates().size()); move210b.getResources().add(tow1);
task.getTemplates().add(move210b);
// 15
MovingTemplate move202 = new MovingTemplate(ship2,berth0,berth2,1.0,task.getTemplates().size());
task.getTemplates().add(move202);
// 16
MovingTemplate move220 = new MovingTemplate(ship2,berth2,berth0,1.0,task.getTemplates().size());
task.getTemplates().add(move220);
// 17
MovingTemplate move202b = new MovingTemplate(ship2,berth0,berth2,8.0,task.getTemplates().size()); move202b.getResources().add(tow1);
task.getTemplates().add(move202b);
// 18
MovingTemplate move220b = new MovingTemplate(ship2,berth2,berth0,8.0,task.getTemplates().size()); move220b.getResources().add(tow1);
task.getTemplates().add(move220b);
// 19
MovingTemplate move212b = new MovingTemplate(ship2,berth1,berth2,8.0,task.getTemplates().size()); move212b.getResources().add(tow1);
task.getTemplates().add(move212b);
// 20
MovingTemplate move221b = new MovingTemplate(ship2,berth2,berth1,8.0,task.getTemplates().size()); move221b.getResources().add(tow1);
task.getTemplates().add(move221b);
// 21
MovingTemplate moveb01 = new MovingTemplate(tow1,berth0,berth1,1.0,task.getTemplates().size());
task.getTemplates().add(moveb01);
// 22
MovingTemplate moveb10 = new MovingTemplate(tow1,berth1,berth0,1.0,task.getTemplates().size());
task.getTemplates().add(moveb10);
// 23
MovingTemplate moveb02 = new MovingTemplate(tow1,berth0,berth2,1.0,task.getTemplates().size());
task.getTemplates().add(moveb02);
// 24
MovingTemplate moveb20 = new MovingTemplate(tow1,berth2,berth0,1.0,task.getTemplates().size());
task.getTemplates().add(moveb20);
// 25
MovingTemplate moveb12b = new MovingTemplate(tow1,berth1,berth2,1.0,task.getTemplates().size());
task.getTemplates().add(moveb12b);
// 26
MovingTemplate moveb21b = new MovingTemplate(tow1,berth2,berth1,1.0,task.getTemplates().size());
task.getTemplates().add(moveb21b);
// Швартовки
// 27
MooringTemplate moore11p = new MooringTemplate(ship1,berth1,1.0,true,task.getTemplates().size()); moore11p.getResources().add(tow1);
task.getTemplates().add(moore11p);
// 28
MooringTemplate moore11m = new MooringTemplate(ship1,berth1,1.0,false,task.getTemplates().size()); moore11m.getResources().add(tow1);
task.getTemplates().add(moore11m);
// 29
MooringTemplate moore12p = new MooringTemplate(ship1,berth2,1.0,true,task.getTemplates().size()); moore12p.getResources().add(tow1);
task.getTemplates().add(moore12p);
// 30
MooringTemplate moore12m = new MooringTemplate(ship1,berth2,1.0,false,task.getTemplates().size()); moore12m.getResources().add(tow1);
task.getTemplates().add(moore12m);
// 31
MooringTemplate moore21p = new MooringTemplate(ship2,berth1,1.0,true,task.getTemplates().size()); moore21p.getResources().add(tow1);
task.getTemplates().add(moore21p);
// 32
MooringTemplate moore21m = new MooringTemplate(ship2,berth1,1.0,false,task.getTemplates().size()); moore21m.getResources().add(tow1);
task.getTemplates().add(moore21m);
// 33
MooringTemplate moore22p = new MooringTemplate(ship2,berth2,1.0,true,task.getTemplates().size()); moore22p.getResources().add(tow1);
task.getTemplates().add(moore22p);
// 34
MooringTemplate moore22m = new MooringTemplate(ship2,berth2,1.0,false,task.getTemplates().size()); moore22m.getResources().add(tow1);
task.getTemplates().add(moore22m);
// Погрузки
// 35
LoadingTemplate load11p = new LoadingTemplate(ship1,berth1,storage1,200.0,task.getTemplates().size());
task.getTemplates().add(load11p);
// 36
LoadingTemplate load11m = new LoadingTemplate(ship1,berth1,storage1,-200.0,task.getTemplates().size());
task.getTemplates().add(load11m);
// 37
LoadingTemplate load12p = new LoadingTemplate(ship1,berth2,storage2,200.0,task.getTemplates().size());
task.getTemplates().add(load12p);
// 38
LoadingTemplate load12m = new LoadingTemplate(ship1,berth2,storage2,-200.0,task.getTemplates().size());
task.getTemplates().add(load12m);
// 39
LoadingTemplate load21p = new LoadingTemplate(ship2,berth1,storage1,200.0,task.getTemplates().size());
task.getTemplates().add(load21p);
// 40
LoadingTemplate load21m = new LoadingTemplate(ship2,berth1,storage1,-200.0,task.getTemplates().size());
task.getTemplates().add(load21m);
// 41
LoadingTemplate load22p = new LoadingTemplate(ship2,berth2,storage2,200.0,task.getTemplates().size());
task.getTemplates().add(load22p);
// 42
LoadingTemplate load22m = new LoadingTemplate(ship2,berth2,storage2,-200.0,task.getTemplates().size());
task.getTemplates().add(load22m);
// Начальное состояние
TransportShipState sini1 = new TransportShipState(0.0, ship1, berth0, false); sini1.getCargoState().put(cargo1, 1000.0);
task.getVesselInitialState().add(sini1);
TransportShipState sini2 = new TransportShipState(0.0, ship2, berth0, false); sini2.getCargoState().put(cargo1, 0.0);
task.getVesselInitialState().add(sini2);
MovingObjectState sini3 = new MovingObjectState(tow1,berth0);
task.getVesselInitialState().add(sini3);
StorageState tini1 = new StorageState(storage1, 200.0);
task.getStorageInitialState().add(tini1);
StorageState tini2 = new StorageState(storage2, 800.0);
task.getStorageInitialState().add(tini2);
// Конечное состояние
TransportShipState send1 = new TransportShipState(0.0, ship1, berth0, false); send1.getCargoState().put(cargo1, 0.0);
task.getVesselEndState().add(send1);
TransportShipState send2 = new TransportShipState(0.0, ship2, berth0, false); send2.getCargoState().put(cargo1, 1000.0);
task.getVesselEndState().add(send2);
// Горизонт планирования
task.setPlanningInterval(18.0);
}
*/
/*
// Тестовый вариант на заполнение всех полей
public static void fillByHand2(TaskCase task)
{
// Статические элементы
// Грузы
Cargo cargo0 = new Cargo(1.0, 0, "LNG");
task.getCargoes().add(cargo0);
Cargo cargo1 = new Cargo(1.0, 1, "Excrement");
task.getCargoes().add(cargo1);
Cargo cargo2 = new Cargo(1.0, 2, "Peaches");
task.getCargoes().add(cargo2);
// Причалы
Berth berth0 = new Berth(0, "Raid", true);
task.getBerths().add(berth0);
Berth berth1 = new Berth(1, "Pier 1", false);
task.getBerths().add(berth1);
Berth berth2 = new Berth(2, "Pier 2", false);
task.getBerths().add(berth2);
// Хранилища
Storage storage1 = new Storage(1, "Storage 1", 1000.0, cargo1);
task.getStorages().add(storage1);
Storage storage2 = new Storage(2, "Storage 2", 1000.0, cargo2);
task.getStorages().add(storage2);
Storage storage3 = new Storage(3, "Storage 3", 1000.0, cargo0);
task.getStorages().add(storage3);
// Оборудование
LoadingEquipment eq1 = new LoadingEquipment(1, "PeachPump 1");
task.getEquipments().add(eq1);
LoadingEquipment eq2 = new LoadingEquipment(2, "PeachPump 2");
task.getEquipments().add(eq2);
// Буксиры
Tow tow1 = new Tow(1, "Tow 1");
task.getTows().add(tow1);
Tow tow2 = new Tow(2, "Tow 2");
task.getTows().add(tow2);
// Бункеровщики
Bunker bunker1 = new Bunker(3, "Bunker 1");
task.getBunkers().add(bunker1);
// Суда
TransportShip ship1 = new TransportShip(4, "Ship 1", 2000);
task.getShips().add(ship1);
TransportShip ship2 = new TransportShip(5, "Ship 2", 2000);
task.getShips().add(ship2);
// Шаблоны операций
// Перемещения
MovingTemplate move101 = new MovingTemplate(ship1,berth0,berth1,5.0,task.getTemplates().size());
task.getTemplates().add(move101);
MovingTemplate move201b1 = new MovingTemplate(ship2,berth0,berth2,2.0,task.getTemplates().size()); move201b1.getResources().add(tow1);
task.getTemplates().add(move201b1);
MovingTemplate move201b12 = new MovingTemplate(ship2,berth2,berth1,2.0,task.getTemplates().size()); move201b12.getResources().add(tow1); move201b12.getResources().add(tow2);
task.getTemplates().add(move201b12);
MovingTemplate moveb01 = new MovingTemplate(tow1,berth0,berth1,1.0,task.getTemplates().size());
task.getTemplates().add(moveb01);
MovingTemplate movebk1 = new MovingTemplate(bunker1,berth0,berth2,1.0,task.getTemplates().size());
task.getTemplates().add(movebk1);
// Швартовки
MooringTemplate moore11p = new MooringTemplate(ship1,berth1,0.2,true,task.getTemplates().size());
task.getTemplates().add(moore11p);
MooringTemplate moore11p1b = new MooringTemplate(ship1,berth1,0.2,true,task.getTemplates().size()); moore11p1b.getResources().add(tow2);
task.getTemplates().add(moore11p1b);
MooringTemplate moore21p = new MooringTemplate(ship2,berth2,0.5,true,task.getTemplates().size());
task.getTemplates().add(moore21p);
MooringTemplate moore21p2b = new MooringTemplate(ship2,berth2,0.5,true,task.getTemplates().size()); moore21p2b.getResources().add(tow1); moore21p2b.getResources().add(tow2);
task.getTemplates().add(moore21p2b);
// Погрузки
LoadingTemplate load11p = new LoadingTemplate(ship1,berth1,storage1,200.0,task.getTemplates().size());
task.getTemplates().add(load11p);
LoadingTemplate load11m1r = new LoadingTemplate(ship1,berth1,storage1,-200.0,task.getTemplates().size()); load11m1r.getResources().add(eq1);
task.getTemplates().add(load11m1r);
LoadingTemplate load12p2r = new LoadingTemplate(ship1,berth2,storage2,10.0,task.getTemplates().size()); load12p2r.getResources().add(eq1); load11m1r.getResources().add(eq2);
task.getTemplates().add(load12p2r);
// Бункеровки
BunkeringTemplate bunk1 = new BunkeringTemplate(ship1,berth0,null,bunker1,300.0,task.getTemplates().size());
task.getTemplates().add(bunk1);
BunkeringTemplate bunk21 = new BunkeringTemplate(ship2,berth1,storage3,null,200.0,task.getTemplates().size());
task.getTemplates().add(bunk21);
BunkeringTemplate bunk22 = new BunkeringTemplate(ship2,berth1,storage3,null,200.0,task.getTemplates().size()); bunk22.getResources().add(eq1); bunk22.getResources().add(eq2);
task.getTemplates().add(bunk22);
// Внешние грузопотоки
CargoFlow cf1 = new CargoFlow(storage1); cf1.getFlow().put(0.0, -100.0); cf1.getFlow().put(1.0, -150.0); cf1.getFlow().put(2.0, 20.0); cf1.getFlow().put(3.0, 400.0);
task.getCargoFlows().add(cf1);
CargoFlow cf2 = new CargoFlow(storage2); cf2.getFlow().put(0.0, 100.0);
task.getCargoFlows().add(cf2);
// Окна непогоды
TimeWindow tw1 = new TimeWindow(load11m1r); tw1.getBanWindows().put(5.2, 2.0); tw1.getBanWindows().put(12.6, 3.0);
task.getTimeWindowConstraints().add(tw1);
TimeWindow tw2 = new TimeWindow(moore21p2b); tw2.getBanWindows().put(3.2, 2.0); tw2.getBanWindows().put(8.3, 0.123);
task.getTimeWindowConstraints().add(tw2);
// Начальное состояние
TransportShipState sini1 = new TransportShipState(0.0, ship1, berth0, false); sini1.getCargoState().put(cargo1, 1000.0);
task.getVesselInitialState().add(sini1);
TransportShipState sini2 = new TransportShipState(0.0, ship2, berth0, false); sini2.getCargoState().put(cargo1, 0.0); sini2.getCargoState().put(cargo0, 130.0);
task.getVesselInitialState().add(sini2);
MovingObjectState sini3 = new MovingObjectState(tow1,berth0);
task.getVesselInitialState().add(sini3);
StorageState tini1 = new StorageState(storage1, 200.0);
task.getStorageInitialState().add(tini1);
StorageState tini2 = new StorageState(storage2, 800.0);
task.getStorageInitialState().add(tini2);
StorageState tini3 = new StorageState(storage3, 500.0);
task.getStorageInitialState().add(tini3);
// Конечное состояние
TransportShipState send1 = new TransportShipState(0.0, ship1, berth0, false); send1.getCargoState().put(cargo1, 0.0); send1.getCargoState().put(cargo2, 400.0);
task.getVesselEndState().add(send1);
TransportShipState send2 = new TransportShipState(0.0, ship2, berth0, false); send2.getCargoState().put(cargo1, 1000.0); send2.getCargoState().put(cargo0, 700.0);
task.getVesselEndState().add(send2);
// Горизонт планирования
task.setPlanningInterval(520.0);
}
*/
/// <summary>
/// Тест мобилизации средств грузообработки
/// </summary>
/// <returns></returns>
public static void fillByHandBK(TaskCase task)
{
//Объекты
// Грузы
Cargo cargo0 = new Cargo(1.0, 0, "LNG");
task.getCargoes().add(cargo0);
// Причалы
Berth berth0 = new Berth(1, "Raid", true);
task.getBerths().add(berth0);
Berth berth1 = new Berth(2, "Pier 1", false);
task.getBerths().add(berth1);
Berth berth2 = new Berth(3, "Pier 2", false);
task.getBerths().add(berth2);
// Хранилища
Storage storage1 = new Storage(4, "Storage 1", 270.0, cargo0);
task.getStorages().add(storage1);
// Оборудование
LoadingEquipment floatingCrane = new LoadingEquipment(5, "Плавучий кран");
task.getEquipments().add(floatingCrane);
// Буксиры
Tow tow1 = new Tow(6, "Tow 1");
task.getTows().add(tow1);
// Суда
TransportShip ship1 = new TransportShip(7, "Ship 1", 2000);
task.getShips().add(ship1);
// Перемещения
// 1
MovingTemplate move101 = new MovingTemplate(ship1,berth0,berth1,2.0,8+task.getTemplates().size()); move101.getResources().add(tow1);
task.getTemplates().add(move101);
// 2
MovingTemplate move110 = new MovingTemplate(ship1,berth1,berth0,2.0,8+task.getTemplates().size()); move110.getResources().add(tow1);
task.getTemplates().add(move110);
// 3
MovingTemplate movet20 = new MovingTemplate(tow1,berth2,berth0,2.0,8+task.getTemplates().size());
task.getTemplates().add(movet20);
// 4
MovingTemplate movet10 = new MovingTemplate(tow1,berth1,berth0,2.0,8+task.getTemplates().size());
task.getTemplates().add(movet10);
// 5
MovingTemplate movet12 = new MovingTemplate(tow1,berth1,berth2,1.0,8+task.getTemplates().size());
task.getTemplates().add(movet12);
// 6
MovingTemplate movek21 = new MovingTemplate(floatingCrane,berth2,berth1,2.0,8+task.getTemplates().size()); movek21.getResources().add(tow1);
task.getTemplates().add(movek21);
// 7
LoadingTemplate lt = new LoadingTemplate(ship1,berth1,storage1,-20.0,8+task.getTemplates().size()); lt.getResources().add(floatingCrane);
task.getTemplates().add(lt);
StorageState tini1 = new StorageState(storage1, cargo0, 0.0);
task.getStorageInitialState().add(tini1);
// Начальное состояние
MovingObjectState sini1 = new MovingObjectState(ship1, berth0);
task.getVesselInitialState().add(sini1);
StorageState stini1 = new StorageState(ship1,cargo0, 100.0);
task.getStorageInitialState().add(stini1);
MovingObjectState sini2 = new MovingObjectState(tow1, berth1);
task.getVesselInitialState().add(sini2);
MovingObjectState sini3 = new MovingObjectState(floatingCrane, berth2);
task.getVesselInitialState().add(sini3);
MovingObjectState send1 = new MovingObjectState(ship1, berth0);
task.getVesselEndState().add(send1);
StorageState stend1 = new StorageState(ship1,cargo0, 0.0);
task.getStorageEndState().add(stend1);
task.setPlanningInterval(18);
}
/*
/// <summary>
/// Тест мобилизации средств грузообработки
/// </summary>
/// <returns></returns>
public static void fillByHandTest(TaskCase task)
{
//Объекты
// Грузы
Cargo cargo0 = new Cargo(1.0, 0, "LNG");
task.getCargoes().add(cargo0);
// Причалы
Berth berth0 = new Berth(0, "Raid", true);
task.getBerths().add(berth0);
Berth berth1 = new Berth(1, "Pier 1", false);
task.getBerths().add(berth1);
Berth berth2 = new Berth(2, "Pier 2", false);
task.getBerths().add(berth2);
// Хранилища
Storage storage1 = new Storage(1, "Storage 1", 270.0, cargo0);
task.getStorages().add(storage1);
// Оборудование
LoadingEquipment floatingCrane = new LoadingEquipment(2, "Плавучий кран");
task.getEquipments().add(floatingCrane);
// Буксиры
Tow tow1 = new Tow(1, "Tow 1");
task.getTows().add(tow1);
// Суда
TransportShip ship1 = new TransportShip(4, "Ship 1", 2000);
task.getShips().add(ship1);
// Перемещения
// 1
MovingTemplate move101 = new MovingTemplate(ship1,berth0,berth1,2.0,task.getTemplates().size()); move101.getResources().add(tow1);
task.getTemplates().add(move101);
// 2
MovingTemplate move110 = new MovingTemplate(ship1,berth1,berth0,1.0,task.getTemplates().size()); move110.getResources().add(tow1);
task.getTemplates().add(move110);
// 3
MovingTemplate movet20 = new MovingTemplate(tow1,berth2,berth0,2.0,task.getTemplates().size());
task.getTemplates().add(movet20);
// 4
MovingTemplate movet10 = new MovingTemplate(tow1,berth1,berth0,2.0,task.getTemplates().size());
task.getTemplates().add(movet10);
// 5
MovingTemplate movet12 = new MovingTemplate(tow1,berth1,berth2,1.0,task.getTemplates().size());
task.getTemplates().add(movet12);
// 6
MovingTemplate movek21 = new MovingTemplate(floatingCrane,berth2,berth1,2.0,task.getTemplates().size()); movek21.getResources().add(tow1);
task.getTemplates().add(movek21);
// 7
LoadingTemplate lt = new LoadingTemplate(ship1,berth1,storage1,100.0,task.getTemplates().size()); lt.getResources().add(floatingCrane);
task.getTemplates().add(lt);
StorageState tini1 = new StorageState(storage1, 200.0);
task.getStorageInitialState().add(tini1);
// Начальное состояние
TransportShipState sini1 = new TransportShipState(0.0, ship1, berth0, false); sini1.getCargoState().put(cargo0, 0.0);
task.getVesselInitialState().add(sini1);
MovingObjectState sini2 = new MovingObjectState(tow1, berth1);
task.getVesselInitialState().add(sini2);
MovingObjectState sini3 = new MovingObjectState(floatingCrane, berth2);
task.getVesselInitialState().add(sini3);
TransportShipState send1 = new TransportShipState(0.0, ship1, berth0, false); send1.getCargoState().put(cargo0, 200.0);
task.getVesselEndState().add(send1);
task.setPlanningInterval(12);
}
*/
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package inport;
/**
*
* @author topazh_ag
*/
public class LoadingEquipment extends MovingObject{
public LoadingEquipment() {
super();
}
public LoadingEquipment(int id, String name) {
super(id, name);
}
@Override
public String toString() {
return getId() + ";" + getName();
}
public LoadingEquipment(String s) {
super(s);
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package inport;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author topazh_ag
*/
public class LoadingTemplate extends OperationTemplate {
private TransportShip loader;
private Storage storage;
private List<LoadingEquipment> resources;
private double intensity;
/**
* Get the value of resources
*
* @return the value of resources
*/
public List<LoadingEquipment> getResources() {
return resources;
}
/**
* Set the value of resources
*
* @param resources new value of resources
*/
public void setResources(List<LoadingEquipment> resources) {
this.resources = resources;
}
public TransportShip getLoader() {
return loader;
}
public void setLoader(TransportShip loader) {
this.loader = loader;
}
public double getIntensity() {
return intensity;
}
public void setIntensity(double intensity) {
this.intensity = intensity;
}
public Storage getStorage() {
return storage;
}
public void setStorage(Storage storage) {
this.storage = storage;
}
public LoadingTemplate(TransportShip loader, Berth berth, Storage storage, double intensity, int id) {
super(id, berth);
this.loader = loader;
this.storage = storage;
this.resources = new ArrayList<>();
this.intensity = intensity;
}
public LoadingTemplate() {
this.resources = new ArrayList<>();
}
@Override
public String toString() {
String res = "";
boolean first = true;
for(LoadingEquipment eq : resources)
{
if (!first)
res += "," + eq.getId();
else
res += eq.getId();
first = false;
}
int source = loader.getId();
if (intensity>0)
source = storage.getId();
int target = loader.getId();
if (intensity<=0)
target = storage.getId();
int cargos = storage.getCargo().getId();
return getId() + ";" + "loa;" + twtoString() + ";" + source + ";" + cargos + ";" + target + ";" + getStartLocation().getId() + ";[" + res +"];"+Math.abs(intensity);
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package inport;
/**
*
* @author topazh_ag
*/
public class MooringTemplate extends TowUsingTemplate {
private TransportShip moorer;
private boolean direct;
/**
* Get the value of direction
*
* @return the value of direction
*/
public boolean isDirect() {
return direct;
}
/**
* Set the value of direction
*
* @param direction new value of direction
*/
public void setDirect(boolean direct) {
this.direct = direct;
}
/**
* Get the value of moorer
*
* @return the value of moorer
*/
public MovingObject getMoorer() {
return moorer;
}
/**
* Set the value of moorer
*
* @param mover new value of moorer
*/
public void setMoorer(TransportShip moorer) {
this.moorer = moorer;
}
public MooringTemplate(TransportShip moorer, Berth berth, double duration, boolean direct, int id) {
super(duration, id, berth);
this.moorer = moorer;
this.direct = direct;
}
public MooringTemplate() {
super();
}
@Override
public String toString() {
String res = "";
boolean first = true;
for(Tow eq : getResources())
{
if (!first)
res += "," + eq.getId();
else
res += eq.getId();
first = false;
}
String code = "mrn";
if (!direct)
code = "unm";
return getId() + ";" + code + ";" + twtoString() + ";" + moorer.getId() + ";" + getStartLocation().getId() + ";[" + res +"];"+getDuration();
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package inport;
/**
*
* @author topazh_ag
*/
public class MovingObject {
private int id;
private String name;
/**
* Get the value of name
*
* @return the value of name
*/
public String getName() {
return name;
}
/**
* Set the value of name
*
* @param name new value of name
*/
public void setName(String name) {
this.name = name;
}
/**
* Get the value of id
*
* @return the value of id
*/
public int getId() {
return id;
}
/**
* Set the value of id
*
* @param id new value of id
*/
public void setId(int id) {
this.id = id;
}
public MovingObject(int id, String name) {
this.id = id;
this.name = name;
}
public MovingObject() {
}
public MovingObject(String s) {
String[] tokens = s.split(";");
id = Integer.parseInt(tokens[0].trim());
name = tokens[1].trim();
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package inport;
/**
*
* @author topazh_ag
*/
public class MovingObjectState {
private MovingObject vessel;
private Berth location;
/**
* Get the value of location
*
* @return the value of location
*/
public Berth getLocation() {
return location;
}
/**
* Set the value of location
*
* @param location new value of location
*/
public void setLocation(Berth location) {
this.location = location;
}
/**
* Get the value of vessel
*
* @return the value of vessel
*/
public MovingObject getVessel() {
return vessel;
}
/**
* Set the value of vessel
*
* @param vessel new value of vessel
*/
public void setVessel(MovingObject vessel) {
this.vessel = vessel;
}
/**
*
* @param vessel
* @param location
*/
public MovingObjectState(MovingObject vessel, Berth location) {
this.vessel = vessel;
this.location = location;
}
public MovingObjectState() {
}
@Override
public String toString() {
return getVessel().getId() + ";" + getLocation().getId();
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package inport;
/**
*
* @author topazh_ag
*/
public class MovingTemplate extends TowUsingTemplate {
private MovingObject mover;
private Berth destination;
/**
* Get the value of destination
*
* @return the value of destination
*/
public Berth getDestination() {
return destination;
}
/**
* Set the value of destination
*
* @param destination new value of destination
*/
public void setDestination(Berth destination) {
this.destination = destination;
}
/**
* Get the value of mover
*
* @return the value of mover
*/
public MovingObject getMover() {
return mover;
}
/**
* Set the value of mover
*
* @param mover new value of mover
*/
public void setMover(MovingObject mover) {
this.mover = mover;
}
public MovingTemplate(MovingObject mover, Berth source, Berth destination, double duration, int id) {
super(duration, id, source);
this.mover = mover;
this.destination = destination;
}
public MovingTemplate() {
super();
}
@Override
public String toString() {
String res = "";
boolean first = true;
for(Tow eq : getResources())
{
if (!first)
res += "," + eq.getId();
else
res += eq.getId();
first = false;
}
return getId() + ";" + "mov;" + twtoString() + ";" + mover.getId() + ";" + getStartLocation().getId() + ";" + destination.getId() + ";[" + res +"];"+getDuration();
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package inport;
/**
*
* @author topazh_ag
*/
public class Operation {
private OperationTemplate template;
private double start;
private double duration;
/**
* Get the value of duration
*
* @return the value of duration
*/
public double getDuration() {
return duration;
}
/**
* Set the value of duration
*
* @param duration new value of duration
*/
public void setDuration(double duration) {
this.duration = duration;
}
/**
* Get the value of start
*
* @return the value of start
*/
public double getStart() {
return start;
}
/**
* Set the value of start
*
* @param start new value of start
*/
public void setStart(double start) {
this.start = start;
}
/**
* Get the value of template
*
* @return the value of template
*/
public OperationTemplate getTemplate() {
return template;
}
/**
* Set the value of template
*
* @param template new value of template
*/
public void setTemplate(OperationTemplate template) {
this.template = template;
}
public Operation() {
}
@Override
public String toString() {
return template.getId() + "; R; " + start + "; " + duration;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package inport;
import java.util.HashMap;
import java.util.Map;
/**
*
* @author topazh_ag
*/
public abstract class OperationTemplate {
private int id;
private Berth startLocation;
Map<Double, Double> timeWindows;
/**
* Get the value of id
*
* @return the value of id
*/
public int getId() {
return id;
}
/**
* Set the value of id
*
* @param id new value of id
*/
public void setId(int id) {
this.id = id;
}
public Berth getStartLocation() {
return startLocation;
}
public void setStartLocation(Berth startLocation) {
this.startLocation = startLocation;
}
public Map<Double, Double> getTimeWindows() {
return timeWindows;
}
public void setBanWindows(Map<Double, Double> timeWindows) {
this.timeWindows = timeWindows;
}
protected String twtoString() {
String res = "";
boolean first = true;
for(Double s : timeWindows.keySet())
{
if (!first)
res += "," + s + ":" + timeWindows.get(s);
else
res += s + ":" + timeWindows.get(s);
first = false;
}
return "[" + res +"]";
}
public OperationTemplate(int id, Berth startLocation) {
this.id = id;
this.startLocation = startLocation;
this.timeWindows = new HashMap<>();
}
public OperationTemplate() {
this.timeWindows = new HashMap<>();
}
public void setTimeWindow(String s) {
this.timeWindows = new HashMap<>();
String[] rs = s.replace("[", "").replace("]", "").split(",");
for (String crs : rs)
if (crs.length()>0)
{
String[] kv = crs.split(":");
Double key1 = Double.parseDouble(kv[0]);
Double value1 = Double.parseDouble(kv[1]);
timeWindows.put(key1, value1);
}
}
}
package inport;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.stream.util.StreamReaderDelegate;
import javax.xml.transform.stream.StreamResult;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author topazh_ag
*/
public class SerializationHelper {
public String serialize(Object object) throws Exception{
StringWriter resultWriter = new StringWriter();
StreamResult result = new StreamResult( resultWriter );
XMLStreamWriter xmlStreamWriter =
XMLOutputFactory.newInstance().createXMLStreamWriter(result);
JAXBContext context = JAXBContext.newInstance(object.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.marshal(new JAXBElement(new QName(object.getClass().getSimpleName()), object.getClass(), object), xmlStreamWriter);
String res = resultWriter.toString();
return res;
}
public Object deserialize(String str, Class klass) throws Exception{
InputStream is = new ByteArrayInputStream(str.getBytes("UTF-8"));
XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(is);
reader = new CamelCaseTransfomingReaderDelegate(reader, klass);
JAXBContext context = JAXBContext.newInstance(klass);
Unmarshaller unmarshaller = context.createUnmarshaller();
JAXBElement elem = unmarshaller.unmarshal(reader, klass);
Object object = elem.getValue();
return object;
}
//adapts to Java property naming style
private static class CamelCaseTransfomingReaderDelegate extends StreamReaderDelegate {
Class klass = null;
public CamelCaseTransfomingReaderDelegate(XMLStreamReader xsr, Class klass) {
super(xsr);
this.klass = klass;
}
@Override
public String getLocalName() {
String nodeName = super.getLocalName();
if (!nodeName.equals(klass.getSimpleName()))
{
nodeName = nodeName.substring(0, 1).toLowerCase() +
nodeName.substring(1, nodeName.length());
}
return nodeName.intern(); //NOTE: intern very important!..
}
}
}
\ No newline at end of file
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package inport;
import java.util.Map;
/**
*
* @author topazh_ag
*/
public class Storage {
private int id;
private String name;
private double volume;
private Cargo cargo;
/**
* Get the value of cargo
*
* @return the value of cargo
*/
public Cargo getCargo() {
return cargo;
}
/**
* Set the value of cargo
*
* @param cargo new value of cargo
*/
public void setCargo(Cargo cargo) {
this.cargo = cargo;
}
/**
* Get the value of volume
*
* @return the value of volume
*/
public double getVolume() {
return volume;
}
/**
* Set the value of volume
*
* @param volume new value of volume
*/
public void setVolume(double volume) {
this.volume = volume;
}
/**
* Get the value of name
*
* @return the value of name
*/
public String getName() {
return name;
}
/**
* Set the value of name
*
* @param name new value of name
*/
public void setName(String name) {
this.name = name;
}
/**
* Get the value of id
*
* @return the value of id
*/
public int getId() {
return id;
}
/**
* Set the value of id
*
* @param id new value of id
*/
public void setId(int id) {
this.id = id;
}
public Storage(int id, String name, double volume, Cargo cargo) {
this.id = id;
this.name = name;
this.volume = volume;
this.cargo = cargo;
}
public Storage() {
}
@Override
public String toString() {
return id + ";" + name + ";" + cargo.getId() + ";" +volume;
}
public Storage(String s, Map<Integer, Cargo> cargoes) {
String[] tokens = s.split(";");
id = Integer.parseInt(tokens[0].trim());
name = tokens[1].trim();
volume = Double.parseDouble(tokens[3].trim());
int key = Integer.parseInt(tokens[2].trim());
cargo = cargoes.get(key);
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package inport;
import java.util.Map;
/**
*
* @author topazh_ag
*/
public class StorageState {
private Object storage;
private Cargo cargo;
private double cargoState;
/**
* Get the value of storage
*
* @return the value of storage
*/
public Object getStorage() {
return storage;
}
/**
* Set the value of storage
*
* @param storage new value of storage
*/
public void setStorage(Object storage) {
this.storage = storage;
}
public Cargo getCargo() {
return cargo;
}
public void setCargo(Cargo cargo) {
this.cargo = cargo;
}
/**
* Get the value of cargoState
*
* @return the value of cargoState
*/
public double getCargoState() {
return cargoState;
}
/**
* Set the value of cargoState
*
* @param cargoState new value of cargoState
*/
public void setCargoState(double cargoState) {
this.cargoState = cargoState;
}
/**
*
* @param storage
*/
public StorageState(Object storage, Cargo cargo, double cargoState) {
this.storage = storage;
this.cargo = cargo;
this.cargoState = cargoState;
}
public StorageState() {
}
@Override
public String toString() {
if (storage instanceof Storage)
return cargo.getId() + ";" + ((Storage)storage).getId() + ";" + cargoState;
if (storage instanceof TransportShip)
return cargo.getId() + ";" + ((TransportShip)storage).getId() + ";" + cargoState;
return "";
}
public StorageState(String s, Map<Integer, Storage> mp, Map<Integer, MovingObject> vp, Map<Integer, Cargo> cp) {
String[] tokens = s.split(";");
int key = Integer.parseInt(tokens[0].trim());
cargo = cp.get(key);
key = Integer.parseInt(tokens[1].trim());
if (mp.containsKey(key))
storage = mp.get(key);
if (vp.containsKey(key))
storage = vp.get(key);
cargoState = Double.parseDouble(tokens[2].trim());
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package inport;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author topazh_ag
*/
public class TaskCase {
// Статическая структура порта
private List<Cargo> cargoes;
private List<Berth> berths;
private List<Storage> storages;
private List<Bunker> bunkers;
private List<Tow> tows;
private List<LoadingEquipment> equipments;
// Обслуживаемые суда
private List<TransportShip> ships;
// Шаблоны операций
private List<OperationTemplate> templates;
// Внешние грузопотоки
private List<CargoFlow> cargoFlows;
// Начальное состояние
private List<MovingObjectState> vesselInitialState;
private List<StorageState> storageInitialState;
// Конечное состояние
private List<MovingObjectState> vesselEndState;
private List<StorageState> storageEndState;
// Горизонт планирования
private double planningInterval;
// Тип критерия оптимизации
private int criterionType;
// План - критерий
private double solution_result;
// План - решение
private List<Operation> solution;
/**
* Get the value of solution
*
* @return the value of solution
*/
public List<Operation> getSolution() {
return solution;
}
/**
* Set the value of solution
*
* @param solution new value of solution
*/
public void setSolution(List<Operation> solution) {
this.solution = solution;
}
/**
* Get the value of planningInterval
*
* @return the value of planningInterval
*/
public double getPlanningInterval() {
return planningInterval;
}
/**
* Set the value of planningInterval
*
* @param planningInterval new value of planningInterval
*/
public void setPlanningInterval(double planningInterval) {
this.planningInterval = planningInterval;
}
public int getCriterionType() {
return criterionType;
}
public void setCriterionType(int criterionType) {
this.criterionType = criterionType;
}
public double getSolution_result() {
return solution_result;
}
public void setSolution_result(double solution_result) {
this.solution_result = solution_result;
}
/**
* Get the value of cargoFlows
*
* @return the value of cargoFlows
*/
public List<CargoFlow> getCargoFlows() {
return cargoFlows;
}
/**
* Set the value of cargoFlows
*
* @param cargoFlows new value of cargoFlows
*/
public void setCargoFlows(List<CargoFlow> cargoFlows) {
this.cargoFlows = cargoFlows;
}
/**
* Get the value of vesselEndState
*
* @return the value of vesselEndState
*/
public List<MovingObjectState> getVesselEndState() {
return vesselEndState;
}
/**
* Set the value of vesselEndState
*
* @param vesselEndState new value of vesselEndState
*/
public void setVesselEndState(List<MovingObjectState> vesselEndState) {
this.vesselEndState = vesselEndState;
}
public List<StorageState> getStorageEndState() {
return storageEndState;
}
public void setStorageEndState(List<StorageState> storageEndState) {
this.storageEndState = storageEndState;
}
/**
* Get the value of storageInitialState
*
* @return the value of storageInitialState
*/
public List<StorageState> getStorageInitialState() {
return storageInitialState;
}
/**
* Set the value of storageInitialState
*
* @param storageInitialState new value of storageInitialState
*/
public void setStorageInitialState(List<StorageState> storageInitialState) {
this.storageInitialState = storageInitialState;
}
/**
* Get the value of vesselInitialState
*
* @return the value of vesselInitialState
*/
public List<MovingObjectState> getVesselInitialState() {
return vesselInitialState;
}
/**
* Set the value of vesselInitialState
*
* @param vesselInitialState new value of vesselInitialState
*/
public void setVesselInitialState(List<MovingObjectState> vesselInitialState) {
this.vesselInitialState = vesselInitialState;
}
/**
* Get the value of templates
*
* @return the value of templates
*/
public List<OperationTemplate> getTemplates() {
return templates;
}
/**
* Set the value of templates
*
* @param templates new value of templates
*/
public void setTemplates(List<OperationTemplate> templates) {
this.templates = templates;
}
/**
* Get the value of ships
*
* @return the value of ships
*/
public List<TransportShip> getShips() {
return ships;
}
/**
* Set the value of ships
*
* @param ships new value of ships
*/
public void setShips(List<TransportShip> ships) {
this.ships = ships;
}
/**
* Get the value of equipments
*
* @return the value of equipments
*/
public List<LoadingEquipment> getEquipments() {
return equipments;
}
/**
* Set the value of equipments
*
* @param equipments new value of equipments
*/
public void setEquipments(List<LoadingEquipment> equipments) {
this.equipments = equipments;
}
/**
* Get the value of tows
*
* @return the value of tows
*/
public List<Tow> getTows() {
return tows;
}
/**
* Set the value of tows
*
* @param tows new value of tows
*/
public void setTows(List<Tow> tows) {
this.tows = tows;
}
/**
* Get the value of bunkers
*
* @return the value of bunkers
*/
public List<Bunker> getBunkers() {
return bunkers;
}
/**
* Set the value of bunkers
*
* @param bunkers new value of bunkers
*/
public void setBunkers(List<Bunker> bunkers) {
this.bunkers = bunkers;
}
/**
* Get the value of storages
*
* @return the value of storages
*/
public List<Storage> getStorages() {
return storages;
}
/**
* Set the value of storages
*
* @param storages new value of storages
*/
public void setStorages(List<Storage> storages) {
this.storages = storages;
}
/**
* Get the value of cargoes
*
* @return the value of cargoes
*/
public List<Cargo> getCargoes() {
return cargoes;
}
/**
* Set the value of cargoes
*
* @param cargoes new value of cargoes
*/
public void setCargoes(List<Cargo> cargoes) {
this.cargoes = cargoes;
}
/**
* Get the value of berths
*
* @return the value of berths
*/
public List<Berth> getBerths() {
return berths;
}
/**
* Set the value of berths
*
* @param berths new value of berths
*/
public void setBerths(List<Berth> berths) {
this.berths = berths;
}
public TaskCase() {
cargoes = new ArrayList<>();
berths = new ArrayList<>();
storages = new ArrayList<>();
bunkers = new ArrayList<>();
tows = new ArrayList<>();
equipments = new ArrayList<>();
ships = new ArrayList<>();
templates = new ArrayList<>();
cargoFlows = new ArrayList<>();
vesselInitialState = new ArrayList<>();
storageInitialState = new ArrayList<>() ;
vesselEndState = new ArrayList<>();
storageEndState = new ArrayList<>();
solution = new ArrayList<>();
}
private MovingObjectState fromString(String s, Map<Integer, Berth> m_berth, Map<Integer, MovingObject> m_vessel)
{
String[] tkns1 = s.split(";");
MovingObjectState st = new MovingObjectState();
int key = Integer.parseInt(tkns1[0].trim());
MovingObject vs = m_vessel.get(key);
st.setVessel(vs);
key = Integer.parseInt(tkns1[1].trim());
st.setLocation(m_berth.get(key));
return st;
}
public void deserialize(String fileName) throws IOException
{
cargoes.clear(); berths.clear(); storages.clear(); bunkers.clear(); tows.clear(); equipments.clear(); ships.clear(); templates.clear();
cargoFlows.clear(); vesselInitialState.clear(); storageInitialState.clear(); vesselEndState.clear(); storageEndState.clear();
solution.clear();
String[] tags = {"Cargoes", "Berths", "Storages", "Bunkers", "Tows", "Loading Equipments", "Transport Ships", "Templates", "Time Windows", "Cargo Flows", "Initial Vessel State", "Initial Storage State", "Final Vessel State", "Final Storage State", "Task Properties", "Solution"};
// Open the file
FileInputStream fstream;
try
{
fstream = new FileInputStream(fileName);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
int index = 0;
Map<Integer, Cargo> m_cargo = new HashMap<>();
Map<Integer, Berth> m_berth = new HashMap<>();
Map<Integer, Storage> m_storage = new HashMap<>();
Map<Integer, MovingObject> m_vessel = new HashMap<>();
Map<Integer, LoadingEquipment> m_equiopment = new HashMap<>();
Map<Integer, OperationTemplate> m_template = new HashMap<>();
//Read File Line By Line
int numInside = 0;
while ((strLine = br.readLine()) != null)
{
numInside ++;
boolean serviceString = false;
for (int i = 0; i<tags.length; i++)
if (strLine.equals(tags[i]))
{
index = i+1;
serviceString = true;
break;
}
if (serviceString || strLine.length()<2 || index==0 || strLine.startsWith("/*"))
{
numInside = 0;
continue;
}
switch (index) {
case 1: Cargo c = new Cargo(strLine); cargoes.add(c); m_cargo.put(c.getId(), c);
break;
case 2: Berth b = new Berth(strLine); berths.add(b); m_berth.put(b.getId(), b);
break;
case 3: Storage s = new Storage(strLine, m_cargo); storages.add(s); m_storage.put(s.getId(), s);
break;
case 4:
break;
case 5: Tow t = new Tow(strLine); tows.add(t); m_vessel.put(t.getId(), t);
break;
case 6: LoadingEquipment l = new LoadingEquipment(strLine); equipments.add(l); m_equiopment.put(l.getId(), l); m_vessel.put(l.getId(), l);
break;
case 7: TransportShip ts = new TransportShip(strLine); ships.add(ts); m_vessel.put(ts.getId(), ts);
break;
case 8: String[] tokens = strLine.split(";");
if (tokens[1].trim().equals("mov"))
{
MovingTemplate mt = new MovingTemplate();
mt.setId(Integer.parseInt(tokens[0].trim()));
mt.setTimeWindow(tokens[2].trim());
int key = Integer.parseInt(tokens[3].trim());
mt.setMover(m_vessel.get(key));
key = Integer.parseInt(tokens[4].trim());
mt.setStartLocation(m_berth.get(key));
key = Integer.parseInt(tokens[5].trim());
mt.setDestination(m_berth.get(key));
String[] rs = tokens[6].trim().replace("[", "").replace("]", "").split(",");
for (String crs : rs)
if (crs.length()>0)
{
key = Integer.parseInt(crs.trim());
mt.getResources().add((Tow)m_vessel.get(key));
}
mt.setDuration(Double.parseDouble(tokens[7].trim()));
templates.add(mt);
m_template.put(mt.getId(), mt);
}
if (tokens[1].trim().equals("mrn") || tokens[1].trim().equals("unm"))
{
MooringTemplate mt = new MooringTemplate();
mt.setId(Integer.parseInt(tokens[0].trim()));
mt.setDirect(true);
if (tokens[1].trim().equals("unm"))
mt.setDirect(false);
mt.setTimeWindow(tokens[2].trim());
int key = Integer.parseInt(tokens[3].trim());
mt.setMoorer((TransportShip)m_vessel.get(key));
key = Integer.parseInt(tokens[4].trim());
mt.setStartLocation(m_berth.get(key));
String[] rs = tokens[5].trim().replace("[", "").replace("]", "").split(",");
for (String crs : rs)
if (crs.length()>0)
{
key = Integer.parseInt(crs.trim());
mt.getResources().add((Tow)m_vessel.get(key));
}
mt.setDuration(Double.parseDouble(tokens[6].trim()));
templates.add(mt);
m_template.put(mt.getId(), mt);
}
if (tokens[1].trim().equals("loa"))
{
LoadingTemplate mt = new LoadingTemplate();
mt.setId(Integer.parseInt(tokens[0].trim()));
mt.setTimeWindow(tokens[2].trim());
int direct = 1;
// Источник. Пока пара - это только хранилище-судно. С бункеровкой будем разбираться потом
int key = Integer.parseInt(tokens[3].trim());
if (m_vessel.containsKey(key))
{
mt.setLoader((TransportShip)m_vessel.get(key));
direct = -1;
}
if (m_storage.containsKey(key))
{
mt.setStorage((Storage)m_storage.get(key));
direct = 1;
}
// Груз. Пока не нужен
// Приемник. Пока пара - это только хранилище-судно. С бункеровкой будем разбираться потом
key = Integer.parseInt(tokens[5].trim());
if (m_vessel.containsKey(key))
{
mt.setLoader((TransportShip)m_vessel.get(key));
direct = 1;
}
if (m_storage.containsKey(key))
{
mt.setStorage((Storage)m_storage.get(key));
direct = - 1;
}
key = Integer.parseInt(tokens[6].trim());
mt.setStartLocation(m_berth.get(key));
String[] rs = tokens[7].trim().replace("[", "").replace("]", "").split(",");
for (String crs : rs)
if (crs.length()>0)
{
key = Integer.parseInt(crs.trim());
mt.getResources().add(m_equiopment.get(key));
}
mt.setIntensity(direct*Double.parseDouble(tokens[8].trim()));
templates.add(mt);
m_template.put(mt.getId(), mt);
}
break;
case 9:
break;
case 10: cargoFlows.add(new CargoFlow(strLine, m_storage, m_cargo));
break;
case 11: vesselInitialState.add(fromString(strLine, m_berth, m_vessel));
break;
case 12: storageInitialState.add(new StorageState(strLine, m_storage, m_vessel, m_cargo));
break;
case 13: vesselEndState.add(fromString(strLine, m_berth, m_vessel));
break;
case 14: storageEndState.add(new StorageState(strLine, m_storage, m_vessel, m_cargo));
break;
case 15:
{
String[] rs = strLine.split(";");
planningInterval = Double.parseDouble(rs[0].trim());
criterionType = Integer.parseInt(rs[1].trim());
}
break;
case 16: // Тут чтение операций если надо. Потом подумаем
break;
default:
break;
}
}
//Close the input stream
br.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(TaskCase.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void serialize(String fileName)
{
try(FileWriter writer = new FileWriter(fileName, false))
{
// запись всего
writer.write("Cargoes"+"\n");
for (Cargo c : cargoes)
writer.write(c.toString()+"\n");
writer.write("Berths"+"\n");
for (Berth c : berths)
writer.write(c.toString()+"\n");
writer.write("Storages"+"\n");
for (Storage c : storages)
writer.write(c.toString()+"\n");
writer.write("Bunkers"+"\n");
for (Bunker c : bunkers)
writer.write(c.toString()+"\n");
writer.write("Tows"+"\n");
for (Tow c : tows)
writer.write(c.toString()+"\n");
writer.write("Loading Equipments"+"\n");
for (LoadingEquipment c : equipments)
writer.write(c.toString()+"\n");
writer.write("Transport Ships"+"\n");
for (TransportShip c : ships)
writer.write(c.toString()+"\n");
writer.write("\n");
writer.write("Templates"+"\n");
for (OperationTemplate c : templates)
writer.write(c.toString()+"\n");
writer.write("\n");
writer.write("Cargo Flows"+"\n");
for (CargoFlow c : cargoFlows)
writer.write(c.toString()+"\n");
writer.write("Initial Vessel State"+"\n");
for (MovingObjectState c : vesselInitialState)
writer.write(c.toString()+"\n");
writer.write("Initial Storage State"+"\n");
for (StorageState c : storageInitialState)
writer.write(c.toString()+"\n");
writer.write("Final Vessel State"+"\n");
for (MovingObjectState c : vesselEndState)
writer.write(c.toString()+"\n");
writer.write("Final Storage State"+"\n");
for (StorageState c : storageEndState)
writer.write(c.toString()+"\n");
writer.write("\n");
writer.write("Task Properties"+"\n");
writer.write(planningInterval+";"+criterionType+"\n");
writer.write("\n");
writer.write("Solution"+"\n");
writer.write(solution_result+"\n");
for (Operation c : solution)
writer.write(c.toString()+"\n");
writer.flush();
}
catch(IOException ex){
System.out.println(ex.getMessage());
}
}
public void formSolutionFromPB(int res[])
{
int nTimes = (int) (planningInterval + 1.0 - 1e-7);
int rLength = res.length;
solution_result = 0.0;
for (int k=1; k<=nTimes; k++)
if (res[rLength-k]>0)
{
solution_result = nTimes - k + 1;
break;
}
int cIndex = -1;
for (OperationTemplate template : templates)
{
boolean inBlock = false;
int duration = 0;
Operation operation = null;
for (int i=0; i<nTimes; i++)
{
cIndex++;
if (res[cIndex]>0)
{
if (inBlock)
duration++;
else
{
// Это начало блока
operation = new Operation();
operation.setTemplate(template);
operation.setStart(i+1);
inBlock = true;
}
} else
{
if (inBlock)
{
// Это окончание блока
operation.setDuration(duration+1.0);
duration = 0;
solution.add(operation);
inBlock = false;
}
}
}
if (inBlock)
{
// Это окончание блока
operation.setDuration(duration+1.0);
solution.add(operation);
}
}
}
private boolean isCompatible(OperationTemplate t1, OperationTemplate t2)
{
MovingObject doer1 = getDoer(t1);
Berth place1 = t1.getStartLocation();
List<Object> resources1 = getResources(t1);
MovingObject doer2 = getDoer(t2);
Berth place2 = t2.getStartLocation();
List<Object> resources2 = getResources(t2);
// Пересекаемость ресурсов
for (Object res2 : resources2)
if (resources1.contains(res2))
return false;
// Выполнитель = ресурс
if (resources1.contains(doer2))
return false;
// Ресурс = выполнитель
if (resources2.contains(doer1))
return false;
// Выполнитель = выполнитель
if (doer1.equals(doer2))
{
// Это не погрузка
if (!(t1 instanceof LoadingTemplate) || (!(t2 instanceof LoadingTemplate)))
return false;
// Разные причалы
if (!place1.equals(place2))
return false;
Storage s1 = ((LoadingTemplate)t1).getStorage();
Storage s2 = ((LoadingTemplate)t2).getStorage();
// В одно хранилище
if (s1.equals(s2))
return false;
}
else
// На одном причале и это не перемещения
if (place1.equals(place2) && (!(t1 instanceof MovingTemplate)) && (!(t2 instanceof MovingTemplate)))
return false;
return true;
}
MovingObject getDoer(OperationTemplate t)
{
if (t instanceof LoadingTemplate)
return ((LoadingTemplate)t).getLoader();
if (t instanceof MooringTemplate)
return ((MooringTemplate)t).getMoorer();
if (t instanceof MovingTemplate)
return ((MovingTemplate)t).getMover();
return null;
}
List<Object> getResources(OperationTemplate t)
{
List<Object> res = new ArrayList<>();
if (t instanceof LoadingTemplate)
res.addAll(((LoadingTemplate)t).getResources());
if (t instanceof MooringTemplate)
res.addAll(((MooringTemplate)t).getResources());
if (t instanceof MovingTemplate)
res.addAll(((MovingTemplate)t).getResources());
return res;
}
private String xij(int i, int j, int nJ, boolean plus)
{
int number = i*nJ + j + 1;
String prefix = "";
if (!plus)
prefix ="~";
return prefix+"x"+number;
}
private String wrbj(int r, int b, int j, int nB, int nJ, int nIni, boolean isMooring, boolean moored, boolean plus)
{
int number = nIni + r*nJ*nB + b*nJ + j + 1;
if (isMooring)
{
if (moored)
number = nIni + r*nJ*nB*2 + b*nJ*2 + nJ + j + 1;
else
number = nIni + r*nJ*nB*2 + b*nJ*2 + j + 1;
}
String prefix = "";
if (!plus)
prefix ="~";
return prefix+"x"+number;
}
private String sum1(List<Integer> iks, int j, int nJ, int weight)
{
String res = "";
for (Integer ik : iks)
res += " +" + weight + " "+xij(ik, j, nJ, true);
return res;
}
private String fj(int j, int nBefore, boolean plus)
{
int number = nBefore + j + 1;
String prefix = "";
if (!plus)
prefix ="~";
return prefix+"x"+number;
}
public int pb_formalize(String fileName/*, int indent*/) throws IOException
{
FileWriter writer = new FileWriter("temp", false);
int nCons = 0;
List<MovingObject> vss = new ArrayList<MovingObject>();
vss.addAll(ships);
vss.addAll(bunkers);
vss.addAll(tows);
vss.addAll(equipments);
// Выяснение вопроса с наличием швартовки
boolean isMooring = false;
for (OperationTemplate tp : templates)
if (tp instanceof MooringTemplate)
{
isMooring = true;
break;
}
int nTimes = (int) (planningInterval + 1.0 - 1e-7);
int nOperations = templates.size();
int nVars = nTimes*nOperations;
int nBerths = berths.size();
int nVessels = vss.size();
int mult =1;
if (isMooring)
mult = 2;
int nSuppVars = nVessels*nBerths*mult*nTimes;
int nBefore = nVars + nSuppVars;
int nAfter = nTimes;
// String s1 = wrbj(0, 1, 17, 6, 18, 756, true, true, true);
// String s2 = wrbj(0, 2, 0, 6, 18, 756, true, false, true);
// String s23 = s1+s2;
// Критерий оптимизации старый
/*
nAfter = 0;
String crit = "";
int degree = 1;
for (int j=nTimes - indent; j<nTimes; j++)
{
String clause = "";
for (int i=0; i<nOperations; i++)
clause += " "+xij(i, j, nTimes, false);
crit += " -"+degree+clause;
degree = degree * 2;
}
writer.write("min: " + crit + " ;\n");
*/
// Критерий оптимизации новый
String crit = "";
for (int j=0; j<nTimes; j++)
{
String clause = " +"+(j+1)+" "+fj(j, nBefore, true);
for (int k=j+1; k<nTimes; k++)
clause += " "+fj(k, nBefore, false);
crit += clause;
}
writer.write("min: " + crit + " ;\n");
// Вычисление служебных переменных для нового критерия
writer.write("* Criteria Variables \n");
for (int j=0; j<nTimes; j++)
{
String clause = " +1 " + fj(j, nBefore, true) + " +1";
for (OperationTemplate tp : templates)
{
int i = templates.indexOf(tp);
clause += " " + xij(i, j, nTimes, false);
}
writer.write(clause+" = 1 ;\n");
nCons++;
}
// Ограничения на окна непогоды
writer.write("* Time Window Constraints \n");
for (OperationTemplate tp : templates)
{
int i = templates.indexOf(tp);
Set<Double> starts = tp.timeWindows.keySet();
for (Double start : starts)
{
// Определяем стартовый индекс
int jstart = (int)(start.doubleValue());
Double duration = tp.timeWindows.get(start);
int jend = (int)(start.doubleValue()+duration.doubleValue());
for (int j=jstart; j<=jend; j++)
{
writer.write(" +1 "+xij(i,j,nTimes,true)+" = 0 ;\n");
nCons++;
}
}
}
//!!!! Неразделимость ресурсов для операций.
writer.write("* Resource Unsharedness \n");
// Новая байда
for (int i1 = 0; i1<nOperations; i1++)
{
OperationTemplate t1 = templates.get(i1);
List<Integer> excluders = new ArrayList<Integer>();
for (int i2 = i1+1; i2<nOperations; i2++)
{
OperationTemplate t2 = templates.get(i2);
if (!isCompatible(t1, t2))
excluders.add(i2);
}
if (excluders.size()>0)
{
for (int j=0; j<nTimes; j++)
{
String clause = " +1 " + xij(i1,j,nTimes,false) + " +1 " + xij(i1,j,nTimes,true);
for (Integer exc : excluders)
clause += " " + xij(exc,j,nTimes,false);
writer.write(clause + " >= 1 ;\n");
nCons++;
}
}
}
/*
// Старая байда
// Буксиры
writer.write("* For Tows \n");
for (Tow tow : tows)
{
List<Integer> iks = new ArrayList<Integer>();
for (OperationTemplate tpl : templates)
{
int ik = templates.indexOf(tpl);
if (tpl instanceof MovingTemplate)
{
MovingTemplate mt = (MovingTemplate)tpl;
if (mt.getMover().equals(tow))
iks.add(ik);
if (mt.getResources().contains(tow))
iks.add(ik);
}
if (tpl instanceof MooringTemplate)
{
MooringTemplate mt = (MooringTemplate)tpl;
if (mt.getResources().contains(tow))
iks.add(ik);
}
}
if (!iks.isEmpty())
for (int j=0; j<nTimes; j++)
{
writer.write(sum1(iks,j,nTimes,1)+" <= 1 ;\n");
nCons++;
}
}
// Краны
writer.write("* For Loading Equipments \n");
for (LoadingEquipment le : equipments)
{
List<Integer> iks = new ArrayList<Integer>();
for (OperationTemplate tpl : templates)
{
int ik = templates.indexOf(tpl);
if (tpl instanceof LoadingTemplate)
{
LoadingTemplate mt = (LoadingTemplate)tpl;
if (mt.getResources().contains(le))
iks.add(ik);
}
if (tpl instanceof BunkeringTemplate)
{
BunkeringTemplate bt = (BunkeringTemplate)tpl;
if (bt.getResources().contains(le))
iks.add(ik);
}
}
if (!iks.isEmpty())
for (int j=0; j<nTimes; j++)
{
writer.write(sum1(iks,j,nTimes,1)+" <= 1 ;\n");
nCons++;
}
}
// Бункеровщики
writer.write("* For Bunkers \n");
for (Bunker bk : bunkers)
{
List<Integer> iks = new ArrayList<Integer>();
for (OperationTemplate tpl : templates)
{
int ik = templates.indexOf(tpl);
if (tpl instanceof BunkeringTemplate)
{
BunkeringTemplate bt = (BunkeringTemplate)tpl;
if (bt.getBunker().equals(bk))
iks.add(ik);
}
}
if (!iks.isEmpty())
for (int j=0; j<nTimes; j++)
{
writer.write(sum1(iks,j,nTimes,1)+" <= 1 ;\n");
nCons++;
}
}
// Причалы
writer.write("* For Berths \n");
for (Berth bt : berths)
{
List<Integer> mrks = new ArrayList<Integer>();
List<List<Integer>> opbs = new ArrayList<List<Integer>>();
for (TransportShip ship : ships)
opbs.add(new ArrayList<Integer>());
boolean filled = false;
for (OperationTemplate tpl : templates)
{
int ik = templates.indexOf(tpl);
if (tpl instanceof MooringTemplate)
{
MooringTemplate mt = (MooringTemplate)tpl;
if (mt.getBerth().equals(bt))
{
mrks.add(ik);
int ind = ships.indexOf(mt.getMoorer());
if (ind>=0)
{
opbs.get(ind).add(ik);
filled = true;
}
}
}
if (tpl instanceof LoadingTemplate)
{
LoadingTemplate lt = (LoadingTemplate)tpl;
if (lt.getBerth().equals(bt))
{
int ind = ships.indexOf(lt.getLoader());
if (ind>=0)
{
opbs.get(ind).add(ik);
filled = true;
}
}
}
if (tpl instanceof BunkeringTemplate)
{
BunkeringTemplate bkt = (BunkeringTemplate)tpl;
if (bkt.getBerth().equals(bt))
{
int ind = ships.indexOf(bkt.getLoader());
if (ind>=0)
{
opbs.get(ind).add(ik);
filled = true;
}
}
}
}
// На причале одновременно выполняется только одна операция швартовки
if (!mrks.isEmpty())
for (int j=0; j<nTimes; j++)
{
writer.write(sum1(mrks,j,nTimes,1)+" <= 1 ;\n");
nCons++;
}
// На причале одновременно грузится (швартуется/бункеруется) только одно судно
if (filled)
for (int j=0; j<nTimes; j++)
{
String clause = "";
int nVess = 0;
for (List<Integer> vsops : opbs)
{
if (!vsops.isEmpty())
{
nVess ++;
String vsop = " +1 ";
for (Integer i : vsops)
vsop += xij(i,j,nTimes,false) + " ";
clause += vsop;
}
}
writer.write(clause + " >= " + (nVess-1) + " ; \n");
nCons++;
}
}
// Суда
writer.write("* For Vessels \n");
for (MovingObject vs : vss)
{
List<Integer> mvks = new ArrayList<Integer>();
List<Integer> loks = new ArrayList<Integer>();
List<Integer> mrks = new ArrayList<Integer>();
for (OperationTemplate tpl : templates)
{
int ik = templates.indexOf(tpl);
// Перемещения
if (tpl instanceof MovingTemplate)
{
MovingTemplate mt = (MovingTemplate)tpl;
if (mt.getMover().equals(vs))
mvks.add(ik);
}
// Погрузка
if (tpl instanceof LoadingTemplate)
{
LoadingTemplate lt = (LoadingTemplate)tpl;
if (lt.getLoader().equals(vs))
loks.add(ik);
}
// Швартовка
if (tpl instanceof MooringTemplate)
{
MooringTemplate rt = (MooringTemplate)tpl;
if (rt.getMoorer().equals(vs))
mrks.add(ik);
}
// Бункеровка
if (tpl instanceof BunkeringTemplate)
{
BunkeringTemplate bt = (BunkeringTemplate)tpl;
if (bt.getLoader().equals(vs))
loks.add(ik);
}
}
// Судно только в одном перемещении и при этом не может быть ни под погрузкой, ни под швартовкой
if (!mvks.isEmpty())
{
int nLM = loks.size()+mrks.size()+1;
for (int j=0; j<nTimes; j++)
{
String clause = sum1(mvks,j,nTimes,nLM);
clause += sum1(loks,j,nTimes,1);
clause += sum1(mrks,j,nTimes,1);
writer.write(clause+" <= " + nLM + " ;\n");
nCons++;
}
}
// TODO: Здесь еще как-то надо разбираться с пересечением швартовок, погрузок на разных причалах и т.д.
if (!loks.isEmpty())
{
}
}
*/
// Ограничения на непрерывность операций швартовки/перемещения
writer.write("* Unterminated Mooring and Moving \n");
for (OperationTemplate tpl : templates)
{
int ik = templates.indexOf(tpl);
int opd = -1;
if (tpl instanceof MovingTemplate)
{
MovingTemplate mt = (MovingTemplate)tpl;
opd = (int)(mt.getDuration() + 1.0 - 1e-7);
}
if (tpl instanceof MooringTemplate)
{
MooringTemplate mt = (MooringTemplate)tpl;
opd = (int)(mt.getDuration() + 1.0 - 1e-7);
}
int bJ = Math.min(nTimes, opd);
if (opd>0)
{
String res = "+1 " + xij(ik,0,nTimes,false) + " +1 "+xij(ik,0,nTimes,true);
for (int k=0; k<bJ; k++)
res += " " +xij(ik,k,nTimes,true);
writer.write(res+" >= 1 ;\n");
nCons++;
for (int j=1; j<nTimes; j++)
{
bJ = Math.min(nTimes, j+opd);
res = "-1 "+xij(ik,j-1,nTimes,false) + " " + xij(ik,j,nTimes,true) + " +1 "+xij(ik,j,nTimes,true) + " " + xij(ik,j-1,nTimes,false);
for (int k=j; k<bJ; k++)
res += " " +xij(ik,k,nTimes,true);
writer.write(res+" >= 0 ;\n");
nCons++;
}
}
}
// Ограничения на объем хранилищ. Проверить!!!
writer.write("* Current Storage Volume Constraints \n");
for (Storage st : storages)
{
int V = (int)st.getVolume();
double V0 = 0;
for (StorageState sst : storageInitialState)
if (sst.getStorage().equals(st))
{
V0 = (int)sst.getCargoState();
break;
}
CargoFlow ccf = null;
for (CargoFlow cf : cargoFlows)
if (cf.getStorage().equals(st))
{
ccf = cf;
break;
}
List<LoadingTemplate> lts = new ArrayList<LoadingTemplate>();
for (OperationTemplate tpl : templates)
{
if (tpl instanceof LoadingTemplate)
{
LoadingTemplate lt = (LoadingTemplate)tpl;
if (lt.getStorage().equals(st))
lts.add(lt);
}
}
if (lts.size()>0)
{
for (int j=0; j<nTimes; j++)
{
double sumV = V0;
if (ccf!=null)
sumV += ccf.getTotalValue(j);
String clause = "";
for (int l=0; l<=j; l++)
{
for (LoadingTemplate lt : lts)
{
int ik = templates.indexOf(lt);
int rate = (int)lt.getIntensity();
if (rate<0)
clause += " +" + (-rate) + " " + xij(ik, l, nTimes, true);
else
clause += " -" + rate + " " + xij(ik, l, nTimes, true);
}
}
String cl1 = clause + " >= " + (int)(-sumV) + " ;\n";
writer.write(cl1);
nCons++;
String cl2 = clause + " <= " + (int)(V-sumV) + " ;\n";
writer.write(cl2);
nCons++;
}
}
}
// Определение локационных переменных
writer.write("* Location Variables \n");
for (MovingObject v : vss)
{
// Нахождение начального состояния для судна. Потом читать из незавершенных операций
MovingObjectState iniState = null;
int iniTime = 0;
boolean iniMoored = false;
for (MovingObjectState state : vesselInitialState)
if (state.getVessel().equals(v))
{
iniState = state;
break;
}
int r = vss.indexOf(v);
for (Berth berth : berths)
{
int b = berths.indexOf(berth);
// Сперва ограничения на начальное состояние
if (iniState!=null && iniState.getLocation().equals(berth))
{
// До прибытия
for(int j=0; j<iniTime; j++)
{
String clause = "+1 "+wrbj(r, b, j, nBerths, nTimes, nVars, isMooring, false, true);
writer.write(clause + " = 0 ;\n");
nCons++;
if (isMooring)
{
clause = "+1 "+wrbj(r, b, j, nBerths, nTimes, nVars, isMooring, true, true);
writer.write(clause + " = 0 ;\n");
nCons++;
}
}
// В момент прибытия
if (!iniMoored)
{
String clause = "+1 "+wrbj(r, b, iniTime, nBerths, nTimes, nVars, isMooring, false, true);
writer.write(clause + " = 1 ;\n");
nCons++;
if (isMooring)
{
clause = "+1 "+wrbj(r, b, iniTime, nBerths, nTimes, nVars, isMooring, true, true);
writer.write(clause + " = 0 ;\n");
nCons++;
}
} else
{
String clause = "+1 "+wrbj(r, b, iniTime, nBerths, nTimes, nVars, isMooring, false, true);
writer.write(clause + " = 0 ;\n");
nCons++;
if (isMooring)
{
clause = "+1 "+wrbj(r, b, iniTime, nBerths, nTimes, nVars, isMooring, true, true);
writer.write(clause + " = 1 ;\n");
nCons++;
}
}
}
else
{
iniTime = 0;
String clause = "+1 "+wrbj(r, b, iniTime, nBerths, nTimes, nVars, isMooring, false, true);
writer.write(clause + " = 0 ;\n");
nCons++;
if (isMooring)
{
clause = "+1 "+wrbj(r, b, iniTime, nBerths, nTimes, nVars, isMooring, true, true);
writer.write(clause + " = 0 ;\n");
nCons++;
}
}
// Теперь на все остальные
List<Integer> rb_plus = new ArrayList<Integer>();
List<Integer> rb_minus = new ArrayList<Integer>();
List<Integer> rm_plus = new ArrayList<Integer>();
List<Integer> rm_minus = new ArrayList<Integer>();
for (OperationTemplate tp : templates)
{
if (tp instanceof MovingTemplate)
{
int idx = templates.indexOf(tp);
MovingTemplate mtp = (MovingTemplate)tp;
// Перемещение основного судна
if (mtp.getMover().equals(v))
{
if (mtp.getDestination().equals(berth))
rb_plus.add(idx);
if (mtp.getStartLocation().equals(berth))
rb_minus.add(idx);
}
// Перемещение его помоганцев
for (Tow tow : mtp.getResources())
if (tow.equals(v))
{
if (mtp.getDestination().equals(berth))
rb_plus.add(idx);
if (mtp.getStartLocation().equals(berth))
rb_minus.add(idx);
}
}
// TODO: отработка швартовки
if (tp instanceof MooringTemplate)
{
int idx = templates.indexOf(tp);
MooringTemplate mtp = (MooringTemplate)tp;
// Перемещение основного судна
if (mtp.getMoorer().equals(v))
{
if (mtp.getStartLocation().equals(berth) && mtp.isDirect()) // Пришвартовка
{
rm_plus.add(idx);
rb_minus.add(idx);
}
if (mtp.getStartLocation().equals(berth) && (!mtp.isDirect())) // Отшвартовка
{
rm_minus.add(idx);
rb_plus.add(idx);
}
}
}
}
if (true) //(rb_plus.size()>0 && rb_minus.size()>0)
{
for (int j=1; j<nTimes; j++)
{
String rbm = "";
for (Integer i : rb_minus)
rbm += " " + xij(i,j-1,nTimes,false);
String rbp = " ";
for (Integer i : rb_plus)
rbp += " -1 " + wrbj(r, b, j-1, nBerths, nTimes, nVars, isMooring, false, false) + " " + xij(i, j-1, nTimes, true) + " " + xij(i, j, nTimes, false);
String clause = "1 "+wrbj(r, b, j, nBerths, nTimes, nVars, isMooring, false, true) + " -1 " + wrbj(r, b, j-1, nBerths, nTimes, nVars, isMooring, false, true) + rbm + rbp;
writer.write(clause + " = 0 ;\n");
nCons++;
}
}
if (isMooring) //(rm_plus.size()>0 && rm_minus.size()>0)
{
for (int j=1; j<nTimes; j++)
{
String rbm = "";
for (Integer i : rm_minus)
rbm += " " + xij(i,j-1,nTimes,false);
String rbp = " ";
for (Integer i : rm_plus)
rbp += " -1 " + wrbj(r, b, j-1, nBerths, nTimes, nVars, isMooring, true, false) + " " + xij(i, j-1, nTimes, true) + " " + xij(i, j, nTimes, false);
String clause = "1 "+wrbj(r, b, j, nBerths, nTimes, nVars, isMooring, true, true) + " -1 " + wrbj(r, b, j-1, nBerths, nTimes, nVars, isMooring, true, true) + rbm + rbp;
writer.write(clause + " = 0 ;\n");
nCons++;
}
}
}
}
// Нахождение всего там, где надо
writer.write("* Correct Places of Moving Resources \n");
for (OperationTemplate tp : templates)
{
int i = templates.indexOf(tp);
List<Integer> resources_f = new ArrayList<Integer>(); // Свободные
List<Integer> resources_m = new ArrayList<Integer>(); // Пришвартованные
Berth b = tp.getStartLocation();
boolean isAlways = true;
if (tp instanceof LoadingTemplate)
{
int idx = vss.indexOf(((LoadingTemplate)tp).getLoader());
if (b.getIsRaid() || (!isMooring))
resources_f.add(idx);
else
resources_m.add(idx);
for (MovingObject mo : ((LoadingTemplate)tp).getResources())
resources_f.add(vss.indexOf(mo));
}
if (tp instanceof MooringTemplate)
{
int idx = vss.indexOf(((MooringTemplate)tp).getMoorer());
if (((MooringTemplate)tp).isDirect())
resources_f.add(idx);
else
resources_m.add(idx);
for (Tow tow : ((MooringTemplate)tp).getResources())
resources_f.add(vss.indexOf(tow));
isAlways = false;
}
if (tp instanceof MovingTemplate)
{
int idx = vss.indexOf(((MovingTemplate)tp).getMover());
resources_f.add(idx);
for (Tow tow : ((MovingTemplate)tp).getResources())
resources_f.add(vss.indexOf(tow));
isAlways = false;
}
int bi = berths.indexOf(b);
for (int j=0; j<nTimes; j++)
{
String clause = "1 "+xij(i, j, nTimes, false) + " +1 " + xij(i, j, nTimes, true);
if (!isAlways && j>0)
clause = "-1 " + xij(i, j, nTimes, true) + " " + xij(i, j-1, nTimes, false) + " +1 " + xij(i, j, nTimes, true) +" " + xij(i, j-1, nTimes, false);
String clause_f = clause;
String clause_m = clause;
if (resources_f.size()>0)
{
for (Integer r : resources_f)
clause_f += " " + wrbj(r, bi, j, nBerths, nTimes, nVars, isMooring, false, true);
if (isAlways || j==0)
writer.write(clause_f + " >= 1 ;\n");
else
writer.write(clause_f + " >= 0 ;\n");
nCons++;
}
if (resources_m.size()>0)
{
for (Integer r : resources_m)
clause_m += " " + wrbj(r, bi, j, nBerths, nTimes, nVars, isMooring, true, true);
if (isAlways || j==0)
writer.write(clause_m + " >= 1 ;\n");
else
writer.write(clause_m + " >= 0 ;\n");
nCons++;
}
}
}
// Состояние загрузки судов
writer.write("* Ship Loading States \n");
// Тут пок огшраничение только на местонахождение транспортных судов
for (TransportShip s : ships)
{
StorageState state1 = null;
StorageState state2 = null;
MovingObjectState eState = null;
for (StorageState state : storageInitialState)
if (state.getStorage().equals(s))
{
state1 = (StorageState)state;
break;
}
for (StorageState state : storageEndState)
if (state.getStorage().equals(s))
{
state2 = (StorageState)state;
break;
}
for (MovingObjectState state : vesselEndState)
if (state.getVessel().equals(s))
{
eState = (MovingObjectState)state;
break;
}
for (Cargo c : cargoes)
{
int start = 0;
int finish = 0;
if (state1!=null && state1.getCargo().equals(c))
start = (int)state1.getCargoState();
if (state2!=null && state2.getCargo().equals(c))
finish = (int)state2.getCargoState();
List<Integer> vc = new ArrayList<Integer>();
List<Integer> intens = new ArrayList<Integer>();
for (OperationTemplate tp : templates)
if (tp instanceof LoadingTemplate)
{
int i = templates.indexOf(tp);
LoadingTemplate ltp = (LoadingTemplate)tp;
if (ltp.getLoader().equals(s) && ltp.getStorage().getCargo().equals(c))
{
vc.add(i);
intens.add((int)ltp.getIntensity());
}
}
for (int j=0; j<nTimes; j++)
{
String clause = "";
double sumplus = 0.0;
double summinus = 0.0;
for (int k=0; k<=j; k++)
{
int counter = 0;
for (int vci : vc)
{
sumplus += Math.max(intens.get(counter), 0.0);
summinus += Math.min(intens.get(counter), 0.0);
if (intens.get(counter)>0)
clause += " +" + intens.get(counter) + " " +xij(vci, k, nTimes, true);
if (intens.get(counter)<0)
clause += " " + intens.get(counter) + " " +xij(vci, k, nTimes, true);
counter++;
}
}
if (clause.length()>0)
{
if (j<nTimes-1)
{
// Положительность груза на судне в каждый момент времени
if (summinus<-start)
{
writer.write(clause + " >= -" + start + " ;\n");
nCons++;
}
// Ограничение на грузоподъемность судна
int maxLoad = (int)(s.getCargoMax()+1.0);
if (sumplus > (maxLoad - start))
{
writer.write(clause + " <= " + (maxLoad - start) + " ;\n");
nCons++;
}
}
else
{
// Конечное состояние судна
writer.write(clause + " = " + (finish - start) + " ;\n");
nCons++;
}
}
}
}
// Нахождение всех судов в конечный момент в нужном состоянии
writer.write("* Final Ship Locations \n");
if (eState!=null)
{
int vi = vss.indexOf(s);
for (Berth berth : berths)
{
int bi = berths.indexOf(berth);
String clause = " +1 "+wrbj(vi, bi, nTimes-1, nBerths, nTimes, nVars, isMooring, false, true) + " = ";
if (eState.getLocation().equals(berth))
{
writer.write(clause + "1 ;\n");
nCons++;
}
else
{
writer.write(clause + "0 ;\n");
nCons++;
}
}
}
}
// Невозможность одновременного нахождения двух пришвартованных судов у одного причала
writer.write("* No more than 1 vessel moored to berth \n");
if (isMooring)
{
for (Berth b : berths)
{
if (b.getIsRaid())
continue;
int bdx = berths.indexOf(b);
for (int j=1; j<nTimes; j++)
{
String clause = "";
for (TransportShip v : ships)
{
int rdx = vss.indexOf(v);
clause += " +1 " + wrbj(rdx, bdx, j, nBerths, nTimes, nVars, isMooring, true, true);
}
writer.write(clause + " <= 1 ;\n");
nCons++;
}
}
}
writer.flush();
writer.close();
FileReader reader = new FileReader("temp");
writer = new FileWriter(fileName, false);
writer.write("* #variable= " + (nBefore + nAfter) + " #constraint= " + nCons + "\n");
BufferedReader breader = new BufferedReader(reader);
String line;
while ((line = breader.readLine()) != null) {
writer.write(line+"\n");
}
breader.close();
writer.flush();
writer.close();
return nOperations;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package inport;
/**
*
* @author topazh_ag
*/
public class Tow extends MovingObject {
public Tow(int id, String name) {
super(id, name);
}
public Tow() {
}
@Override
public String toString() {
return getId() + ";" + getName() + ";1000000";
}
public Tow(String s) {
super(s);
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package inport;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author topazh_ag
*/
public abstract class TowUsingTemplate extends OperationTemplate {
private List<Tow> resources;
private double duration;
/**
* Get the value of resources
*
* @return the value of resources
*/
public List<Tow> getResources() {
return resources;
}
/**
* Set the value of resources
*
* @param resources new value of resources
*/
public void setResources(List<Tow> resources) {
this.resources = resources;
}
/**
* Get the value of duration
*
* @return the value of duration
*/
public double getDuration() {
return duration;
}
/**
* Set the value of duration
*
* @param duration new value of duration
*/
public void setDuration(double duration) {
this.duration = duration;
}
public TowUsingTemplate(double duration, int id, Berth startLocation) {
super(id, startLocation);
this.resources = new ArrayList<>();
this.duration = duration;
}
public TowUsingTemplate() {
this.resources = new ArrayList<>();
}
@Override
public String toString() {
String res = "";
boolean first = true;
for(Tow eq : resources)
{
if (!first)
res += "," + eq.getId();
else
res += eq.getId();
first = false;
}
return getId() + ";" + "tut" + ";" + ";[" + res +"];"+duration;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package inport;
/**
*
* @author topazh_ag
*/
public class TransportShip extends MovingObject {
private double cargoMax;
public double getCargoMax() {
return cargoMax;
}
public void setCargoMax(double cargoMax) {
this.cargoMax = cargoMax;
}
public TransportShip(int id, String name, double cargoMax) {
super(id, name);
this.cargoMax = cargoMax;
}
public TransportShip() {
}
@Override
public String toString() {
return getId() + ";" + getName() + ";" + cargoMax;
}
public TransportShip(String s) {
super(s);
String[] tokens = s.split(";");
cargoMax = Double.parseDouble(tokens[2].trim());
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment