Commit 8ab1f58e authored by Marta Różańska's avatar Marta Różańska
Browse files

fixes after review

parent c61bb742
......@@ -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() {
......
......@@ -18,7 +18,7 @@ public class ExpressionEvaluator {
For instance if precision is set to 0.01,
1 and 1.005 are considered equal.
*/
public static final double PRECISION = 0.1;
private 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);
}
public static boolean evaluateComparator(ComparatorEnum comparator, double argLeft, double argRight) {
private static boolean evaluateComparator(ComparatorEnum comparator, double argLeft, double argRight) {
switch (comparator) {
case GREATER_THAN:
return argLeft > argRight;
......
......@@ -12,7 +12,7 @@ import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class ncParser {
public class NCParser {
private Gson gson = new Gson();
private CacheService<NodeCandidates> filecacheService = new FilecacheService();
......
......@@ -105,7 +105,7 @@ public class GeneticSolverCoordinator {
UtilityGeneratorApplication utilityGenerator = new UtilityGeneratorApplication(applicationId, cpResourcePath, false, nodeCandidates, utilityGeneratorProperties,
melodicSecurityProperties, jwtService, penaltyFunctionProperties);
Boolean solutionFeasible = solve(cp, utilityGenerator, timeLimit);
boolean solutionFeasible = solve(cp, utilityGenerator, timeLimit);
if (!solutionFeasible) {
log.info("Problem is infeasible");
......
......@@ -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...");
}
......
......@@ -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.
......
......@@ -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)));
}
......
......@@ -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();
}
......
......@@ -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...");
}
......
......@@ -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...");
......
......@@ -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());
......
......@@ -54,11 +54,11 @@ public class PTCPWrapper {
/*
Returns maximal value of variable @variable
*/
public int getMaxValue(int variable) {
private int getMaxValue(int variable) {
return cpWrapper.getMaxDomainValue(variable);
}
public int getMinValue(int variable) {
private int getMinValue(int variable) {
return cpWrapper.getMinDomainValue(variable);
}
......
Supports Markdown
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