Advertisement
zk69

Linear Controller

Apr 30th, 2024
751
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.34 KB | None | 0 0
  1. #include "../flowstar/Continuous.h"
  2. #include<fstream>
  3. #include<ctime>
  4.  
  5. using namespace std;
  6. using namespace flowstar;
  7.  
  8. int main(int argc, char* argv[])
  9. {
  10.     // Declaration of the state variables.
  11.     unsigned int numVars = 3;
  12.  
  13.     int x0_id = stateVars.declareVar("x0");
  14.     int x1_id = stateVars.declareVar("x1");
  15.     int u_id = stateVars.declareVar("u");
  16.  
  17.     int domainDim = numVars + 1;
  18.  
  19.     // Define the continuous dynamics.
  20.     Expression_AST<Real> deriv_x0("40 - x1");  // theta_r = 0
  21.     Expression_AST<Real> deriv_x1("-0.2*x1 + u");
  22.     Expression_AST<Real> deriv_u("0");
  23.  
  24.     vector<Expression_AST<Real>> ode_rhs(numVars);
  25.     ode_rhs[x0_id] = deriv_x0;
  26.     ode_rhs[x1_id] = deriv_x1;
  27.     ode_rhs[u_id] = deriv_u;
  28.  
  29.     Deterministic_Continuous_Dynamics dynamics(ode_rhs);
  30.  
  31.     // Specify the parameters for reachability computation.
  32.     Computational_Setting setting;
  33.  
  34.     unsigned int order = 12;
  35.     setting.setFixedStepsize(0.01, order);
  36.     setting.setTime(0.1);
  37.     setting.setCutoffThreshold(1e-10);
  38.     setting.setQueueSize(1000);
  39.     setting.setRemainderEstimation(vector<Interval>(numVars, Interval(-0.01, 0.01)));
  40.     setting.prepare();
  41.  
  42.     float state_1 = std::stof(argv[1]);
  43.     float state_2 = std::stof(argv[2]);
  44.    
  45.     Interval init_x0(state_1 - 1, state_1 + 1), init_x1(state_2 - 2, state_2 + 2), init_u(0);
  46.     vector<Interval> X0 = {init_x0, init_x1, init_u};
  47.    
  48.     Flowpipe initial_set(X0);
  49.  
  50.     // Result of the reachability computation
  51.     Result_of_Reachability result;
  52.    
  53.     string theta_1 = argv[3];
  54.     string theta_2 = argv[4];
  55.     int step_num = std::stoi(argv[5]);
  56.  
  57.     for (int iter = 0; iter < step_num; ++iter)
  58.     {
  59.         vector<Interval> box;
  60.         initial_set.intEval(box, order, setting.tm_setting.cutoff_threshold);
  61.  
  62.         string strExpU = "x0*" + theta_1 + "+x1*" + theta_2;
  63.  
  64.         Expression_AST<Real> exp_u(strExpU);
  65.  
  66.         TaylorModel<Real> tm_u;
  67.         exp_u.evaluate(tm_u, initial_set.tmvPre.tms, order, initial_set.domain, setting.tm_setting.cutoff_threshold, setting.g_setting);
  68.  
  69.         initial_set.tmvPre.tms[u_id] = tm_u;
  70.  
  71.         dynamics.reach(result, setting, initial_set, vector<Constraint>());
  72.  
  73.         if (result.status == COMPLETED_SAFE || result.status == COMPLETED_UNSAFE || result.status == COMPLETED_UNKNOWN)
  74.         {
  75.             initial_set = result.fp_end_of_time;
  76.         }
  77.         else
  78.         {
  79.             printf("Terminated due to too large overestimation.\n");
  80.             break;
  81.         }
  82.     }
  83.  
  84.     ofstream result_output("./outputs/reach_result.txt");
  85.     vector<Interval> end_box;
  86.     result.fp_end_of_time.intEval(end_box, order, setting.tm_setting.cutoff_threshold);
  87.     string reach_result = "Verification result: Unknown(" + std::to_string(step_num) + ")";
  88.    
  89.     if (end_box[0].inf() >= -0.4 && end_box[0].sup() <= -0.28 && end_box[1].inf() >= 0.05 && end_box[1].sup() <= 0.22)
  90.     {
  91.         reach_result = "Verification result: Yes(" + std::to_string(step_num) + ")";
  92.     }
  93.     else if (end_box[0].inf() >= 0.28 || end_box[0].sup() <= -0.4 || end_box[1].inf() >= 0.22 || end_box[1].sup() <= 0.05)
  94.     {
  95.         reach_result = "Verification result: No(" + std::to_string(step_num) + ")";
  96.     }
  97.  
  98.     if (result_output.is_open())
  99.     {
  100.         result_output << reach_result << endl;
  101.     }
  102.  
  103.     return 0;
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement