From 98fd21d56379940441f1fde12db8d4452de4e05c Mon Sep 17 00:00:00 2001 From: Rudi Schlatte Date: Fri, 23 Feb 2024 15:45:53 +0100 Subject: [PATCH] Don't skip float constraints for int variables This is legal AMPL: var replicas integer >= 1.0, <= 8; Therefore, also unify code for generating float and int variables since it is roughly the same. Change-Id: I768af08063666e43bf162c9c88e8536c6c6b5f79 --- .../optimiser/controller/AMPLGenerator.java | 37 +++++-------------- 1 file changed, 10 insertions(+), 27 deletions(-) diff --git a/optimiser-controller/src/main/java/eu/nebulouscloud/optimiser/controller/AMPLGenerator.java b/optimiser-controller/src/main/java/eu/nebulouscloud/optimiser/controller/AMPLGenerator.java index 2bf5611..3e83277 100644 --- a/optimiser-controller/src/main/java/eu/nebulouscloud/optimiser/controller/AMPLGenerator.java +++ b/optimiser-controller/src/main/java/eu/nebulouscloud/optimiser/controller/AMPLGenerator.java @@ -198,46 +198,29 @@ public class AMPLGenerator { out.println("# Variables"); for (final JsonNode p : app.getKubevelaVariables().values()) { ObjectNode param = (ObjectNode) p; - // Even if these variables are sent over as "float", we know they - // have to be treated as integers for kubevela (replicas, memory) - // or SAL (cpu). I.e., paramMeaning overrides paramType. - List integerVariables = List.of("cpu", "memory", "replicas"); + List integerMeanings = List.of("cpu", "memory", "replicas"); String paramName = param.get("key").textValue(); String paramPath = param.get("path").textValue(); String paramType = param.get("type").textValue(); String paramMeaning = param.get("meaning").textValue(); + // Even if these variables are sent over as "float", we know they + // have to be treated as integers for kubevela (replicas, memory) + // or SAL (cpu). I.e., paramMeaning overrides paramType. + boolean shouldBeInt = paramType.equals("int") || integerMeanings.contains(paramMeaning); ObjectNode value = (ObjectNode)param.get("value"); - if (paramType.equals("int") || integerVariables.contains(paramMeaning)) { - out.format("var %s integer", paramName); - if (value != null) { - String separator = ""; - JsonNode lower = value.at("/lower_bound"); - JsonNode upper = value.at("/higher_bound"); - // TODO: What if the constraints are given as float? - // Round up or down? Emit as floats and hope for the - // best? Leave out illegal constraints? - if (lower.isIntegralNumber()) { - out.format(" >= %s", lower.longValue()); - separator = ", "; - } - if (upper.isIntegralNumber()) { - out.format("%s<= %s", separator, upper.longValue()); - } - } - out.println(";"); - } else if (paramType.equals("float")) { + if (paramType.equals("int") || paramType.equals("float")) { out.format("var %s", paramName); + if (shouldBeInt) { out.print(" integer"); } if (value != null) { String separator = ""; JsonNode lower = value.at("/lower_bound"); JsonNode upper = value.at("/higher_bound"); - // `isNumber` because the constraint might be given as integer if (lower.isNumber()) { - out.format(" >= %s", lower.doubleValue()); - separator = ", "; + out.format(" >= %s", lower.numberValue()); + separator = ","; } if (upper.isNumber()) { - out.format("%s<= %s", separator, upper.doubleValue()); + out.format("%s <= %s", separator, upper.numberValue()); } } out.println(";");