Commit 45603ffc authored by Christoph Richter's avatar Christoph Richter
Browse files

Merge branch 'richter-dev' into 'master'

Richter dev

See merge request !2
parents 1094bf27 1d26d0fa
Pipeline #68782 passed with stage
in 7 minutes and 12 seconds
......@@ -9,7 +9,7 @@
<groupId>de.monticore.lang.monticar</groupId>
<artifactId>embedded-montiarc-math-opt-generator</artifactId>
<version>0.0.8-SNAPSHOT</version>
<version>0.1.0</version>
<!-- == PROJECT DEPENDENCIES ============================================= -->
......@@ -23,10 +23,10 @@
<Embedded-MontiArc.version>0.0.18-SNAPSHOT</Embedded-MontiArc.version>
<Embedded-MontiArc-Behaviour.version>0.0.14-SNAPSHOT</Embedded-MontiArc-Behaviour.version>
<Math.version>0.0.19-SNAPSHOT</Math.version>
<MathOpt.version>0.0.19-SNAPSHOT</MathOpt.version>
<MathOpt.version>0.1.0</MathOpt.version>
<Embedded-MontiArc-Math.version>0.0.19-SNAPSHOT</Embedded-MontiArc-Math.version>
<Embedded-MontiArc-Math-Opt.version>0.0.7-SNAPSHOT</Embedded-MontiArc-Math-Opt.version>
<Embedded-montiarc-math-generator.version>0.0.23-SNAPSHOT</Embedded-montiarc-math-generator.version>
<Embedded-MontiArc-Math-Opt.version>0.1.0</Embedded-MontiArc-Math-Opt.version>
<Embedded-montiarc-math-generator.version>0.0.25-SNAPSHOT</Embedded-montiarc-math-generator.version>
<Embedded-montiarc-math-middleware-generator.version>0.0.4-SNAPSHOT</Embedded-montiarc-math-middleware-generator.version>
<!-- .. Libraries .................................................. -->
<guava.version>18.0</guava.version>
......
......@@ -95,7 +95,7 @@ public class GeneratorEMAMOpt2CppCli {
if (componentSymbol != null) {
g.generateFiles(componentSymbol, symTab);
} else {
g.saveFilesToDisk(g.handleTestAndCheckDir(symTab));
g.saveFilesToDisk(g.handleTestAndCheckDir(symTab, componentSymbol));
}
} catch (IOException var9) {
Log.error("error during generation", var9);
......
......@@ -124,11 +124,11 @@ public class Problem {
/**
* Default value if no lower bound is set
*/
public final static String LOWER_BOUND_INF = "-1E+19";
public final static String LOWER_BOUND_INF = "-1E19";
/**
* Default value if no upper bound is set
*/
public final static String UPPER_BOUND_INF = "1E+19";
public final static String UPPER_BOUND_INF = "1E19";
/**
* number of constraints in function g
*/
......
......@@ -80,9 +80,27 @@ public class ProblemAssignmentHandler {
if (expr instanceof MathMatrixNameExpressionSymbol) {
MathMatrixNameExpressionSymbol matNameExpr = (MathMatrixNameExpressionSymbol) expr;
String indexExpr = ExecuteMethodGeneratorMatrixExpressionHandler.generateExecuteCode(matNameExpr.getMathMatrixAccessOperatorSymbol(), new ArrayList<>());
indexExpr = indexExpr.replaceAll(",", "+");
if (MathConverter.curBackend.usesZeroBasedIndexing()) {
indexExpr += " - " + matNameExpr.getMathMatrixAccessOperatorSymbol().getMathMatrixAccessSymbols().size();
if (indexExpr.contains(".col") && indexExpr.contains(".row"))
indexExpr = "all";
else if (indexExpr.startsWith(".col"))
indexExpr = "\"col\", " + indexExpr.substring(indexExpr.indexOf("(") + 1, indexExpr.indexOf(")"));
else if (indexExpr.startsWith(".row"))
indexExpr = "\"row\", " + indexExpr.substring(indexExpr.indexOf("(") + 1, indexExpr.indexOf(")"));
else {
Vector<Integer> dims = p.getOptimizationVariableDimensions();
if (indexExpr.contains(",")) {
indexExpr = indexExpr.replaceAll(",", "+ (");
StringBuilder indexSB = new StringBuilder();
indexSB.append(indexExpr);
indexSB.insert(indexSB.length() - 1 ," - 1 ) * " + dims.get(0) + " ");
indexExpr = indexSB.toString();
}
if (MathConverter.curBackend.usesZeroBasedIndexing()) {
if (indexExpr.contains("+"))
indexExpr = indexExpr.replaceAll("\\+", "- 1 +");
else if (indexExpr.contains(")"))
indexExpr = indexExpr.substring(0, indexExpr.lastIndexOf(")")) + "- 1)";
}
}
String funcSigniture = String.format("%s, %s, %s", currXL, indexExpr, currXU);
xMatrixElementConstraints.add(funcSigniture);
......
......@@ -166,10 +166,122 @@ class ${viewModel.callSolverName}
}
};
static void addConstraintOnX(Dvector &xl, Dvector &xu, const double &lower, int index, const double &upper) {
static void addConstraintOnX(Dvector &xl, Dvector &xu, const double &lower, const int index, const double &upper) {
xl[index] = std::fmax(xl[index], lower);
xu[index] = std::fmin(xu[index], upper);
};
static void addConstraintOnX(Dvector &xl, Dvector &xu, const double &lower, const std::string access, const int index, const double &upper) {
int dims[] = {<#list viewModel.optimizationVariableDimensions as dim>${dim?c}<#sep>, </#list>};
int cols;
int rows = dims[0];
if (sizeof(dims) <= 1)
cols = 1;
else
cols = dims[1];
if (access.compare("col") == 0) {
for (int i = index; i < xl.size(); i += rows) {
xl[i] = std::fmax(xl[i], lower);
xu[i] = std::fmin(xu[i], upper);
}
} else if (access.compare("row") == 0) {
for (int i = index * cols; i < ((index + 1) * cols); i++) {
xl[i] = std::fmax(xl[i], lower);
xu[i] = std::fmin(xu[i], upper);
}
}
};
static void addConstraintOnX(Dvector &xl, Dvector &xu, const colvec &lower, const std::string access, const int index, const double &upper) {
int dims[] = {<#list viewModel.optimizationVariableDimensions as dim>${dim?c}<#sep>, </#list>};
int cols;
int rows = dims[0];
if (sizeof(dims) <= 1)
cols = 1;
else
cols = dims[1];
for (int i = index; i < xl.size(); i += rows) {
xl[i] = std::fmax(xl[i], lower[i]);
xu[i] = std::fmin(xu[i], upper);
}
};
static void addConstraintOnX(Dvector &xl, Dvector &xu, const double &lower, const std::string access, const int index, const colvec &upper) {
int dims[] = {<#list viewModel.optimizationVariableDimensions as dim>${dim?c}<#sep>, </#list>};
int cols;
int rows = dims[0];
if (sizeof(dims) <= 1)
cols = 1;
else
cols = dims[1];
for (int i = index; i < xl.size(); i += rows) {
xl[i] = std::fmax(xl[i], lower);
xu[i] = std::fmin(xu[i], upper[i]);
}
};
static void addConstraintOnX(Dvector &xl, Dvector &xu, const colvec &lower, const std::string access, const int index, const colvec &upper) {
int dims[] = {<#list viewModel.optimizationVariableDimensions as dim>${dim?c}<#sep>, </#list>};
int cols;
int rows = dims[0];
if (sizeof(dims) <= 1)
cols = 1;
else
cols = dims[1];
for (int i = index; i < xl.size(); i += rows) {
xl[i] = std::fmax(xl[i], lower[i]);
xu[i] = std::fmin(xu[i], upper[i]);
}
};
static void addConstraintOnX(Dvector &xl, Dvector &xu, const rowvec &lower, const std::string access, const int index, const double &upper) {
int dims[] = {<#list viewModel.optimizationVariableDimensions as dim>${dim?c}<#sep>, </#list>};
int cols;
int rows = dims[0];
if (sizeof(dims) <= 1)
cols = 1;
else
cols = dims[1];
for (int i = index * cols; i < ((index + 1) * cols); i++) {
xl[i] = std::fmax(xl[i], lower[i]);
xu[i] = std::fmin(xu[i], upper);
}
};
static void addConstraintOnX(Dvector &xl, Dvector &xu, const double &lower, const std::string access, const int index, const rowvec &upper) {
int dims[] = {<#list viewModel.optimizationVariableDimensions as dim>${dim?c}<#sep>, </#list>};
int cols;
int rows = dims[0];
if (sizeof(dims) <= 1)
cols = 1;
else
cols = dims[1];
for (int i = index * cols; i < ((index + 1) * cols); i++) {
xl[i] = std::fmax(xl[i], lower);
xu[i] = std::fmin(xu[i], upper[i]);
}
};
static void addConstraintOnX(Dvector &xl, Dvector &xu, const rowvec &lower, const std::string access, const int index, const rowvec &upper) {
int dims[] = {<#list viewModel.optimizationVariableDimensions as dim>${dim?c}<#sep>, </#list>};
int cols;
int rows = dims[0];
if (sizeof(dims) <= 1)
cols = 1;
else
cols = dims[1];
for (int i = index * cols; i < ((index + 1) * cols); i++) {
xl[i] = std::fmax(xl[i], lower[i]);
xu[i] = std::fmin(xu[i], upper[i]);
}
};
static void addConstraintOnX(Dvector &xl, Dvector &xu, const double &lower, const std::string access, const double &upper) {
for (int i = 0; i < xl.size(); i++) {
xl[i] = std::fmax(xl[i], lower);
xu[i] = std::fmin(xu[i], upper);
}
};
public:
static bool solveOptimizationProblemIpOpt(
......
......@@ -52,8 +52,8 @@ public class GeneratorCplexSolverTest {
}
@Test
public void lPOptimizationCMakeTest() throws IOException {
List<File> files = doGenerateModel("de.rwth.monticar.optimization.lPOptimizationTest");
public void transportationProblemCMakeTest() throws IOException {
List<File> files = doGenerateModel("de.rwth.monticar.optimization.transportationProblem");
}
@Test
......
......@@ -51,13 +51,13 @@ public class GeneratorEMAMOpt2CMakeTest extends BasicGenerationTest {
}
@Test
public void standardIpoptOptimizationTestCMakeTest() throws IOException {
List<File> files = doGenerateModel("de.rwth.monticar.optimization.standardIpoptOptimizationTest");
public void hs71TestCMakeTest() throws IOException {
List<File> files = doGenerateModel("de.rwth.monticar.optimization.hS71");
}
@Test
public void lPOptimizationCMakeTest() throws IOException {
List<File> files = doGenerateModel("de.rwth.monticar.optimization.lPOptimizationTest");
public void transportationProblemCMakeTest() throws IOException {
List<File> files = doGenerateModel("de.rwth.monticar.optimization.transportationProblem");
}
@Test
......
......@@ -69,7 +69,7 @@ public class GeneratorEMAMOpt2CPPTest extends BasicGenerationTest {
*/
@Test
public void testStandardIpoptOptimizationTest() throws IOException {
List<File> files = doGenerateOptimizationModel("standardIpoptOptimizationTest");
List<File> files = doGenerateOptimizationModel("hS71");
// TODO: create reference solution
// String restPath = "testMath/optimizationSolver/";
// testFilesAreEqual(files, restPath);
......@@ -81,7 +81,7 @@ public class GeneratorEMAMOpt2CPPTest extends BasicGenerationTest {
*/
@Test
public void testLPOptimizationTest() throws IOException {
List<File> files = doGenerateOptimizationModel("lPOptimizationTest");
List<File> files = doGenerateOptimizationModel("transportationProblem");
// TODO: create reference solution
// String restPath = "testMath/optimizationSolver/";
// testFilesAreEqual(files, restPath);
......
......@@ -23,10 +23,11 @@ public class TrajectoryControllerTest extends BasicGenerationTest {
GeneratorEMAMOpt2CPP generator = new GeneratorEMAMOpt2CPP();
generator.setGenerateAutopilotAdapter(true);
generator.setGenerateServerWrapper(false);
generator.setGenerateCMake(true);
generator.setGenerationTargetPath("./target/generated-sources-cpp/TrajectoryControllerMPC/src/");
generator.setPreferedSolver(Solver.Ipopt);
generator.getSolverOptions().put("String derivative_test", "none");
generator.getSolverOptions().put("Integer print_level", "0");
generator.getSolverOptions().put("Integer print_level", "3");
generator.getSolverOptions().put("Numeric tol", "0.01");
generator.getSolverOptions().put("Integer max_iter", "50");
List<File> files = generator.generate(componentInstanceSymbol, symtab);
......
package de.rwth.monticar.optimization;
stream BoundedConditionsTest for BoundedConditionsTest{
xOut: 0.5 +/- 0.0001;
yOut: 0.25 +/- 0.0001;
}
\ No newline at end of file
package de.rwth.monticar.optimization;
stream ColRowMinTest for ColRowMinTest{
xOut: [1 +/- 0.0001, 2 +/- 0.0001; 1 +/- 0.0001, 1 +/- 0.0001];
yOut: 3 +/- 0.0001;
}
\ No newline at end of file
package de.rwth.monticar.optimization;
stream ConstMatrixSumMinimizationTest for ConstMatrixSumMinimizationTest{
xOut: -1 +/- 0.0001;
yOut: -1 +/- 0.0001;
}
\ No newline at end of file
package de.rwth.monticar.optimization;
stream ConstraintTest for ConstraintTest{
xOut: [0 +/- 0.0001, 2 +/- 0.0001, 2 +/- 0.0001; 1 +/- 0.0001, 1 +/- 0.0001, 2 +/- 0.0001];
yOut: -1.6901 +/- 0.0001;
}
\ No newline at end of file
......@@ -3,9 +3,9 @@ package de.rwth.monticar.optimization;
component ExistingOptimizationVariableTest{
ports out Q xOut1,
out Q aOut2,
out Q^{3,3} aOut2,
out Q bOut2,
out Q aOut3,
out Q^{3,3} aOut3,
out Q cOut3;
......
package de.rwth.monticar.optimization;
stream ExistingOptimizationVariableTest for ExistingOptimizationVariableTest{
xOut1: -1 +/- 0.0001;
aOut2: [-10 +/- 0.0001, -, -; -, -10 +/- 0.0001, -; -, -, -10 +/- 0.0001];
bOut2: -1000 +/- 0.0001;
aOut3: [-20 +/- 0.0001, -, -; -, -20 +/- 0.0001, -; -, -, -20 +/- 0.0001];
cOut2: -57 +/- 0.0001;
}
\ No newline at end of file
......@@ -2,7 +2,7 @@ package de.rwth.monticar.optimization;
component ForLoopConditionsTest{
ports out Q xOut,
ports out Q^{3} xOut,
out Q yOut;
implementation Math{
......
package de.rwth.monticar.optimization;
stream ForLoopConditionsTest for ForLoopConditionsTest{
xOut: [0 +/- 0.0001; 0 +/- 0.0001; 0 +/- 0.0001];
yOut: 0 +/- 0.0001;
}
\ No newline at end of file
package de.rwth.monticar.optimization;
stream HS71 for HS71{
xOut: [-4.5676 +/- 0.01; -1.6614 +/- 0.01; -1.7612 +/- 0.01; -3.6434 +/- 0.01];
yOut: -134.734 +/- 0.01;
}
\ No newline at end of file
// transportation problem example (linear)
// see https://www.gams.com/products/simple-example/
package de.rwth.monticar.optimization;
component LPOptimizationTest{
ports out Q^{3, 2} xOut,
out Q yOut;
implementation Math
{
// define problem
Q m = 2;
Q n = 3;
// define A, b
Q^{2, 1} A = [350; 600];
Q^{3, 1} b = [325; 300; 275];
// cost matrix
Q ^{m, n} c = [2.5, 1.7, 1.8; 2.5, 1.8, 1.4];
// minimization problem
Q y = minimize(Q^{2, 3} x)
sum(c .* x);
subject to
sum(x, 2) == A;
sum(x, 1) == b;
x >= 0;
end
xOut = x;
yOut = y;
}
}
\ No newline at end of file
package de.rwth.monticar.optimization;
stream MatrixSumMinimizationTest1 for MatrixSumMinimizationTest1{
xOut: [0 +/- 0.0001, 0 +/- 0.0001; 0 +/- 0.0001, -5 +/- 0.0001];
yOut: -5 +/- 0.0001;
}
\ No newline at end of file
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