Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
Conversion
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Vladislav Kiselev
Conversion
Commits
8af8ce77
Commit
8af8ce77
authored
Nov 13, 2018
by
Vladislav Kiselev
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Непрерывность швартовки, парсинг результата, упрощения
parent
b50e372f
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
140 additions
and
27 deletions
+140
-27
src/inport/ConversionUtil.java
src/inport/ConversionUtil.java
+92
-12
src/inport/Main.java
src/inport/Main.java
+35
-9
src/inport/ParserException.java
src/inport/ParserException.java
+7
-0
test/conversion_0.mzn
test/conversion_0.mzn
+6
-6
No files found.
src/inport/ConversionUtil.java
View file @
8af8ce77
...
...
@@ -2,9 +2,9 @@ package inport;
import
javafx.util.Pair
;
import
java.io.FileWriter
;
import
java.io.IOException
;
import
java.io.*
;
import
java.util.*
;
import
java.util.function.BiFunction
;
import
java.util.function.Function
;
public
class
ConversionUtil
{
...
...
@@ -177,6 +177,9 @@ public class ConversionUtil {
}
writer
.
write
(
"n_locations = "
+
locationNumberById
.
size
()
+
";\n"
);
BiFunction
<
Integer
,
Boolean
,
Integer
>
getLocNById
=
(
Integer
id
,
Boolean
isMoored
)
->
locationNumberById
.
get
(
new
Pair
<>(
id
,
isMoored
));
ArrayList
<
MovingObject
>
movingObjects
=
new
ArrayList
<>();
Map
<
Integer
,
Integer
>
mObjNumberById
=
new
TreeMap
<>();
for
(
MovingObject
obj
:
task
.
getShips
())
{
...
...
@@ -216,19 +219,18 @@ public class ConversionUtil {
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
);
arrivalOp
.
get
(
n
).
get
(
getLocNById
.
apply
(
op
.
getDestination
().
getId
(),
false
)).
add
(
i
);
departureOp
.
get
(
n
).
get
(
getLocNById
.
apply
(
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
)
));
initialStates
.
set
(
mObjToN
.
apply
(
state
.
getVessel
()),
getLocNById
.
apply
(
state
.
getLocation
().
getId
(),
false
));
}
writeArray
(
writer
,
"initial_m_obj_loc"
,
initialStates
,
(
Integer
p
)
->
p
+
1
);
writer
.
write
(
"\n"
);
...
...
@@ -254,27 +256,31 @@ public class ConversionUtil {
writeArray
(
writer
,
"bw_fin"
,
bw_fin
);
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
);
operationsDuration
.
set
(
i
,
(
int
)
Math
.
ceil
(
op
.
getDuration
()));
isMovingObj
.
add
(
true
);
}
else
if
(
operationTemplates
.
get
(
i
)
instanceof
MooringTemplate
)
{
MooringTemplate
op
=
(
MooringTemplate
)
operationTemplates
.
get
(
i
);
operationsDuration
.
set
(
i
,
(
int
)
Math
.
ceil
(
op
.
getDuration
()));
isMovingObj
.
add
(
true
);
}
else
{
isMovingObj
.
add
(
false
);
}
}
writeArray
(
writer
,
"operations_duration"
,
operationsDuration
);
writeArray
(
writer
,
"is_
moving
_operation"
,
isMovingObj
);
writeArray
(
writer
,
"is_
continuous
_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
)
));
finalStates
.
set
(
mObjToN
.
apply
(
state
.
getVessel
()),
getLocNById
.
apply
(
state
.
getLocation
().
getId
(),
false
));
}
writeArray
(
writer
,
"final_m_obj_loc"
,
finalStates
,
(
Integer
p
)
->
p
+
1
);
}
...
...
@@ -293,7 +299,7 @@ public class ConversionUtil {
s
.
add
(
mObjToN
.
apply
(
obj
)
+
1
);
}
operationsResources
.
set
(
i
,
s
);
operationsStartLoc
.
set
(
i
,
locationNumberById
.
get
(
new
Pair
<>(
op
.
getStartLocation
().
getId
(),
false
)
)
+
1
);
operationsStartLoc
.
set
(
i
,
getLocNById
.
apply
(
op
.
getStartLocation
().
getId
(),
false
)
+
1
);
}
// TODO швартовка, погрузка.
}
...
...
@@ -315,4 +321,78 @@ public class ConversionUtil {
}
}
}
static
public
void
resolveMiniZincResults
(
TaskCase
task
,
String
fileName
)
throws
IOException
,
ParserException
{
List
<
Operation
>
operations
=
null
;
Integer
result
=
null
;
try
(
FileInputStream
fstream
=
new
FileInputStream
(
fileName
))
{
BufferedReader
br
=
new
BufferedReader
(
new
InputStreamReader
(
fstream
));
String
line
;
while
(((
line
=
br
.
readLine
())
!=
null
))
{
line
=
line
.
trim
();
if
(
line
.
equals
(
""
))
{
continue
;
}
String
regexpr
=
"^\\[((true)|(false))(,\\s((true)|(false)))*]\\z"
;
if
(
line
.
matches
(
regexpr
))
{
if
(
operations
!=
null
)
{
continue
;
}
String
[]
rawTokens
=
line
.
split
(
"[\\s\\[\\],]"
);
ArrayList
<
String
>
tokens
=
new
ArrayList
<>();
for
(
String
str
:
rawTokens
)
{
if
(!
str
.
isEmpty
())
{
tokens
.
add
(
str
);
}
}
final
int
n_intervals
=
(
int
)
task
.
getPlanningInterval
();
if
(
tokens
.
size
()
!=
(
n_intervals
+
2
)
*
task
.
getTemplates
().
size
())
{
throw
new
ParserException
(
"Invalid length of \"op_status\""
);
}
operations
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
task
.
getTemplates
().
size
();
i
++)
{
int
duration
=
0
;
for
(
int
j
=
0
;
j
<
n_intervals
+
2
;
j
++)
{
if
(
tokens
.
get
(
i
*
(
n_intervals
+
2
)
+
j
).
equals
(
"true"
))
{
duration
++;
}
else
if
(
duration
!=
0
)
{
Operation
op
=
new
Operation
();
op
.
setStart
(
j
-
duration
-
1
);
op
.
setDuration
(
duration
);
op
.
setTemplate
(
task
.
getTemplates
().
get
(
i
));
operations
.
add
(
op
);
duration
=
0
;
}
}
}
continue
;
}
if
(
line
.
matches
(
"\\d+"
))
{
if
(
result
!=
null
)
{
continue
;
}
result
=
Integer
.
parseInt
(
line
);
continue
;
}
break
;
}
if
(
operations
==
null
)
{
throw
new
ParserException
(
"No \"op_status\" in input"
);
}
if
(
result
==
null
)
{
throw
new
ParserException
(
"No result in input"
);
}
task
.
setSolution
(
operations
);
task
.
setSolution_result
(
result
);
}
}
}
src/inport/Main.java
View file @
8af8ce77
...
...
@@ -3,19 +3,45 @@ package inport;
import
java.io.IOException
;
public
class
Main
{
public
static
void
main
(
String
[]
args
)
{
if
(
args
.
length
<
2
)
{
if
(
args
.
length
<
1
)
{
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
());
String
type
=
args
[
0
];
switch
(
type
)
{
case
"to_MiniZinc"
:
{
String
input
=
args
[
1
];
String
output
=
args
[
2
];
TaskCase
task
=
new
TaskCase
();
try
{
task
.
deserialize
(
input
);
ConversionUtil
.
portToMiniZinc
(
task
,
output
);
}
catch
(
IOException
ex
)
{
System
.
out
.
println
(
ex
.
getMessage
());
}
break
;
}
case
"resolve_result"
:
{
String
input
=
args
[
1
];
String
fileWIthResult
=
args
[
2
];
String
output
=
args
[
3
];
try
{
TaskCase
task
=
new
TaskCase
();
task
.
deserialize
(
input
);
ConversionUtil
.
resolveMiniZincResults
(
task
,
fileWIthResult
);
task
.
serialize
(
output
);
}
catch
(
IOException
|
ParserException
ex
)
{
System
.
out
.
println
(
ex
.
getMessage
());
}
break
;
}
default
:
System
.
out
.
println
(
"Unknown type \""
+
type
+
"\""
);
}
}
}
src/inport/ParserException.java
0 → 100644
View file @
8af8ce77
package
inport
;
public
class
ParserException
extends
Exception
{
ParserException
(
String
mess
)
{
super
(
mess
);
}
}
test/conversion_0.mzn
View file @
8af8ce77
...
...
@@ -42,13 +42,13 @@ constraint forall (op in 1..n_operations, j in {0, n_intervals + 1})
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
;
array
[
1
..
n_operations
]
of
var
bool
:
is_
continuous
_operation
;
% TODO
%constraint forall (i in 1..n_operations) (
% if (is_
moving
_operation[i]) then (
% if (is_
continuous
_operation[i]) then (
% forall (j in 1..n_intervals) (
% let {var int : len = operations_duration[i]} in
% if (j + len > n_intervals + 1) then
...
...
@@ -69,7 +69,7 @@ array [1..n_operations] of var bool : is_moving_operation;
constraint
forall
(
i
in
1
..
n_operations
)
(
if
(
is_
moving
_operation
[
i
])
then
(
if
(
is_
continuous
_operation
[
i
])
then
(
let
{
var
int
:
len
=
operations_duration
[
i
]}
in
(
forall
(
j
in
1
..
(
n_intervals
-
len
+
1
))
(
(
op_start
[
i
,
j
]
==
1
)
->
(
...
...
@@ -131,8 +131,8 @@ constraint forall (t in 1..n_intervals) (
solve
minimize
sum
(
is_not_terminated
);
output
[
"res = "
,
show
(
sum
(
is_not_terminated
)),
"\n
\n"
,
"op_status = "
,
show
(
op_status
),
"\n\n"
,
output
[
show
(
sum
(
is_not_terminated
)),
"
\n"
,
show
(
op_status
),
"\n\n"
,
"m_obj_loc = "
,
show
(
m_obj_loc
),
"\n\n"
,
"op_fin = "
,
show
(
op_fin
),
"\n\n"
,
"op_start = "
,
show
(
op_start
),
"\n\n"
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment