Commit 9ee8ff1f authored by Marta Różańska's avatar Marta Różańska
Browse files

fixes in packaging

parent aa2d7859
......@@ -21,7 +21,6 @@ import lombok.extern.slf4j.Slf4j;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
@Slf4j
public class UtilityGeneratorApplication {
......@@ -29,31 +28,31 @@ public class UtilityGeneratorApplication {
private UtilityFunctionEvaluator utilityFunctionEvaluator;
public UtilityGeneratorApplication(String camelModelFilePath, String cpModelFilePath, boolean readFromFile, NodeCandidates nodeCandidates, UtilityGeneratorProperties properties,
MelodicSecurityProperties melodicSecurityProperties, JWTService jwtService, PenaltyFunctionProperties penaltyFunctionProperties) {
MelodicSecurityProperties melodicSecurityProperties, JWTService jwtService, PenaltyFunctionProperties penaltyFunctionProperties) {
log.info("Creating of the Utility Generator");
utilityFunctionEvaluator = new UtilityFunctionEvaluator(camelModelFilePath, cpModelFilePath, readFromFile, nodeCandidates, properties, melodicSecurityProperties, penaltyFunctionProperties, jwtService);
}
public UtilityGeneratorApplication(String camelModelFilePath, String cpModelFilePath, boolean readFromFile, NodeCandidates nodeCandidates, UtilityGeneratorProperties properties,
MelodicSecurityProperties melodicSecurityProperties, JWTService jwtService, PenaltyFunctionProperties penaltyFunctionProperties,
List<Map.Entry<TemplateProvider.AvailableTemplates, Double>> utilityComponents) {
MelodicSecurityProperties melodicSecurityProperties, JWTService jwtService, PenaltyFunctionProperties penaltyFunctionProperties,
List<Map.Entry<TemplateProvider.AvailableTemplates, Double>> utilityComponents) {
log.info("Creating template Utility Generator");
checkWeightsOfUtilityComponents(utilityComponents);
utilityFunctionEvaluator = new UtilityFunctionEvaluator(camelModelFilePath, cpModelFilePath, readFromFile, nodeCandidates, properties,
utilityFunctionEvaluator = new UtilityFunctionEvaluator(camelModelFilePath, cpModelFilePath, readFromFile, nodeCandidates, properties,
melodicSecurityProperties, penaltyFunctionProperties, jwtService, utilityComponents);
}
public UtilityGeneratorApplication(String cpModelFilePath, NodeCandidates nodeCandidates, List<Map.Entry<TemplateProvider.AvailableTemplates, Double>> utilityComponents) {
log.info("Creating template Utility Generator");
checkWeightsOfUtilityComponents(utilityComponents);
utilityFunctionEvaluator = new UtilityFunctionEvaluator( cpModelFilePath, nodeCandidates, utilityComponents);
utilityFunctionEvaluator = new UtilityFunctionEvaluator(cpModelFilePath, nodeCandidates, utilityComponents);
}
public double evaluate(Collection<VariableValueDTO> solution) {
return this.utilityFunctionEvaluator.evaluate(solution);
}
private final void checkWeightsOfUtilityComponents(List<Map.Entry<TemplateProvider.AvailableTemplates, Double>> utilityComponents) {
private void checkWeightsOfUtilityComponents(List<Map.Entry<TemplateProvider.AvailableTemplates, Double>> utilityComponents) {
if (utilityComponents.stream().map(Map.Entry::getValue).reduce(0.0, Double::sum) > 1.0
|| utilityComponents.stream().map(Map.Entry::getValue).anyMatch(weight -> weight < 0)) {
throw new RuntimeException("Sum of weights must be smaller or equal to 1 and non-negative!");
......
......@@ -6,9 +6,8 @@ import lombok.extern.slf4j.Slf4j;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static eu.melodic.upperware.utilitygenerator.utility_function.utility_templates_provider.BasicTemplatesProvider.*;
......@@ -24,17 +23,19 @@ public class TemplateProvider {
CORES_MIN_MAX,
DISK_MIN_MAX,
RAM_MIN_MAX
};
}
;
public static String getTemplate(Collection<VariableDTO> variablesFromConstraintProblem,
List<Map.Entry<AvailableTemplates, Double>> utilityComponents) {
return getSum( utilityComponents.stream().map( (template) ->
multiply( template.getValue().toString(), getTemplate(variablesFromConstraintProblem, template.getKey()))
).collect(Collectors.toList()));
List<Map.Entry<AvailableTemplates, Double>> utilityComponents) {
return getSum(utilityComponents.stream().map((template) ->
multiply(template.getValue().toString(), getTemplate(variablesFromConstraintProblem, template.getKey()))
).collect(Collectors.toList()));
}
private static String getTemplate(Collection<VariableDTO> variablesFromConstraintProblem, AvailableTemplates type) {
switch (type){
switch (type) {
case COST:
return getOnlyCostUtility(variablesFromConstraintProblem);
case CORES:
......
......@@ -16,7 +16,6 @@ import java.util.stream.Collectors;
import static java.lang.String.format;
//import eu.melodic.upperware.cpsolver.solver.parser.creator.*;
public class CPParsedData {
private Collection<Constant> constants;
......
......@@ -15,7 +15,10 @@ public class ParallelUtilityProviderImpl implements UtilityProvider {
public ParallelUtilityProviderImpl(List<UtilityGeneratorApplication> utility) {
this.utility = utility;
occupied = utility.stream().map(util -> false).collect(Collectors.toList());
occupied = utility
.stream()
.map(util -> false)
.collect(Collectors.toList());
}
@Override
......
......@@ -92,7 +92,7 @@ public class ExpressionEvaluator {
return expression instanceof ComposedExpression;
}
static double evaluateExpression(Expression exp, Map<String, NumericValueInterface> variables) {
public static double evaluateExpression(Expression exp, Map<String, NumericValueInterface> variables) {
if (isConstant(exp)) {
return getValueOfNumericInterface(((Constant) exp).getValue());
} else if (isCpMetric(exp)) {
......
......@@ -12,6 +12,7 @@ import eu.paasage.upperware.metamodel.cp.*;
import eu.paasage.upperware.metamodel.types.BasicTypeEnum;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
......
package eu.melodic.upperware.cp_wrapper;
import eu.melodic.upperware.cp_wrapper.utils.constraint.Constraint;
import eu.melodic.upperware.cp_wrapper.utils.constraint_graph.ConstraintGraph;
import eu.melodic.upperware.cp_wrapper.utils.expression_evaluator.ExpressionEvaluator;
import eu.melodic.upperware.cp_wrapper.utils.numeric_value.NumericValueInterface;
import eu.melodic.upperware.cp_wrapper.utils.numeric_value.implementations.DoubleValue;
import eu.melodic.upperware.cp_wrapper.utils.test_utils.mockups.*;
import eu.melodic.upperware.cp_wrapper.utils.variable_orderer.HeuristicVariableOrderer;
import eu.paasage.upperware.metamodel.cp.*;
import eu.paasage.upperware.metamodel.types.BasicTypeEnum;
import eu.paasage.upperware.metamodel.types.NumericValueUpperware;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import static org.testng.AssertJUnit.*;
class ExpressionEvaluatorTest {
......@@ -119,4 +122,70 @@ class ExpressionEvaluatorTest {
vars.put(names[2], new DoubleValue(vals[4]));
assertEquals(ExpressionEvaluator.evaluateExpression(composed, vars) , realValue);
}
static class HeuristicVariableOrdererTest {
private static List<String> variables;
@BeforeAll
static void setUp() {
variables = Arrays.asList("var1", "var2", "var3", "var4", "var5", "var6", "var7", "var8");
}
private static ConstraintGraph createExpandingGraph() {
Collection<Constraint> constraints = new ArrayList<>();
int varsSize = variables.size();
for(int i = 0; i < varsSize; i++) {
constraints.add(new ConstraintMockup(variables.subList(0,i+1)));
}
return new ConstraintGraph(constraints, variables);
}
private static ConstraintGraph sameNumberOfConstraintsGraph() {
Collection<Constraint> constraints = new ArrayList<>();
int varsSize = variables.size();
for(int i = 0; i < varsSize; i++) {
constraints.add(new ConstraintMockup(variables.subList(0,i+1)));
for (int j = i + 1; j < varsSize; j++) {
constraints.add(new ConstraintMockup(variables.subList(j, j+1)));
}
}
return new ConstraintGraph(constraints, variables);
}
@Test
public void expandingGraphTest(){
ConstraintGraph graph = createExpandingGraph();
HeuristicVariableOrderer orderer = new HeuristicVariableOrderer(graph, Collections.emptyList());
for (int i = 0; i < variables.size(); i++) {
Assertions.assertTrue( orderer.getNameFromIndex(i).equals(variables.get(i)));
}
}
@Test
public void sameNumberOfConstraintsTest() {
ConstraintGraph graph = sameNumberOfConstraintsGraph();
// Here the ordering is not deterministic but we can at least check if
// all the variables are indexed
HeuristicVariableOrderer orderer = new HeuristicVariableOrderer(graph, Collections.emptyList());
Set<String> vars = new HashSet<>();
for (int i = 0; i < variables.size(); i++) {
vars.add(orderer.getNameFromIndex(i));
}
Assertions.assertEquals(vars.size(), variables.size());
}
@Test
public void simpleGraphTest() {
Collection<Constraint> constraints = new ArrayList<>();
constraints.add(new ConstraintMockup(variables.subList(0,1)));
constraints.add(new ConstraintMockup(variables.subList(1,3)));
constraints.add(new ConstraintMockup(variables.subList(2,3)));
ConstraintGraph graph = new ConstraintGraph(constraints, variables.subList(0,3));
HeuristicVariableOrderer orderer = new HeuristicVariableOrderer(graph, Collections.emptyList());
Assertions.assertTrue(orderer.getNameFromIndex(0).equals(variables.get(2)));
Assertions.assertTrue(orderer.getNameFromIndex(1).equals(variables.get(1)));
Assertions.assertTrue(orderer.getNameFromIndex(2).equals(variables.get(0)));
}
}
}
\ No newline at end of file
......@@ -43,11 +43,11 @@ class HeuristicVariableOrdererTest {
@Test
public void expandingGraphTest(){
ConstraintGraph graph = createExpandingGraph();
HeuristicVariableOrderer orderer = new HeuristicVariableOrderer(graph, Collections.emptyList());
for (int i = 0; i < variables.size(); i++) {
assertTrue( orderer.getNameFromIndex(i).equals(variables.get(i)));
}
ConstraintGraph graph = createExpandingGraph();
HeuristicVariableOrderer orderer = new HeuristicVariableOrderer(graph, Collections.emptyList());
for (int i = 0; i < variables.size(); i++) {
assertTrue( orderer.getNameFromIndex(i).equals(variables.get(i)));
}
}
@Test
......
......@@ -6,7 +6,7 @@
<artifactId>zpp-solver</artifactId>
<groupId>org.ow2.paasage</groupId>
<version>3.1.0-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
......
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