|
| 1 | +/** Model-based Prognoser - Body |
| 2 | + * @class BenchmarkPrognoser BenchmarkPrognoser.h Benchmark.h |
| 3 | + * |
| 4 | + * @brief Benchmark Prognoser Class |
| 5 | + * |
| 6 | + * BenchmarkPrognoser class. |
| 7 | + * |
| 8 | + * @author Micah Ricks |
| 9 | + * @version 0.1.0 |
| 10 | + * |
| 11 | + * |
| 12 | + * Contact: Micah Ricks (mricks1@bulldogs.aamu.edu) |
| 13 | + * Created: January 31, 2017 |
| 14 | + * |
| 15 | + * @copyright Copyright (c) 2017 United States Government as represented by |
| 16 | + * the Administrator of the National Aeronautics and Space Administration. |
| 17 | + * All Rights Reserved. |
| 18 | + */ |
| 19 | + |
| 20 | + #include <stdio.h> |
| 21 | + #include <stdlib.h> |
| 22 | + #include <cstddef> |
| 23 | + |
| 24 | + #include <memory> |
| 25 | + #include <vector> |
| 26 | + |
| 27 | + #include "Benchmark.h" |
| 28 | + #include "SharedLib.h" |
| 29 | + #include "BenchmarkPrognoser.h" |
| 30 | + #include "ObserverFactory.h" |
| 31 | + #include "PredictorFactory.h" |
| 32 | + #include "PrognosticsModelFactory.h" |
| 33 | + #include "UData.h" |
| 34 | + #include "CommManager.h" |
| 35 | + #include "GSAPConfigMap.h" |
| 36 | + |
| 37 | +namespace PCOE { |
| 38 | + // Configuration Keys |
| 39 | + const std::string MODEL_KEY = "model"; |
| 40 | + const std::string OBSERVER_KEY = "observer"; |
| 41 | + const std::string PREDICTOR_KEY = "predictor"; |
| 42 | + const std::string EVENT_KEY = "Model.event"; |
| 43 | + const std::string NUMSAMPLES_KEY = "Predictor.numSamples"; |
| 44 | + const std::string HORIZON_KEY = "Predictor.horizon"; |
| 45 | + const std::string PREDICTEDOUTPUTS_KEY = "Model.predictedOutputs"; |
| 46 | + const std::string INPUTS_KEY = "inputs"; |
| 47 | + const std::string OUTPUTS_KEY = "outputs"; |
| 48 | + Benchmark benchmark1,benchmark2; |
| 49 | + unsigned long long const INIT_TIME=0; |
| 50 | + |
| 51 | + BenchmarkPrognoser::BenchmarkPrognoser(GSAPConfigMap & configMap) : CommonPrognoser(configMap), initialized(false) { |
| 52 | + // Check for required config parameters |
| 53 | + configMap.checkRequiredParams({ MODEL_KEY,OBSERVER_KEY,PREDICTOR_KEY,EVENT_KEY,NUMSAMPLES_KEY,HORIZON_KEY,PREDICTEDOUTPUTS_KEY,INPUTS_KEY,OUTPUTS_KEY }); |
| 54 | + /// TODO(CT): Move Model, Predictor subkeys into Model/Predictor constructor |
| 55 | + |
| 56 | + // Create Model |
| 57 | + log.WriteLine(LOG_DEBUG, moduleName, "Creating Model"); |
| 58 | + PrognosticsModelFactory & pProgModelFactory = PrognosticsModelFactory::instance(); |
| 59 | + model = std::unique_ptr<PrognosticsModel>(pProgModelFactory.Create(configMap[MODEL_KEY][0], configMap)); |
| 60 | + |
| 61 | + // Create Observer |
| 62 | + log.WriteLine(LOG_DEBUG, moduleName, "Creating Observer"); |
| 63 | + ObserverFactory & pObserverFactory = ObserverFactory::instance(); |
| 64 | + observer = std::unique_ptr<Observer>(pObserverFactory.Create(configMap[OBSERVER_KEY][0], configMap)); |
| 65 | + |
| 66 | + // Create Predictor |
| 67 | + log.WriteLine(LOG_DEBUG, moduleName, "Creating Predictor"); |
| 68 | + PredictorFactory & pPredictorFactory = PredictorFactory::instance(); |
| 69 | + predictor = std::unique_ptr<Predictor>(pPredictorFactory.Create(configMap[PREDICTOR_KEY][0], configMap)); |
| 70 | + |
| 71 | + // Set model for observer and predictor |
| 72 | + observer->setModel(model.get()); |
| 73 | + predictor->setModel(model.get()); |
| 74 | + |
| 75 | + // Set configuration parameters |
| 76 | + unsigned int numSamples = static_cast<unsigned int>(std::stoul(configMap[NUMSAMPLES_KEY][0])); |
| 77 | + unsigned int horizon = static_cast<unsigned int>(std::stoul(configMap[HORIZON_KEY][0])); |
| 78 | + std::string event = configMap[EVENT_KEY][0]; |
| 79 | + std::vector<std::string> predictedOutputs = configMap[PREDICTEDOUTPUTS_KEY]; |
| 80 | + |
| 81 | + // Set inputs and outputs |
| 82 | + inputs = configMap[INPUTS_KEY]; |
| 83 | + outputs = configMap[OUTPUTS_KEY]; |
| 84 | + |
| 85 | + // Create progdata |
| 86 | + results.setUncertainty(UType::Samples); // @todo(MD): do not force samples representation |
| 87 | + results.addEvent(event); // @todo(MD): do not assume only a single event |
| 88 | + results.addSystemTrajectories(predictedOutputs); // predicted outputs |
| 89 | + results.setPredictions(1, horizon); // interval, number of predictions |
| 90 | + results.setupOccurrence(numSamples); |
| 91 | + results.events[event].timeOfEvent.npoints(numSamples); |
| 92 | + results.sysTrajectories.setNSamples(numSamples); |
| 93 | + } |
| 94 | + |
| 95 | + void BenchmarkPrognoser::step() { |
| 96 | + |
| 97 | + //init time in nanoseconds |
| 98 | + if(benchmark2.begin != INIT_TIME) |
| 99 | + { |
| 100 | + benchmark2.nanosecondsEnd(); |
| 101 | + benchmark2.findElapsedTime(); |
| 102 | + |
| 103 | + } |
| 104 | + benchmark1.nanosecondsBegin(); |
| 105 | + |
| 106 | + static double initialTime = comm.getValue(outputs[0]).getTime() / 1.0e3; |
| 107 | + |
| 108 | + // Get new relative time (convert to seconds) |
| 109 | + // @todo(MD): Add config for time units so conversion is not hard-coded |
| 110 | + double newT = comm.getValue(outputs[0]).getTime() / 1.0e3 - initialTime; |
| 111 | + |
| 112 | + // Fill in input and output data |
| 113 | + log.WriteLine(LOG_DEBUG, moduleName, "Getting data in step"); |
| 114 | + std::vector<double> u(model->getNumInputs()); |
| 115 | + std::vector<double> z(model->getNumOutputs()); |
| 116 | + for (unsigned int i = 0; i < model->getNumInputs(); i++) { |
| 117 | + u[i] = comm.getValue(inputs[i]); |
| 118 | + } |
| 119 | + for (unsigned int i = 0; i < model->getNumOutputs(); i++) { |
| 120 | + z[i] = comm.getValue(outputs[i]); |
| 121 | + } |
| 122 | + |
| 123 | + // If this is the first step, will want to initialize the observer and the predictor |
| 124 | + if (!initialized) { |
| 125 | + log.WriteLine(LOG_DEBUG, moduleName, "Initializing BenchmarkPrognoser"); |
| 126 | + std::vector<double> x(model->getNumStates()); |
| 127 | + model->initialize(x, u, z); |
| 128 | + observer->initialize(newT, x, u); |
| 129 | + initialized = true; |
| 130 | + lastTime = newT; |
| 131 | + } else { |
| 132 | + // If time has not advanced, skip this step |
| 133 | + if (newT <= lastTime) { |
| 134 | + log.WriteLine(LOG_TRACE, moduleName, "Skipping step because time did not advance."); |
| 135 | + |
| 136 | + benchmark1.nanosecondsEnd(); |
| 137 | + benchmark1.findElapsedTime(); |
| 138 | + benchmark2.nanosecondsBegin(); |
| 139 | + return; |
| 140 | + } |
| 141 | + |
| 142 | + // Run observer |
| 143 | + log.WriteLine(LOG_DEBUG, moduleName, "Running Observer Step"); |
| 144 | + observer->step(newT, u, z); |
| 145 | + log.WriteLine(LOG_DEBUG, moduleName, "Done Running Observer Step"); |
| 146 | + |
| 147 | + // Run predictor |
| 148 | + log.WriteLine(LOG_DEBUG, moduleName, "Running Prediction Step"); |
| 149 | + // Set up state |
| 150 | + std::vector<UData> stateEst = observer->getStateEstimate(); |
| 151 | + predictor->predict(newT, stateEst, results); |
| 152 | + log.WriteLine(LOG_DEBUG, moduleName, "Done Running Prediction Step"); |
| 153 | + |
| 154 | + // Set lastTime |
| 155 | + lastTime = newT; |
| 156 | + |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | +//destructor |
| 161 | +BenchmarkPrognoser::~BenchmarkPrognoser() { |
| 162 | + benchmark1.clearFile(); |
| 163 | + benchmark1.printTemp(); |
| 164 | + benchmark1.printScreen(); |
| 165 | + benchmark2.printScreen(); |
| 166 | + benchmark1.writeFile(); |
| 167 | + benchmark2.writeFile(); |
| 168 | +} |
| 169 | + |
| 170 | +} |
0 commit comments