Commit 84bd0eb0 authored by Vladislav Kiselev's avatar Vladislav Kiselev

Начало

parent 7a9c7444
/*
* 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);
}
}
}
package inport;
public class ConversionException extends Exception {
ConversionException(String mess) {
super(mess);
}
}
package inport;
import javafx.util.Pair;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
import java.util.function.Function;
public class ConversionUtil {
static class PairComparator implements Comparator<Pair<Integer, Boolean>> {
public int compare(Pair<Integer, Boolean> p1, Pair<Integer, Boolean> p2) {
int res = p1.getKey().compareTo(p2.getKey());
if (res != 0) {
return res;
} else {
return p1.getValue().compareTo(p2.getValue());
}
}
}
static private void write2DArray(FileWriter writer,
String name,
ArrayList<ArrayList<ArrayList<Integer>>> operations) throws IOException {
writer.write(name + " = \n");
boolean isFirst0 = true;
for (ArrayList<ArrayList<Integer>> m1 : operations) {
if (isFirst0) {
isFirst0 = false;
writer.write(" [| ");
} else {
writer.write(" | ");
}
boolean isFirst1 = true;
for (ArrayList<Integer> m2 : m1) {
if (isFirst1) {
isFirst1 = false;
} else {
writer.write(", ");
}
writer.write("{");
boolean isFirst2 = true;
for (Integer val : m2) {
if (isFirst2) {
isFirst2 = false;
} else {
writer.write(", ");
}
writer.write((val + 1) + "");
}
writer.write("}");
}
writer.write("\n");
}
writer.write(" |];\n");
}
static private <T, E> void writeArray(FileWriter writer, String name, ArrayList<T> data, Function<T, E> f) throws IOException {
writer.write(name + " = [");
for (int i = 0; i < data.size(); i++) {
if (i != 0) {
writer.write(", ");
}
writer.write(f.apply(data.get(i)).toString() + "");
}
writer.write("];\n");
}
static private <T> void writeArray(FileWriter writer, String name, ArrayList<T> data) throws IOException {
writeArray(writer, name, data, (T val) -> val);
}
static private <T> String setToString(Set<T> s) {
StringBuilder res = new StringBuilder();
res.append("{");
boolean isFirst = true;
for (T t : s) {
if (isFirst) {
isFirst = false;
} else {
res.append(", ");
}
res.append(t.toString());
}
res.append("}");
return res.toString();
}
static private ArrayList<Integer> integerArray(int size, int initVal) {
ArrayList<Integer> res = new ArrayList<>();
for (int i = 0; i < size; i++) {
res.add(initVal);
}
return res;
}
static public void portToMiniZinc(TaskCase task, String fileName) throws IOException {
try(FileWriter writer = new FileWriter(fileName, false)) {
int n_intervals = (int)task.getPlanningInterval();
writer.write("n_intervals = " + n_intervals + ";\n");
writer.write("n_operations = " + task.getTemplates().size() + ";\n");
ArrayList<Berth> berths = new ArrayList<>(task.getBerths());
Map<Pair<Integer, Boolean>, Integer> locationNumberById = new TreeMap<>(new PairComparator());
for (Berth berth : berths) {
locationNumberById.put(new Pair<>(berth.getId(), false), locationNumberById.size());
locationNumberById.put(new Pair<>(berth.getId(), true), locationNumberById.size());
}
writer.write("n_locations = " + locationNumberById.size() + ";\n");
ArrayList<MovingObject> movingObjects = new ArrayList<>();
Map<Integer, Integer> mObjNumberById = new TreeMap<>();
for (MovingObject obj : task.getShips()) {
mObjNumberById.put(obj.getId(), movingObjects.size());
movingObjects.add(obj);
}
for (MovingObject obj : task.getTows()) {
mObjNumberById.put(obj.getId(), movingObjects.size());
movingObjects.add(obj);
}
writer.write("n_moving_obj = " + movingObjects.size() + ";\n");
writer.write("\n");
Function<MovingObject, Integer> mObjToN = (MovingObject obj) -> mObjNumberById.get(obj.getId());
ArrayList<OperationTemplate> operationTemplates = new ArrayList<>(task.getTemplates());
{
ArrayList<ArrayList<ArrayList<Integer>>> arrivalOp = new ArrayList<>();
ArrayList<ArrayList<ArrayList<Integer>>> departureOp = new ArrayList<>();
for (int i = 0; i < movingObjects.size(); i++) {
arrivalOp.add(new ArrayList<>());
departureOp.add(new ArrayList<>());
for (int j = 0; j < locationNumberById.size(); j++) {
arrivalOp.get(i).add(new ArrayList<>());
departureOp.get(i).add(new ArrayList<>());
}
}
for (int i = 0; i < operationTemplates.size(); i++) {
if (operationTemplates.get(i) instanceof MovingTemplate) {
MovingTemplate op = (MovingTemplate)operationTemplates.get(i);
ArrayList<Integer> movingObjN = new ArrayList<>();
for (MovingObject obj : op.getResources()) {
movingObjN.add(mObjToN.apply(obj));
}
movingObjN.add(mObjToN.apply(op.getMover()));
for (Integer n : movingObjN) {
arrivalOp.get(n).get(locationNumberById.get(new Pair<>(op.getDestination().getId(), false))).add(i);
departureOp.get(n).get(locationNumberById.get(new Pair<>(op.getStartLocation().getId(), false))).add(i);
}
}
}
write2DArray(writer, "arrival_op", arrivalOp);
write2DArray(writer, "departure_op", departureOp);
}
{ // Начальные положения объектов.
ArrayList<Integer> initialStates = integerArray(movingObjects.size(), 0);
for (MovingObjectState state : task.getVesselInitialState()) {
initialStates.set(mObjToN.apply(state.getVessel()), locationNumberById.get(new Pair<>(state.getLocation().getId(), false)) + 1);
}
writeArray(writer, "initial_m_obj_loc", initialStates);
}
// TODO окна погоды.
writer.write("\n");
{ // Непрерывность перемещения.
ArrayList<Integer> operationsDuration = integerArray(operationTemplates.size(), 0);
ArrayList<Boolean> isMovingObj = new ArrayList<>();
for (int i = 0; i < operationTemplates.size(); i++) {
if (operationTemplates.get(i) instanceof MovingTemplate) {
MovingTemplate op = (MovingTemplate)operationTemplates.get(i);
int duration = (int)Math.ceil(op.getDuration());
operationsDuration.set(i, duration);
isMovingObj.add(true);
} else {
isMovingObj.add(false);
}
}
writeArray(writer, "operations_duration", operationsDuration);
writeArray(writer, "is_moving_operation", isMovingObj);
}
{ // Конечные положения объектов.
ArrayList<Integer> finalStates = integerArray(movingObjects.size(), 0);
for (MovingObjectState state : task.getVesselEndState()) {
finalStates.set(mObjToN.apply(state.getVessel()), locationNumberById.get(new Pair<>(state.getLocation().getId(), false)));
}
writeArray(writer, "final_m_obj_loc", finalStates);
}
{ // Наличие всех ресурсов на месте.
ArrayList<Set<Integer>> operationsResources = new ArrayList<>();
ArrayList<Integer> operationsStartLoc = integerArray(operationTemplates.size(), 0);
for (int i = 0; i < operationTemplates.size(); i++) {
operationsResources.add(new TreeSet<>());
if (operationTemplates.get(i) instanceof MovingTemplate) {
MovingTemplate op = (MovingTemplate)operationTemplates.get(i);
Set<Integer> s = new TreeSet<>();
s.add(mObjToN.apply(op.getMover()) + 1);
for (MovingObject obj : op.getResources()) {
s.add(mObjToN.apply(obj) + 1);
}
operationsResources.set(i, s);
operationsStartLoc.set(i, locationNumberById.get(new Pair<>(op.getStartLocation().getId(), false)));
}
// TODO швартовка, погрузка.
}
writeArray(writer, "operations_start_loc", operationsStartLoc);
writeArray(writer, "operations_resources", operationsResources, ConversionUtil::setToString);
}
}
}
}
/*
* 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);
}
}
package inport;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
if (args.length < 2) {
System.out.println("To few arguments.");
return;
}
TaskCase task = new TaskCase();
try {
task.deserialize(args[0]);
// task.simpleMiniZincConversion(args[1]);
ConversionUtil.portToMiniZinc(task, args[1]);
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
/*
* 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());
}
}
This diff is collapsed.
/*
* 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());
}
}
n_intervals = 24;
n_operations = 16;
n_locations = 6;
n_moving_obj = 2;
arrival_op =
[| {2, 4}, {}, {1, 6}, {}, {3, 5}, {}
| {8, 10}, {}, {7, 12}, {}, {9, 11}, {}
|];
departure_op =
[| {1, 3}, {}, {2, 5}, {}, {4, 6}, {}
| {7, 9}, {}, {8, 11}, {}, {10, 12}, {}
|];
initial_m_obj_loc = [1, 1];
operations_duration = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0];
is_moving_operation = [true, true, true, true, true, true, true, true, true, true, true, true, false, false, false, false];
final_m_obj_loc = [0, 0];
operations_start_loc = [0, 2, 0, 4, 2, 4, 0, 2, 0, 4, 2, 4, 0, 0, 0, 0];
operations_resources = [{1}, {1}, {1}, {1}, {1}, {1}, {2}, {2}, {2}, {2}, {2}, {2}, {}, {}, {}, {}];
include "globals.mzn";
int : n_intervals;
int : n_operations;
int : n_locations;
int : n_moving_obj;
array [1..n_operations, 0..(n_intervals + 1)] of var bool : op_status;
array [1..n_operations, 0..(n_intervals + 1)] of var bool : op_start;
array [1..n_operations, 0..(n_intervals + 1)] of var bool : op_fin;
% 0 - локация не определена.
array [1..n_moving_obj, 0..(n_intervals + 1)] of var 0..n_locations : m_obj_loc; % Moving object location.
array [1..n_moving_obj, 1..n_locations] of set of 1..n_operations : arrival_op;
array [1..n_moving_obj, 1..n_locations] of set of 1..n_operations : departure_op;
% Определение m_obj_loc.
constraint forall (i in 1..n_moving_obj, j in 1..(n_intervals + 1))
((m_obj_loc[i, j] != 0) -> (
(m_obj_loc[i, j] == m_obj_loc[i, j - 1] /\ (forall (k in departure_op[i, m_obj_loc[i, j]]) (op_status[k, j - 1] == 0)))
\/
(not (forall (k in arrival_op[i, m_obj_loc[i, j]]) (op_fin[i, j - 1] == 0)))
));
% Начальное состояние.
array [1..n_moving_obj] of var 0..n_locations : initial_m_obj_loc;
constraint forall (i in 1..n_moving_obj) (m_obj_loc[i, 0] = initial_m_obj_loc[i]);
% Конечное состояние.
array [1..n_moving_obj] of var 0..n_locations : final_m_obj_loc;
constraint forall (i in 1..n_moving_obj) (
(final_m_obj_loc[i] != 0) ->
(m_obj_loc[i, n_intervals + 1] = final_m_obj_loc[i])
);
constraint forall (i in 1..n_operations, j in {0, n_intervals + 1}) (op_status[i, j] == 0); % Краевые значения.
% Определение op_start и op_fin.
constraint forall (i in 1..n_operations) (op_start[i, 0] == 0 /\ op_fin[i, n_intervals + 1] == 0);
constraint forall (i in 1..n_operations, j in 1..(n_intervals + 1)) (op_start[i, j] = op_status[i, j] /\ not op_status[i, j - 1]);
constraint forall (i in 1..n_operations, j in 1..n_intervals ) (op_fin [i, j] = op_status[i, j] /\ not op_status[i, j + 1]);
% Непрерывность перемещения.
array [1..n_operations] of var int : operations_duration;
array [1..n_operations] of var bool : is_moving_operation;
constraint forall (i in 1..n_operations) (
if (is_moving_operation[i]) then (
forall (j in 1..n_intervals) (
if (j + operations_duration[i] > n_intervals + 1) then
op_start[i, j] = 0
else (
(op_start[i, j] == 1) -> (
(forall (k in 1..(operations_duration[i] - 1))
(op_status[i, j + k] == 1)
) /\
(op_status[i, j + operations_duration[i]] == 0)
)
)
endif
)
) else true
endif
);
% Наличие всех ресурсов на месте.
array [1..n_operations] of set of 1..n_moving_obj : operations_resources;
array [1..n_operations] of 0..n_locations : operations_start_loc;
constraint forall (op in 1..n_operations, j in 1..n_intervals) (
forall (obj in operations_resources[op]) (
(op_start[op, j]) -> (m_obj_loc[obj, j] == operations_start_loc[op])
)
);
solve satisfy;
output ["op_status = ", show(op_status), "\n"];
minizinc --solver Gecode conversion_0.mzn conversion_0.dzn
:: minizinc --solver Chuffed conversion_0.mzn conversion_0.data
Cargoes
0;LNG;0.0
Berths
1;Raid
2;Pier 1
3;Pier 2
Storages
4;Storage 1;0;10000.0
Bunkers
Tows
Loading Equipments
Transport Ships
5;Ship 1;2000.0
6;Ship 2;2000.0
Templates
7;mov;[];5;1;2;[];1.0
8;mov;[];5;2;1;[];1.0
9;mov;[];5;1;3;[];1.0
10;mov;[];5;3;1;[];1.0
11;mov;[];5;2;3;[];1.0
12;mov;[];5;3;2;[];1.0
13;mov;[];6;1;2;[];1.0
14;mov;[];6;2;1;[];1.0
15;mov;[];6;1;3;[];1.0
16;mov;[];6;3;1;[];1.0
17;mov;[];6;2;3;[];1.0
18;mov;[];6;3;2;[];1.0
19;loa;[];4;0;5;2;[];100.0
20;loa;[];4;0;5;3;[];50.0
21;loa;[];4;0;6;2;[];100.0
22;loa;[];4;0;6;3;[];50.0
Cargo Flows
Initial Vessel State
5;1
6;1
Initial Storage State
0;5;0.0
0;6;0.0
0;4;10000.0
Final Vessel State
5;1
6;1
Final Storage State
0;5;1000.0
0;6;1000.0
Task Properties
24.0;0
Solution
16.0
7; R; 1.0; 1.0
10; R; 16.0; 1.0
11; R; 9.0; 1.0
14; R; 16.0; 1.0
15; R; 1.0; 1.0
18; R; 8.0; 1.0
19; R; 2.0; 7.0
20; R; 10.0; 6.0
21; R; 9.0; 7.0
22; R; 2.0; 6.0
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