Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Melodic
melodic-upperware
Commits
8ab1f58e
Commit
8ab1f58e
authored
May 07, 2020
by
Marta Różańska
Browse files
fixes after review
parent
c61bb742
Changes
12
Hide whitespace changes
Inline
Side-by-side
zpp-solver/cp-parser/src/main/java/eu/melodic/upperware/cp_wrapper/utility_provider/implementations/UtilityProviderFromFileFactory.java
View file @
8ab1f58e
...
...
@@ -16,9 +16,9 @@ public class UtilityProviderFromFileFactory implements UtilityProviderFactory {
private
PenaltyFunctionProperties
penaltyFunctionProperties
;
private
UtilityGeneratorProperties
utilityGeneratorProperties
;
private
JWTService
jwtService
;
NodeCandidates
nodeCandidates
;
String
camelModelFilePath
;
String
cpProblemFilePath
;
private
NodeCandidates
nodeCandidates
;
private
String
camelModelFilePath
;
private
String
cpProblemFilePath
;
@Override
public
UtilityProvider
create
()
{
...
...
zpp-solver/cp-parser/src/main/java/eu/melodic/upperware/cp_wrapper/utils/expression_evaluator/ExpressionEvaluator.java
View file @
8ab1f58e
...
...
@@ -18,7 +18,7 @@ public class ExpressionEvaluator {
For instance if precision is set to 0.01,
1 and 1.005 are considered equal.
*/
p
ublic
static
final
double
PRECISION
=
0.1
;
p
rivate
static
final
double
PRECISION
=
0.1
;
public
static
double
getValueOfNumericInterface
(
NumericValueUpperware
value
)
{
if
(
value
instanceof
IntegerValueUpperware
)
{
...
...
@@ -70,7 +70,7 @@ public class ExpressionEvaluator {
return
evaluateComparator
(
comparator
,
leftExpValue
,
rightExpValue
);
}
p
ublic
static
boolean
evaluateComparator
(
ComparatorEnum
comparator
,
double
argLeft
,
double
argRight
)
{
p
rivate
static
boolean
evaluateComparator
(
ComparatorEnum
comparator
,
double
argLeft
,
double
argRight
)
{
switch
(
comparator
)
{
case
GREATER_THAN:
return
argLeft
>
argRight
;
...
...
zpp-solver/cp-parser/src/main/java/eu/melodic/upperware/cp_wrapper/utils/nc_json_parser/
nc
Parser.java
→
zpp-solver/cp-parser/src/main/java/eu/melodic/upperware/cp_wrapper/utils/nc_json_parser/
NC
Parser.java
View file @
8ab1f58e
...
...
@@ -12,7 +12,7 @@ import java.util.*;
import
java.util.stream.Collectors
;
import
java.util.stream.IntStream
;
public
class
nc
Parser
{
public
class
NC
Parser
{
private
Gson
gson
=
new
Gson
();
private
CacheService
<
NodeCandidates
>
filecacheService
=
new
FilecacheService
();
...
...
zpp-solver/genetic-solver/src/main/java/eu/melodic/upperware/genetic_solver/GeneticSolverCoordinator.java
View file @
8ab1f58e
...
...
@@ -105,7 +105,7 @@ public class GeneticSolverCoordinator {
UtilityGeneratorApplication
utilityGenerator
=
new
UtilityGeneratorApplication
(
applicationId
,
cpResourcePath
,
false
,
nodeCandidates
,
utilityGeneratorProperties
,
melodicSecurityProperties
,
jwtService
,
penaltyFunctionProperties
);
B
oolean
solutionFeasible
=
solve
(
cp
,
utilityGenerator
,
timeLimit
);
b
oolean
solutionFeasible
=
solve
(
cp
,
utilityGenerator
,
timeLimit
);
if
(!
solutionFeasible
)
{
log
.
info
(
"Problem is infeasible"
);
...
...
zpp-solver/mcts-solver/src/main/java/eu/melodic/upperware/mcts_solver/MCTSSolverController.java
View file @
8ab1f58e
...
...
@@ -24,7 +24,7 @@ public class MCTSSolverController {
String
cpModelPath
=
request
.
getCpProblemFilePath
();
String
nodeCandidatesFilePath
=
request
.
getNodeCandidatesFilePath
();
int
seconds
=
request
.
getTimeLimit
();
log
.
info
(
"Received constraintProblemSolutionFromFile request: \n"
+
camelModelFilePath
+
" \n"
+
cpModelPath
);
log
.
info
(
"Received constraintProblemSolutionFromFile request:
{} \n{}
\n"
,
camelModelFilePath
,
cpModelPath
);
MCTSSolverCoordinator
.
generateCPSolutionFromFile
(
camelModelFilePath
,
cpModelPath
,
nodeCandidatesFilePath
,
seconds
);
log
.
info
(
"Sleeping..."
);
...
...
@@ -42,8 +42,7 @@ public class MCTSSolverController {
String
notificationUri
=
request
.
getNotificationURI
();
String
requestUuid
=
request
.
getWatermark
().
getUuid
();
int
seconds
=
request
.
getTimeLimit
();
log
.
info
(
"Received request: "
+
applicationId
+
" "
+
cdoResourcePath
+
" "
+
notificationUri
+
" "
+
requestUuid
);
log
.
info
(
"Received request: {}, {}, {}, {}"
,
applicationId
,
cdoResourcePath
,
notificationUri
,
requestUuid
);
MCTSSolverCoordinator
.
generateCPSolution
(
applicationId
,
cdoResourcePath
,
notificationUri
,
requestUuid
,
seconds
);
log
.
info
(
"Sleeping..."
);
}
...
...
zpp-solver/mcts-solver/src/main/java/eu/melodic/upperware/mcts_solver/solver/mcts/tree/Tree.java
View file @
8ab1f58e
...
...
@@ -25,7 +25,8 @@ public abstract class Tree {
public
Solution
run
(
int
iterations
)
{
return
IntStream
.
range
(
0
,
iterations
)
.
mapToObj
(
i
->
runIteration
())
.
max
(
Solution:
:
compareTo
).
get
();
.
max
(
Solution:
:
compareTo
)
.
orElseThrow
(()
->
new
IllegalStateException
(
"There was an error during comparing solutions"
));
}
// Back propagates calculated solution on path from leaf to root.
...
...
zpp-solver/mcts-solver/src/main/java/eu/melodic/upperware/mcts_solver/solver/mcts/tree_impl/policy/CheapestPolicyImpl.java
View file @
8ab1f58e
...
...
@@ -41,7 +41,10 @@ public class CheapestPolicyImpl implements Policy {
public
CheapestPolicyImpl
(
MCTSWrapper
mctsWrapper
)
{
this
.
mctsWrapper
=
mctsWrapper
;
this
.
variables
=
mctsWrapper
.
getVariableDTOCollection
();
this
.
components
=
variables
.
stream
().
map
(
VariableDTO:
:
getComponentId
).
distinct
().
collect
(
Collectors
.
toList
());
this
.
components
=
variables
.
stream
()
.
map
(
VariableDTO:
:
getComponentId
)
.
distinct
()
.
collect
(
Collectors
.
toList
());
components
.
forEach
(
component
->
componentToVariables
.
put
(
component
,
getVariablesForComponent
(
component
)));
}
...
...
zpp-solver/mcts-solver/src/main/java/eu/melodic/upperware/mcts_solver/solver/utils/tree_printer/TreePrinter.java
View file @
8ab1f58e
...
...
@@ -3,6 +3,7 @@ package eu.melodic.upperware.mcts_solver.solver.utils.tree_printer;
import
eu.melodic.upperware.mcts_solver.solver.mcts.cp_wrapper.MCTSWrapper
;
import
eu.melodic.upperware.mcts_solver.solver.mcts.tree.Node
;
import
eu.melodic.upperware.mcts_solver.solver.mcts.tree_impl.NodeStatisticsImpl
;
import
org.apache.commons.lang.StringUtils
;
import
java.io.BufferedWriter
;
import
java.io.FileWriter
;
...
...
@@ -77,14 +78,14 @@ public class TreePrinter {
private
static
String
getOutgoingEdges
(
Node
root
)
{
if
(
root
.
getNodeStatistics
().
getDepth
()
>=
MAX_DEPTH
)
{
return
""
;
return
StringUtils
.
EMPTY
;
}
List
<
String
>
edges
=
root
.
getChildren
().
stream
()
.
sorted
(
getNodeComparator
().
reversed
())
.
limit
(
MAX_CHILDREN
)
.
map
(
child
->
edgeHash
(
root
,
child
))
.
collect
(
Collectors
.
toList
());
return
String
.
join
(
""
,
edges
);
return
String
.
join
(
StringUtils
.
EMPTY
,
edges
);
}
private
static
String
edgeHash
(
Node
parent
,
Node
child
)
{
...
...
@@ -94,7 +95,10 @@ public class TreePrinter {
private
static
void
saveTreeStructureGreedy
(
Node
root
,
String
treeFilePath
)
throws
IOException
{
BufferedWriter
writer
=
new
BufferedWriter
(
new
FileWriter
(
treeFilePath
));
List
<
Node
>
atMaxDepth
=
getNodesAtDepth
(
Math
.
min
(
MAX_DEPTH
,
getMaxTreeDepth
(
root
)),
root
).
stream
().
sorted
(
getNodeComparator
().
reversed
()).
limit
(
MAX_CHILDREN_AT_MAX_DEPTH
).
collect
(
Collectors
.
toList
());
List
<
Node
>
atMaxDepth
=
getNodesAtDepth
(
Math
.
min
(
MAX_DEPTH
,
getMaxTreeDepth
(
root
)),
root
).
stream
()
.
sorted
(
getNodeComparator
().
reversed
())
.
limit
(
MAX_CHILDREN_AT_MAX_DEPTH
)
.
collect
(
Collectors
.
toList
());
saveTreeStructureGreedy
(
atMaxDepth
,
writer
);
writer
.
close
();
}
...
...
@@ -103,10 +107,16 @@ public class TreePrinter {
boolean
reachedRoot
=
false
;
HashSet
<
Node
>
alreadySaved
=
new
HashSet
<>();
while
(!
reachedRoot
)
{
String
edges
=
atMaxDepth
.
stream
().
map
(
child
->
edgeHash
(
child
.
getParent
(),
child
)).
collect
(
Collectors
.
joining
(
""
));
String
edges
=
atMaxDepth
.
stream
()
.
map
(
child
->
edgeHash
(
child
.
getParent
(),
child
))
.
collect
(
Collectors
.
joining
(
""
));
alreadySaved
.
addAll
(
atMaxDepth
);
writer
.
write
(
edges
);
atMaxDepth
=
atMaxDepth
.
stream
().
map
(
Node:
:
getParent
).
distinct
().
filter
(
node
->
node
.
getParent
()!=
null
&&
!
alreadySaved
.
contains
(
node
)).
collect
(
Collectors
.
toList
());
atMaxDepth
=
atMaxDepth
.
stream
()
.
map
(
Node:
:
getParent
)
.
distinct
()
.
filter
(
node
->
node
.
getParent
()!=
null
&&
!
alreadySaved
.
contains
(
node
))
.
collect
(
Collectors
.
toList
());
if
(
atMaxDepth
.
size
()
==
0
||
atMaxDepth
.
get
(
0
).
getParent
()
==
null
)
{
reachedRoot
=
true
;
}
...
...
@@ -117,12 +127,18 @@ public class TreePrinter {
List
<
Node
>
atDepth
=
Arrays
.
asList
(
root
);
int
maxDepth
=
depth
;
while
(
depth
!=
0
)
{
atDepth
=
atDepth
.
stream
().
map
(
Node:
:
getChildren
).
flatMap
(
Collection:
:
stream
).
collect
(
Collectors
.
toList
());
atDepth
=
atDepth
.
stream
()
.
map
(
Node:
:
getChildren
)
.
flatMap
(
Collection:
:
stream
)
.
collect
(
Collectors
.
toList
());
depth
--;
}
/** add leaves at smaller depths **/
if
(
maxDepth
>
2
)
{
atDepth
.
addAll
(
getNodesAtDepth
(
maxDepth
-
1
,
root
).
stream
().
filter
(
node
->
node
.
getChildrenSize
()
==
0
).
collect
(
Collectors
.
toList
()));
atDepth
.
addAll
(
getNodesAtDepth
(
maxDepth
-
1
,
root
)
.
stream
()
.
filter
(
node
->
node
.
getChildrenSize
()
==
0
)
.
collect
(
Collectors
.
toList
()));
}
return
atDepth
;
}
...
...
@@ -131,7 +147,10 @@ public class TreePrinter {
int
depth
=
0
;
List
<
Node
>
atDepth
=
Arrays
.
asList
(
root
);
while
(!
atDepth
.
isEmpty
())
{
atDepth
=
atDepth
.
stream
().
map
(
Node:
:
getChildren
).
flatMap
(
Collection:
:
stream
).
collect
(
Collectors
.
toList
());
atDepth
=
atDepth
.
stream
()
.
map
(
Node:
:
getChildren
)
.
flatMap
(
Collection:
:
stream
)
.
collect
(
Collectors
.
toList
());
depth
++;
}
return
depth
-
1
;
...
...
@@ -153,7 +172,7 @@ public class TreePrinter {
private
static
String
getNodeValue
(
Node
node
,
MCTSWrapper
mctsWrapper
)
{
if
(
node
.
getNodeStatistics
().
getDepth
()
==
0
)
{
return
""
;
return
StringUtils
.
EMPTY
;
}
else
{
return
((
Integer
)
mctsWrapper
.
getValueFromIndex
(
node
.
getValue
(),
node
.
getNodeStatistics
().
getDepth
()
-
1
)).
toString
();
}
...
...
zpp-solver/node-candidates-solver/src/main/java/eu/melodic/upperware/nc_solver/NCSolverController.java
View file @
8ab1f58e
...
...
@@ -24,7 +24,7 @@ public class NCSolverController {
String
cpModelPath
=
request
.
getCpProblemFilePath
();
String
nodeCandidatesFilePath
=
request
.
getNodeCandidatesFilePath
();
int
seconds
=
request
.
getTimeLimit
();
log
.
info
(
"Received constraintProblemSolutionFromFile request: \n"
+
camelModelFilePath
+
" \n"
+
cpModelPath
);
log
.
info
(
"Received constraintProblemSolutionFromFile request:
{} \n{}
\n"
,
camelModelFilePath
,
cpModelPath
);
ncSolverCoordinator
.
generateCPSolutionFromFile
(
camelModelFilePath
,
cpModelPath
,
nodeCandidatesFilePath
,
seconds
);
log
.
info
(
"Sleeping..."
);
...
...
@@ -37,8 +37,7 @@ public class NCSolverController {
String
notificationUri
=
request
.
getNotificationURI
();
String
requestUuid
=
request
.
getWatermark
().
getUuid
();
int
seconds
=
request
.
getTimeLimit
();
log
.
info
(
"Received request: "
+
applicationId
+
" "
+
cdoResourcePath
+
" "
+
notificationUri
+
" "
+
requestUuid
);
log
.
info
(
"Received request: {}, {}, {}, {}"
,
applicationId
,
cdoResourcePath
,
notificationUri
,
requestUuid
);
ncSolverCoordinator
.
generateCPSolution
(
applicationId
,
cdoResourcePath
,
notificationUri
,
requestUuid
,
seconds
);
log
.
info
(
"Sleeping..."
);
}
...
...
zpp-solver/pt-solver/src/main/java/eu/melodic/upperware/pt_solver/PTSolverController.java
View file @
8ab1f58e
...
...
@@ -24,7 +24,7 @@ public class PTSolverController {
String
cpModelPath
=
request
.
getCpProblemFilePath
();
String
nodeCandidatesFilePath
=
request
.
getNodeCandidatesFilePath
();
int
seconds
=
request
.
getTimeLimit
();
log
.
info
(
"Received constraintProblemSolutionFromFile request:
\n"
+
camelModelFilePath
+
" \n"
+
cpModelPath
);
log
.
info
(
"Received constraintProblemSolutionFromFile request:
{} \n {} \n"
,
camelModelFilePath
,
cpModelPath
);
ptSolverCoordinator
.
generateCPSolutionFromFile
(
camelModelFilePath
,
cpModelPath
,
nodeCandidatesFilePath
,
seconds
);
log
.
info
(
"Sleeping..."
);
...
...
@@ -42,7 +42,7 @@ public class PTSolverController {
String
notificationUri
=
request
.
getNotificationURI
();
String
requestUuid
=
request
.
getWatermark
().
getUuid
();
int
seconds
=
request
.
getTimeLimit
();
log
.
info
(
"Received request:
"
+
applicationId
+
" "
+
cdoResourcePath
+
" "
+
notificationUri
+
" "
+
requestUuid
);
log
.
info
(
"Received request:
{} {} {} {}"
,
applicationId
,
cdoResourcePath
,
notificationUri
,
requestUuid
);
ptSolverCoordinator
.
generateCPSolution
(
applicationId
,
cdoResourcePath
,
notificationUri
,
requestUuid
,
seconds
);
log
.
info
(
"Sleeping..."
);
...
...
zpp-solver/pt-solver/src/main/java/eu/melodic/upperware/pt_solver/PTSolverCoordinator.java
View file @
8ab1f58e
...
...
@@ -105,8 +105,10 @@ public class PTSolverCoordinator {
ConstraintProblem
cp
=
getCPFromCDO
(
cpResourcePath
,
trans
)
.
orElseThrow
(()
->
new
IllegalStateException
(
"Constraint Problem does not exist in CDO"
));
List
<
UtilityGeneratorApplication
>
utilityGenerators
=
IntStream
.
range
(
0
,
numThreads
).
mapToObj
(
index
->
new
UtilityGeneratorApplication
(
applicationId
,
cpResourcePath
,
false
,
nodeCandidates
,
utilityGeneratorProperties
,
melodicSecurityProperties
,
jwtService
,
penaltyFunctionProperties
)).
collect
(
Collectors
.
toList
());
List
<
UtilityGeneratorApplication
>
utilityGenerators
=
IntStream
.
range
(
0
,
numThreads
)
.
mapToObj
(
index
->
new
UtilityGeneratorApplication
(
applicationId
,
cpResourcePath
,
false
,
nodeCandidates
,
utilityGeneratorProperties
,
melodicSecurityProperties
,
jwtService
,
penaltyFunctionProperties
))
.
collect
(
Collectors
.
toList
());
solve
(
cp
,
utilityGenerators
,
seconds
);
...
...
@@ -125,7 +127,7 @@ public class PTSolverCoordinator {
private
void
solve
(
ConstraintProblem
cp
,
List
<
UtilityGeneratorApplication
>
utilityGenerators
,
int
seconds
)
{
PTSolver
solver
=
new
PTSolver
(
minTemp
,
maxTemp
,
numThreads
,
cp
,
new
ParallelUtilityProviderImpl
(
utilityGenerators
));
Pair
<
List
<
VariableValueDTO
>,
Double
>
solution
=
solver
.
solve
(
new
MaxRuntime
(
seconds
,
TimeUnit
.
SECONDS
));
log
.
info
(
"Found solution with utility:
"
+
solution
.
getValue1
());
log
.
info
(
"Found solution with utility:
{}"
,
solution
.
getValue1
());
if
(
solution
.
getValue1
()
>
0.0
)
{
saveBestSolutionInCDO
(
cp
,
solution
.
getValue1
(),
solution
.
getValue0
());
...
...
zpp-solver/pt-solver/src/main/java/eu/melodic/upperware/pt_solver/pt_solver/ptcp_wrapper/PTCPWrapper.java
View file @
8ab1f58e
...
...
@@ -54,11 +54,11 @@ public class PTCPWrapper {
/*
Returns maximal value of variable @variable
*/
p
ublic
int
getMaxValue
(
int
variable
)
{
p
rivate
int
getMaxValue
(
int
variable
)
{
return
cpWrapper
.
getMaxDomainValue
(
variable
);
}
p
ublic
int
getMinValue
(
int
variable
)
{
p
rivate
int
getMinValue
(
int
variable
)
{
return
cpWrapper
.
getMinDomainValue
(
variable
);
}
...
...
Write
Preview
Supports
Markdown
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