Changeset 6216


Ignore:
Timestamp:
Apr 11, 2014 12:53:02 PM (6 years ago)
Author:
nlandin
Message:

Added import of optimization problems from XML and updated test file with tests. #3598

Location:
branches/NewXMLExport
Files:
22 added
5 edited

Legend:

Unmodified
Added
Removed
  • branches/NewXMLExport/ModelicaCasADiInterface/python/modelica_casadi_transfer_wrapper.py

    r6011 r6216  
    154154   
    155155def _transfer_optimica(ocp, class_name, files, options, log_level):
    156     return modelicacasadi_wrapper._transferOptimizationProblem(ocp, class_name, files, options, log_level)
     156    return modelicacasadi_wrapper.transferXmlOptimization(ocp, class_name, files)
     157    #return modelicacasadi_wrapper._transferOptimizationProblem(ocp, class_name, files, options, log_level)
    157158
    158159
  • branches/NewXMLExport/ModelicaCasADiInterface/src/CMakeLists.txt

    r6011 r6216  
    5252  transferOptimica.cpp
    5353  transferXML.cpp
     54  transferXMLOptimization.cpp
    5455  Variable.cpp
    5556  ${TYPES_CPP}
     
    8384  transferOptimica.hpp
    8485  transferXML.hpp
     86  transferXMLOptimization.cpp
    8587  Variable.hpp
    8688  ${TYPES_HPP}
  • branches/NewXMLExport/ModelicaCasADiInterface/src/transferXML.cpp

    r6194 r6216  
    7575                    transferEquations(m, elem);
    7676                }
     77            } else {
     78                // do nothing
    7779            }
    7880        }
     
    643645            return funcVars.find(varName)->second->getVar();
    644646        } else if (m->getVariable(varName) != NULL) {
     647            std::cout << m->getVariable(varName) << std::endl;
    645648            return m->getVariable(varName)->getVar();
    646649        }
     650        std::cout << "new mx: " << m->getVariable(varName) << std::endl;
    647651        return MX(varName);
    648652    } else if (!strcmp(name, "call")) {
  • branches/NewXMLExport/ModelicaCasADiInterface/swig/ModelicaCasADi.i

    r6011 r6216  
    4747#include "Ref.hpp"
    4848#include "transferXML.hpp"
     49#include "transferXMLOptimization.hpp"
    4950%}
    5051
     
    136137%include "transferOptimica.hpp"
    137138%include "transferXML.hpp"
     139%include "transferXMLOptimization.hpp"
    138140
    139141%extend ModelicaCasADi::SharedNode {
  • branches/NewXMLExport/Python/src/tests_jmodelica/test_casadiinterface_transfer_xml.py

    r6194 r6216  
    3737def load_optimization_problem(*args, **kwargs):
    3838    ocp = OptimizationProblem()
    39     transfer_optimization_problem(ocp, *args, **kwargs)
     39    arglist = []
     40    for arg in args:
     41        arglist.append(arg)
     42    modelname = os.path.join(modelFile, arglist[0])
     43    modelname += '.xml'
     44    modelname = modelname.replace("/", "\\")
     45    print modelname
     46    transfer_optimization_problem(ocp, arglist[0], modelname, **kwargs)
    4047    return ocp
    4148
     
    11241131    assert isinstance(model.getVariable('x3'), BooleanVariable)
    11251132    assert isinstance(model.getVariable('x4'), BooleanVariable)
     1133
     1134
     1135# Optimica tests
     1136def computeStringRepresentationForContainer(myContainer):
     1137    stringRepr = ""
     1138    for index in range(len(myContainer)):
     1139        stringRepr += str(myContainer[index])
     1140    return stringRepr
     1141
     1142@testattr(xml = True)   
     1143def test_OptimicaLessThanPathConstraint():
     1144    optProblem =  load_optimization_problem("atomicOptimizationLEQ", optproblemsFile)
     1145    expected = str(x1) + " <= " + str(MX(1.0))
     1146    assert( computeStringRepresentationForContainer(optProblem.getPathConstraints()) == expected)
     1147
     1148@testattr(xml = True)
     1149def test_OptimicaGreaterThanPathConstraint():
     1150    optProblem =  load_optimization_problem("atomicOptimizationGEQ", optproblemsFile)
     1151    expected = str(x1) + " >= " + str(MX(1.0))
     1152    assert( computeStringRepresentationForContainer(optProblem.getPathConstraints()) == expected)
     1153   
     1154@testattr(xml = True)   
     1155def test_OptimicaSevaralPathConstraints():
     1156    optProblem =  load_optimization_problem("atomicOptimizationGEQandLEQ", optproblemsFile)
     1157    expected = str(x2) + " <= " + str(MX(1.0)) +  str(x1) + " >= " + str(MX(1.0))
     1158    assert( computeStringRepresentationForContainer(optProblem.getPathConstraints()) == expected)   
     1159
     1160@testattr(xml = True)
     1161def test_OptimicaEqualityPointConstraint():
     1162    optProblem =  load_optimization_problem("atomicOptimizationEQpoint", optproblemsFile)
     1163    expected = str(MX("x1(finalTime)")) + " = " + str(MX(1.0))
     1164    assert( computeStringRepresentationForContainer(optProblem.getPointConstraints()) == expected)
     1165   
     1166@testattr(xml = True)   
     1167def test_OptimicaLessThanPointConstraint():
     1168    optProblem =  load_optimization_problem("atomicOptimizationLEQpoint", optproblemsFile)
     1169    expected = str(MX("x1(finalTime)")) + " <= " + str(MX(1.0))
     1170    assert( computeStringRepresentationForContainer(optProblem.getPointConstraints()) == expected)
     1171
     1172@testattr(xml = True)
     1173def test_OptimicaGreaterThanPointConstraint():
     1174    optProblem =  load_optimization_problem("atomicOptimizationGEQpoint", optproblemsFile)
     1175    expected = str(MX("x1(finalTime)")) + " >= " + str(MX(1.0))
     1176    assert( computeStringRepresentationForContainer(optProblem.getPointConstraints()) == expected)
     1177   
     1178@testattr(xml = True)   
     1179def test_OptimicaSevaralPointConstraints():
     1180    optProblem =  load_optimization_problem("atomicOptimizationGEQandLEQandEQpoint", optproblemsFile)
     1181    expected = str(MX("x2(startTime + 1)")) + " <= " + str(MX(1.0)) +  str(MX("x1(startTime + 1)")) + " >= " + str(MX(1.0)) + str(MX("x2(finalTime + 1)")) + " = " + str(MX(1.0))
     1182    assert( computeStringRepresentationForContainer(optProblem.getPointConstraints()) == expected)
     1183   
     1184@testattr(xml = True)   
     1185def test_OptimicaMixedConstraints():
     1186    optProblem =  load_optimization_problem("atomicOptimizationMixedConstraints", optproblemsFile)
     1187    expectedPath = str(MX("x3(startTime + 1)")) + " <= " + str(x1)
     1188    expectedPoint =  str(MX("x2(startTime + 1)")) + " <= " + str(MX(1.0)) +  str(MX("x1(startTime + 1)")) + " >= " + str(MX(1.0))
     1189    assert( computeStringRepresentationForContainer(optProblem.getPathConstraints()) == expectedPath)
     1190    assert( computeStringRepresentationForContainer(optProblem.getPointConstraints()) == expectedPoint)
     1191
     1192@testattr(xml = True)
     1193def test_OptimicaTimedVariables():
     1194    def heurestic_MC_variables_equal(MC_var1, MC_var2):
     1195        return MC_var1.getVar().isEqual(MC_var2.getVar()) and str(MC_var1) == str(MC_var2)
     1196
     1197    optProblem =  load_optimization_problem("atomicOptimizationTimedVariables", optproblemsFile)
     1198    # test there are 3 timed
     1199    timedVars = optProblem.getTimedVariables()
     1200    assert len(timedVars) == 4
     1201
     1202    # test they contain model vars
     1203    x1 = optProblem.getVariable("x1")
     1204    x2 = optProblem.getVariable("x2")
     1205    x3 = optProblem.getVariable("x3")
     1206
     1207    assert heurestic_MC_variables_equal(x1, timedVars[0].getBaseVariable())
     1208    assert heurestic_MC_variables_equal(x2, timedVars[1].getBaseVariable())
     1209    assert heurestic_MC_variables_equal(x3, timedVars[2].getBaseVariable())
     1210    assert heurestic_MC_variables_equal(x1, timedVars[3].getBaseVariable())
     1211       
     1212       
     1213    # Test their time expression has start/final parameter MX in them and
     1214    # that timed variables are lazy.
     1215    startTime = optProblem.getVariable("startTime")
     1216    finalTime = optProblem.getVariable("finalTime")
     1217    path_constraints = optProblem.getPathConstraints()
     1218    point_constraints = optProblem.getPointConstraints()
     1219
     1220    tp1 = timedVars[0].getTimePoint()
     1221    tp2 = timedVars[1].getTimePoint()
     1222    tp3 = timedVars[2].getTimePoint()
     1223    tp4 = timedVars[3].getTimePoint()
     1224
     1225    tv1 = timedVars[0].getVar()
     1226    tv2 = timedVars[1].getVar()
     1227    tv3 = timedVars[2].getVar()
     1228    tv4 = timedVars[3].getVar()
     1229
     1230    assert tp1.getDep(1).isEqual(startTime.getVar())
     1231    assert tp2.getDep(1).isEqual(startTime.getVar())
     1232    assert tp3.getDep(0).isEqual(finalTime.getVar())
     1233    assert tp4.isEqual(finalTime.getVar())
     1234
     1235    assert tv1.isEqual(point_constraints[0].getLhs())
     1236    assert tv2.isEqual(path_constraints[0].getLhs())
     1237    assert tv3.isEqual(path_constraints[1].getLhs())
     1238    assert tv4.isEqual(optProblem.getMayerTerm())   
     1239   
     1240@testattr(xml = True)
     1241def test_OptimicaStartTime():
     1242    optProblem =  load_optimization_problem("atomicOptimizationStart5", optproblemsFile)
     1243    assert( optProblem.getStartTime().getValue() == 5)
     1244   
     1245@testattr(xml = True)   
     1246def test_OptimicaFinalTime():
     1247    optProblem =  load_optimization_problem("atomicOptimizationFinal10", optproblemsFile)
     1248    assert( optProblem.getFinalTime().getValue() == 10)
     1249
     1250@testattr(xml = True)
     1251def test_OptimicaLagrangeTerm():
     1252    optProblem =  load_optimization_problem("atomicLagrangeX1", optproblemsFile)
     1253    assert str(optProblem.getLagrangeTerm()) == str(x1)
     1254    optProblem =  load_optimization_problem("atomicLagrangeNull", optproblemsFile)
     1255    assert str(optProblem.getLagrangeTerm()) == str(MX(0)) 
     1256
     1257@testattr(xml = True)
     1258def test_OptimicaMayerTerm():
     1259    optProblem =  load_optimization_problem("atomicMayerFinalTime", optproblemsFile)
     1260    assert str(optProblem.getMayerTerm()) == str(MX("finalTime"))
     1261    optProblem =  load_optimization_problem("atomicMayerNull", optproblemsFile)
     1262    assert str(optProblem.getMayerTerm()) == str(MX(0))
     1263   
     1264@testattr(xml = True)
     1265def test_OptimicaFree():
     1266    model =  load_optimization_problem("atomicWithFree", optproblemsFile)
     1267    diffs =  model.getVariables(Model.DIFFERENTIATED)
     1268    assert str((diffs[0].getAttribute("free"))) == str(MX(False))
     1269
     1270@testattr(xml = True)
     1271def test_OptimicaInitialGuess():
     1272    model =  load_optimization_problem("atomicWithInitialGuess", optproblemsFile)
     1273    diffs =  model.getVariables(Model.DIFFERENTIATED)
     1274    assert str(diffs[0].getAttribute("initialGuess")) == str(MX(5))
Note: See TracChangeset for help on using the changeset viewer.