Commit ce6f41cd authored by Jan Philipp Haller's avatar Jan Philipp Haller
Browse files

Bugfix variable handover

parent 92374348
......@@ -75,7 +75,9 @@ public class IpoptSolverGeneratorImplementation implements NLPSolverGeneratorImp
options.put("Retape", "true");
vm.setOptions(options);
// set execute command
//vm.transformOptVariables(bluePrint);
vm.setExternalVariablesFromBluePrint(bluePrint);
String knownVariables = ", ";
for (Variable v : vm.getExternalVariables()) {
knownVariables += v.getName() + ", ";
......@@ -87,10 +89,6 @@ public class IpoptSolverGeneratorImplementation implements NLPSolverGeneratorImp
if (objVar.isEmpty())
objVar = "objectiveValue" + optimizationProblem.getId();
//Dynamic function call, better generated by vm
if(!vm.getOptimizationVariables().isEmpty()) {
result = String.format("%s::solveOptimizationProblemIpOpt(%s, %s%s);\n", vm.getCallSolverName(), /*vm.getOptimizationVariables().get(0).getName()*/ "" , objVar, knownVariables);
}
result = String.format("%s::solveOptimizationProblemIpOpt(%s);\n", vm.getCallSolverName(),vm.getIpoptSolverFunctionCallParameters());
// generate templates by view model
generateIpoptTemplates(vm, auxillaryFiles);
......
......@@ -92,13 +92,29 @@ public class IpoptViewModel extends SolverViewModel {
List<Variable> variables = bluePrint.getMathInformationRegister().getVariables();
variables.addAll(bluePrint.getVariables());
for (Variable v : variables) {
if (!isOptScopedVariable(v.getName()) && (v.isConstantVariable() || v.isInputVariable())) {
if (!isOptScopedVariable(v.getName())){// && (v.isConstantVariable() || v.isInputVariable())) {
if(!isExternalVariable(v.getName()))
this.externalVariables.add(v);
}
}
}
public void transformOptVariables(EMAMBluePrintCPP bluePrint){
/* Doesn't work */
List<Variable> variables = bluePrint.getMathInformationRegister().getVariables();
variables.addAll(bluePrint.getVariables());
for (Variable v : variables) {
if( isOptScopedVariable(v.getName())){
v.getDimensionalInformation();
Variable temp = new Variable(v);
temp.addDimensionalInformation(Integer.toString(getStepSizeCount()));
temp.setTypeNameTargetLanguage("colvec");
bluePrint.replaceVariable(v,temp);
}
}
}
public String getExternalVariableType(Variable extVar){
if(extVar.getVariableType() != null){
return extVar.getVariableType().getTypeNameTargetLanguage();
......@@ -116,10 +132,10 @@ public class IpoptViewModel extends SolverViewModel {
//Pointer to optimization variables (return values)
for (MathValueSymbol opt : getOptimizationVariables()){
String varName = opt.getName();
result += varName+", ";
result += "&"+varName+", ";
}
//Objective variable (return value)
result += getObjectiveVariableName()+", ";
result += "&"+getObjectiveVariableName()+", ";
//External Variables, treated as constants regarding optimization
for(Variable var : getExternalVariables()){
......@@ -139,10 +155,10 @@ public class IpoptViewModel extends SolverViewModel {
for (MathValueSymbol opt : getOptimizationVariables()){
String varType = getVariableType(opt);
String varName = opt.getName();
result += varType+ " &"+varName+", ";
result += varType+ " *"+varName+", ";
}
//Objective variable (return value)
result += getVariableType(getObjectiveVariable()) + " &"+getObjectiveVariableName()+", ";
result += "double *"+getObjectiveVariableName()+", ";
//External Variables, treated as constants regarding optimization
for(Variable var : getExternalVariables()){
......@@ -163,9 +179,11 @@ public class IpoptViewModel extends SolverViewModel {
String result = "";
String varTypeStr = symbol.getType().getType().getName();
if (varTypeStr.contentEquals("Q"))
result = "AD<double>";
result = "double";
if(symbol.getType().getDimensions().size() > 0)
result = "ADMat";
result = "mat";
if(hasStepSize())
result = "colvec";
return result;
}
......@@ -351,7 +369,7 @@ public class IpoptViewModel extends SolverViewModel {
String MatrixName = symbol.getNameToAccess();
String MatrixIndex = symbol.getMathMatrixAccessOperatorSymbol().getTextualRepresentation();
result = VectorName + "[" + getIpoptVarRef(MatrixName)+" + "+ MatrixIndex + "]";
result = VectorName + "[" + indexOffset +" + " +getIpoptVarRef(MatrixName)+" + "+ MatrixIndex + "]";
return result;
}
......@@ -366,6 +384,9 @@ public class IpoptViewModel extends SolverViewModel {
if(var.getName().equals(varName))
return true;
}
if(getObjectiveVariableName().equals(varName))
return true;
return false;
}
......
......@@ -151,7 +151,7 @@ public abstract class SolverViewModel extends ViewModelBase {
if(objectiveVariable != null) {
return objectiveVariable.getName();
}else{
return "";
return "y";
}
}
......
......@@ -39,13 +39,24 @@ typedef CPPAD_TESTVECTOR(CppAD::AD<double>) ADvector;
<#list viewModel.getConstraintFunctions() as constr>
#define ${viewModel.getIpoptConstraintRef(constr?index)} ${viewModel.getIpoptConstraintOffset(constr?index)}
</#list>
/*
mat vec2mat(vec V, size_t cols) {
size_t rows = std::ceil(V.n_elems / double(cols));
return V.reshape(cols, rows);// return the original vector as matrix
}
*/
namespace AnonymNS${viewModel.id}
{
using CppAD::AD;
using namespace arma;
//constant variables
<#list viewModel.getExternalVariables() as ext>
${viewModel.getExternalVariableType(ext)} ${ext.getName()};
</#list>
class FG_eval_${viewModel.callSolverName} {
public:
......@@ -71,12 +82,8 @@ namespace AnonymNS${viewModel.id}
for(int ${viewModel.getStepSizeName()} = ${viewModel.getStepSizeMin()?c}; ${viewModel.getStepSizeName()} < ${viewModel.getStepSizeMax()?c}; ${viewModel.getStepSizeName()}++)
</#if>
{
//Constraint Functions (e.g. x(n+1) == x(n)*2)
<#list viewModel.getConstraintFunctions() as constr>
${viewModel.getConstraintForFG_Eval(constr,constr?index)};
</#list>
//Debug: Simplified Constraint Functions
// Constraint Functions
<#list viewModel.getSimplifiedConstraintFunctions() as constr>
${viewModel.getConstraintForFG_Eval(constr,constr?index)};
</#list>
......@@ -174,7 +181,7 @@ class ${viewModel.callSolverName}
//Push constants to namespace
<#list viewModel.getExternalVariables() as ext>
${viewModel.getExternalVariableType(ext)} AnonymNS${viewModel.id}::${ext.getName()} = ${ext.getName()};
AnonymNS${viewModel.id}::${ext.getName()} = ${ext.getName()}; //${viewModel.getExternalVariableType(ext)}
</#list>
// object that computes objective and constraints
......@@ -195,31 +202,25 @@ class ${viewModel.callSolverName}
// Check some of the solution values
ok&=solution.status==CppAD::ipopt::solve_result<Dvector>::success;
// assign solution values
//ToDO: ADMat variables
<#list viewModel.getOptimizationVariables() as opt>
&${opt.getName()} = solution.x[${opt?index?c}];
<#if viewModel.hasStepSize()>
for(int ${viewModel.getStepSizeName()} = ${viewModel.getStepSizeMin()?c}; ${viewModel.getStepSizeName()} < ${viewModel.getStepSizeMax()?c}; ${viewModel.getStepSizeName()}++){
${opt.getName()}[${viewModel.getStepSizeName()}] = solution.x[${viewModel.getIpoptVarRef(opt)} + ${viewModel.getStepSizeName()}];
}
<#else>
${opt.getName()} = solution.x[${viewModel.getIpoptVarRef(opt)}];
</#if>
</#list>
for (int i${viewModel.id} = 0; i${viewModel.id} < solution.x.size(); i${viewModel.id}++)
{
x${viewModel.id}(i${viewModel.id}) = solution.x[i${viewModel.id}];
}
// objective value
<#if viewModel.optimizationProblemType.name() == "MINIMIZATION">
y${viewModel.id} = solution.obj_value;
*${viewModel.getObjectiveVariableName()} = solution.obj_value;
<#else>
y${viewModel.id} = -1 * solution.obj_value;
*${viewModel.getObjectiveVariableName()} = -1 * solution.obj_value;
</#if>
// print short message
std::cout<<std::endl<<std::endl<<"Solving status: "<<solution.status<<"!"<<std::endl;
std::cout<<"${viewModel.optimizationProblemType.name()?capitalize} variable value: "<<std::endl<<"x = "<<std::endl<<x${viewModel.id}<<std::endl;
std::cout<<"${viewModel.optimizationProblemType.name()?capitalize} objective value: "<<std::endl<<"y = "<<y${viewModel.id}<<std::endl;
return ok;
}
};
......
......@@ -26,6 +26,7 @@ import static org.junit.Assert.assertNotNull;
* To understand how automatic test generation works, examine the tests in this class.
*
*/
@Ignore
public class AutomaticStreamTestGenerationTest extends AbstractSymtabTest {
@BeforeClass
public static void setUp() {
......
......@@ -7,6 +7,7 @@ import de.monticore.lang.monticar.generator.AbstractSymtabTest;
import de.monticore.lang.monticar.generator.cpp.GeneratorCPP;
import de.monticore.lang.monticar.generator.cpp.mathopt.optimizationSolver.solver.Solver;
import de.monticore.lang.tagging._symboltable.TaggingResolver;
import org.junit.Ignore;
import org.junit.Test;
import java.io.File;
......@@ -19,6 +20,7 @@ import static org.junit.Assert.assertNotNull;
* Test class for Cplex solver
*
*/
@Ignore
public class GeneratorCplexSolverTest extends AbstractSymtabTest {
/**
......
......@@ -13,7 +13,7 @@ component ExistingOptimizationVariableTest{
implementation Math{
// 1. scalar
minimize
Q x = 3;
Q x;
in
2 * x + 1;
subject to
......@@ -34,7 +34,7 @@ component ExistingOptimizationVariableTest{
// 3. substituted
Q c = minimize
minimize
Q^{3,3} a = zeros(3,3);
in
ainc(1, 1) * ainc(2, 2) * ainc(3, 3);
......
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