Commit 3ea26561 authored by Jan Philipp Haller's avatar Jan Philipp Haller
Browse files

Fixed Matrix Dimension Calculations and Tests

parent 88f6f3ef
Pipeline #447050 failed with stage
in 1 minute and 31 seconds
......@@ -15,11 +15,12 @@ public class MathDimensionCalculatorHelper {
if (mathExpressionSymbol.getOperator().equals("+")) {
MathExpressionSymbol realLeftExpression = MathOptimizer.getCurrentAssignment(mathExpressionSymbol.getLeftExpression(), precedingExpressions);
MathExpressionSymbol realRightExpression = MathOptimizer.getCurrentAssignment(mathExpressionSymbol.getRightExpression(), precedingExpressions);
result = MathDimensionCalculatorHelper.calculateMatrixColumns(realLeftExpression, realRightExpression, precedingExpressions);
result = MathDimensionCalculatorHelper.calculateAdditionMatrixColumns(realLeftExpression, realRightExpression, precedingExpressions);
} else if (mathExpressionSymbol.getOperator().equals("*")) {
MathExpressionSymbol realLeftExpression = MathOptimizer.getCurrentAssignment(mathExpressionSymbol.getLeftExpression(), precedingExpressions);
MathExpressionSymbol realRightExpression = MathOptimizer.getCurrentAssignment(mathExpressionSymbol.getRightExpression(), precedingExpressions);
result = MathDimensionCalculatorHelper.calculateMatrixColumns(realLeftExpression, realRightExpression, precedingExpressions);
//WHY are + and * the same?
result = MathDimensionCalculatorHelper.calculateMultiplicationMatrixColumns(realLeftExpression, realRightExpression, precedingExpressions);
} else {
Log.info(mathExpressionSymbol.getClass().getName(), "Not handled:");
}
......@@ -28,11 +29,15 @@ public class MathDimensionCalculatorHelper {
public static int calculateMatrixColumns(MathExpressionSymbol realLeftExpression, MathExpressionSymbol realRightExpression, List<MathExpressionSymbol> precedingExpressions) {
int result = 0;
if (MathDimensionCalculator.getMatrixRows(realRightExpression, precedingExpressions) == 1) {
//orig:
//if (MathDimensionCalculator.getMatrixRows(realRightExpression, precedingExpressions) == 1) {
if (MathDimensionCalculator.getMatrixColumns(realRightExpression, precedingExpressions) == 1) {
result = MathDimensionCalculator.getMatrixColumns(realLeftExpression, precedingExpressions);
} else {
//orig: result = MathDimensionCalculator.getMatrixColumns(realLeftExpression, precedingExpressions);
result = MathDimensionCalculator.getMatrixColumns(realRightExpression, precedingExpressions);
//orig:
result = MathDimensionCalculator.getMatrixColumns(realLeftExpression, precedingExpressions);
//result = MathDimensionCalculator.getMatrixColumns(realRightExpression, precedingExpressions);
}
return result;
}
......@@ -42,17 +47,37 @@ public class MathDimensionCalculatorHelper {
if (mathExpressionSymbol.getOperator().equals("+")) {
MathExpressionSymbol realLeftExpression = MathOptimizer.getCurrentAssignment(mathExpressionSymbol.getLeftExpression(), precedingExpressions);
MathExpressionSymbol realRightExpression = MathOptimizer.getCurrentAssignment(mathExpressionSymbol.getRightExpression(), precedingExpressions);
result = calculateMatrixRows(realLeftExpression, realRightExpression, precedingExpressions);
result = calculateAdditionMatrixRows(realLeftExpression, realRightExpression, precedingExpressions);
} else if (mathExpressionSymbol.getOperator().equals("*")) {
MathExpressionSymbol realLeftExpression = MathOptimizer.getCurrentAssignment(mathExpressionSymbol.getLeftExpression(), precedingExpressions);
MathExpressionSymbol realRightExpression = MathOptimizer.getCurrentAssignment(mathExpressionSymbol.getRightExpression(), precedingExpressions);
result = calculateMatrixRows(realLeftExpression, realRightExpression, precedingExpressions);
//WHY are + and * the same?
result = calculateMultiplicationMatrixRows(realLeftExpression, realRightExpression, precedingExpressions);
} else {
Log.info(mathExpressionSymbol.getClass().getName(), "Not handled:");
}
return result;
}
//Warning: Do we need sanity checks?
private static int calculateAdditionMatrixRows(MathExpressionSymbol realLeftExpression, MathExpressionSymbol realRightExpression, List<MathExpressionSymbol> precedingExpressions) {
//Q: Do we need a sanity check here?
return MathDimensionCalculator.getMatrixRows(realLeftExpression, precedingExpressions);
}
private static int calculateMultiplicationMatrixRows(MathExpressionSymbol realLeftExpression, MathExpressionSymbol realRightExpression, List<MathExpressionSymbol> precedingExpressions) {
return MathDimensionCalculator.getMatrixRows(realLeftExpression, precedingExpressions);
}
private static int calculateAdditionMatrixColumns(MathExpressionSymbol realLeftExpression, MathExpressionSymbol realRightExpression, List<MathExpressionSymbol> precedingExpressions) {
//Q: Do we need a sanity check here?
return MathDimensionCalculator.getMatrixColumns(realLeftExpression, precedingExpressions);
}
private static int calculateMultiplicationMatrixColumns(MathExpressionSymbol realLeftExpression, MathExpressionSymbol realRightExpression, List<MathExpressionSymbol> precedingExpressions) {
return MathDimensionCalculator.getMatrixColumns(realRightExpression, precedingExpressions);
}
public static int calculateMatrixRows(MathExpressionSymbol realLeftExpression, MathExpressionSymbol realRightExpression, List<MathExpressionSymbol> precedingExpressions) {
int result;
if (MathDimensionCalculator.getMatrixColumns(realRightExpression, precedingExpressions) == 1) {
......
......@@ -49,7 +49,8 @@ public class MathInformationRegister {
secondDoubleDot = mathMatrixAccessOperatorSymbol.getMathMatrixAccessSymbols().get(1).isDoubleDot();
}
if (firstDoubleDot)
//Check "doubleDots" !!!
if (secondDoubleDot)
result = 1;
else {
result = getAmount(name, 0);
......@@ -129,7 +130,7 @@ public class MathInformationRegister {
firstDoubleDot = mathMatrixAccessOperatorSymbol.getMathMatrixAccessSymbols().get(0).isDoubleDot();
secondDoubleDot = mathMatrixAccessOperatorSymbol.getMathMatrixAccessSymbols().get(1).isDoubleDot();
}
if (secondDoubleDot)
if (firstDoubleDot)
result = 1;
else {
result = getAmount(name, 1);
......
......@@ -245,7 +245,8 @@ public class MathMatrixMultiplicationOrder implements MathOptimizationRule {
MathExpressionSymbol newExpression = getMostEfficientExpression(newExps);
Log.info(oldMathArithmeticExpressionSymbol.getTextualRepresentation(), "oldExpression inflated");
String debugHelpString = newExpression.getTextualRepresentation();
String debugHelpStringNew = newExpression.getTextualRepresentation();
String debugHelpStringOld = oldMathArithmeticExpressionSymbol.getTextualRepresentation();
Log.info(newExpression.getTextualRepresentation(), "newExpression:");
Log.info(newExpression.getTextualRepresentation(), "Term Rewritten:");
long operationsStandard = MathOptimizer.getEstimatedOperations(oldMathArithmeticExpressionSymbol, precedingExpressions);
......
......@@ -136,16 +136,22 @@ public class MathOptimizer {
MathExpressionSymbol realLeftExpressionSymbol = getCurrentAssignment(mathArithmeticExpressionSymbol.getLeftExpression().getRealMathExpressionSymbol(), precedingExpressions);
MathExpressionSymbol realRightExpressionSymbol = getCurrentAssignment(mathArithmeticExpressionSymbol.getRightExpression().getRealMathExpressionSymbol(), precedingExpressions);
//Debug: A = m x n, B = n x p. Then: Op(A * B) = m * n * p
//orig:
/*
long n = MathDimensionCalculator.getMatrixColumns(realLeftExpressionSymbol, precedingExpressions);
long m = MathDimensionCalculator.getMatrixRows(realLeftExpressionSymbol, precedingExpressions);
long p = MathDimensionCalculator.getMatrixRows(realRightExpressionSymbol, precedingExpressions);
*/
long n = MathDimensionCalculator.getMatrixRows(realLeftExpressionSymbol, precedingExpressions);
long m = MathDimensionCalculator.getMatrixColumns(realLeftExpressionSymbol, precedingExpressions);
//long p = MathDimensionCalculator.getMatrixRows(realRightExpressionSymbol, precedingExpressions);
//Should be this:
long p = MathDimensionCalculator.getMatrixColumns(realRightExpressionSymbol, precedingExpressions);
//Debug
long leftCol = MathDimensionCalculator.getMatrixColumns(realLeftExpressionSymbol, precedingExpressions);
long leftRow = MathDimensionCalculator.getMatrixRows(realLeftExpressionSymbol, precedingExpressions);
long rightCol = MathDimensionCalculator.getMatrixColumns(realRightExpressionSymbol, precedingExpressions);
long rightRow = MathDimensionCalculator.getMatrixRows(realRightExpressionSymbol, precedingExpressions);
long previousOperations = 0;
//Log.info(realLeftExpressionSymbol.getTextualRepresentation()+" "+MathDimensionCalculator.getMatrixRows(realLeftExpressionSymbol, precedingExpressions) +" "+
// MathDimensionCalculator.getMatrixColumns(realLeftExpressionSymbol, precedingExpressions),"Matrix:");
......@@ -177,7 +183,7 @@ public class MathOptimizer {
MathExpressionSymbol realLeftExpressionSymbol = getCurrentAssignment(mathArithmeticExpressionSymbol.getLeftExpression().getRealMathExpressionSymbol(), precedingExpressions);
MathExpressionSymbol realRightExpressionSymbol = getCurrentAssignment(mathArithmeticExpressionSymbol.getRightExpression().getRealMathExpressionSymbol(), precedingExpressions);
return MathDimensionCalculator.getMatrixColumns(realLeftExpressionSymbol, precedingExpressions) * MathDimensionCalculator.getMatrixRows(realLeftExpressionSymbol, precedingExpressions) * MathDimensionCalculator.getMatrixRows(realRightExpressionSymbol, precedingExpressions);
return MathDimensionCalculator.getMatrixColumns(realLeftExpressionSymbol, precedingExpressions) * MathDimensionCalculator.getMatrixRows(realLeftExpressionSymbol, precedingExpressions) * MathDimensionCalculator.getMatrixColumns(realRightExpressionSymbol, precedingExpressions);
} else if (mathArithmeticExpressionSymbol.getMathOperator().equals("+")) {
MathExpressionSymbol realLeftExpressionSymbol = getCurrentAssignment(mathArithmeticExpressionSymbol.getLeftExpression().getRealMathExpressionSymbol(), precedingExpressions);
......
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.lang.monticar.generator.mathopt;
//import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ExpandedComponentInstanceSymbol;
//import de.monticore.lang.monticar.generator.cpp.EMAMOpt2CPPSymbolTableHelper;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAComponentInstanceSymbol;
import de.monticore.lang.monticar.generator.AbstractSymtabTest;
import de.monticore.lang.monticar.generator.cpp.GeneratorCPP;
import de.monticore.lang.tagging._symboltable.TaggingResolver;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.List;
import static org.junit.Assert.assertNotNull;
public class CarModelGenerationTest extends AbstractSymtabTest {
// private static TaggingResolver symtab = EMAMOpt2CPPSymbolTableHelper.getInstance().createSymTabAndTaggingResolver("src/test/resources");
protected static List<File> doGenerateModel(String fullModelName) throws IOException {
TaggingResolver symtab = createSymTabAndTaggingResolver("src/test/resources/mathopt");
EMAComponentInstanceSymbol componentInstanceSymbol = symtab.<EMAComponentInstanceSymbol>resolve(fullModelName, EMAComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(componentInstanceSymbol);
GeneratorCPP generatorCPP = new GeneratorCPP();
generatorCPP.useArmadilloBackend();
generatorCPP.setGenerationTargetPath("./target/generated-sources-cpp/mathopt/carmodel/" + fullModelName.substring(fullModelName.lastIndexOf(".") + 1, fullModelName.length()));
return generatorCPP.generateFiles(symtab, componentInstanceSymbol);
}
@Test
public void carModelInstanceTest() throws IOException {
List<File> files = doGenerateModel("de.rwth.monticar.carmodel.carModelInstanceTest");
}
}
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.lang.monticar.generator.mathopt;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAComponentInstanceSymbol;
import de.monticore.lang.monticar.generator.AbstractSymtabTest;
import de.monticore.lang.monticar.generator.Generator;
import de.monticore.lang.monticar.generator.cpp.GeneratorCPP;
import de.monticore.lang.tagging._symboltable.TaggingResolver;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.List;
import static org.junit.Assert.assertNotNull;
public class TrajectoryControllerTest extends AbstractSymtabTest {
@Test
public void generateTrajectoryControllerMPCBicycle() throws IOException {
TaggingResolver symtab = createSymTabAndTaggingResolver("src/test/resources/mathopt");
EMAComponentInstanceSymbol componentInstanceSymbol = symtab.<EMAComponentInstanceSymbol>resolve("de.rwth.monticar.mpc.trajectoryControllerMPC", EMAComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(componentInstanceSymbol);
GeneratorCPP generator = new GeneratorCPP();
generator.setGenerateDynamicInterface(true);
generator.setGenerateServerWrapper(false);
generator.setGenerateCMake(true);
generator.setGenerationTargetPath("./target/generated-sources-cpp/TrajectoryControllerMPC/src/");
//TODO: fix errors
// generator.setPreferedSolver(Solver.Ipopt);
// generator.getSolverOptions().put("String derivative_test", "none");
// 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.generateFiles(symtab, componentInstanceSymbol);
}
}
/* (c) https://github.com/MontiCore/monticore */
#ifndef OPTIMIZER_SIMPLESCALARMULTIPLICATION2
#define OPTIMIZER_SIMPLESCALARMULTIPLICATION2
#ifndef M_PI
......@@ -19,7 +18,7 @@ matOut=Matrix(10,200);
}
void execute()
{
matOut = mat1*mat2*factor;
matOut = mat1*(mat2*factor);
}
};
......
Markdown is supported
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