Main Content

이 번역 페이지는 최신 내용을 담고 있지 않습니다. 최신 내용을 영문으로 보려면 여기를 클릭하십시오.

스케줄 편집기 API를 사용하여 모델의 무작위 스케줄 생성 및 분석하기

이 예제에서는 스케줄 편집기 API를 사용하여 스케줄에 대한 작업을 수행합니다. 그런 다음, 함수를 사용하여 무작위 스케줄을 생성하고 시뮬레이션 데이터 인스펙터에서 분석합니다.

모델 열기 및 Schedule 객체 가져오기

Throttle Position Control 시스템의 모델을 열고 get_param을 사용하여 simulink.schedule.OrderedSchedule 객체를 가져옵니다. 이 객체에는 현재 스케줄이 포함되어 있습니다.

model = 'ScheduleEditorAPIWithSubsystemPartitions';
open_system(model);
schedule = get_param(model, 'Schedule')
schedule = 

  OrderedSchedule with properties:

           Order: [9x3 table]
    RateSections: [3x1 simulink.schedule.RateSection]
          Events: [0x1 simulink.schedule.Event]
     Description: ''

Schedule 객체 검토하기

schedule 객체에는 모델 내 분할의 실행 순서가 포함된 Order 속성이 있습니다. Order 속성은 분할 이름, 인덱스, 유형 및 트리거가 포함된 테이블을 표시합니다.

schedule.Order
ans =

  9x3 table

                          Index      Type      Trigger
                          _____    ________    _______

    Cont                    1      Periodic    "0"    
    TPSSecondaryRun5ms      2      Periodic    "0.005"
    MonitorRun5ms           3      Periodic    "0.005"
    ControllerRun5ms        4      Periodic    "0.005"
    ActuatorRun5ms          5      Periodic    "0.005"
    D2                      6      Periodic    "0.005"
    D3                      7      Periodic    "0.01" 
    APPSnsrRun              8      Periodic    "0.01" 
    TPSPrimaryRun10ms       9      Periodic    "0.01" 

Order 테이블의 인덱스 변수를 사용하여 모델의 실행 순서를 변경합니다.

schedule.Order.Index('ActuatorRun5ms') = 2;
schedule.Order
ans =

  9x3 table

                          Index      Type      Trigger
                          _____    ________    _______

    Cont                    1      Periodic    "0"    
    ActuatorRun5ms          2      Periodic    "0.005"
    TPSSecondaryRun5ms      3      Periodic    "0.005"
    MonitorRun5ms           4      Periodic    "0.005"
    ControllerRun5ms        5      Periodic    "0.005"
    D2                      6      Periodic    "0.005"
    D3                      7      Periodic    "0.01" 
    APPSnsrRun              8      Periodic    "0.01" 
    TPSPrimaryRun10ms       9      Periodic    "0.01" 

스케줄을 수정하기 위해 Order 속성 내에서 순서를 이동한 경우 그 결과로 생성되는 스케줄이 유효해야 합니다. 보다 쉽게 스케줄을 수정하고 유효한 순서로 이동시킬 수 있도록 각 분할은 RateSections 속성에서 동일한 레이트를 갖는 분할들로 그룹화됩니다. RateSection 속성의 각 요소에는 동일한 레이트의 분할이 들어있는 순서 테이블이 포함되어 있습니다.

schedule.RateSections(2)
schedule.RateSections(2).Order
ans = 

  RateSection with properties:

     Rate: "0.005"
    Order: [5x3 table]


ans =

  5x3 table

                          Index      Type      Trigger
                          _____    ________    _______

    ActuatorRun5ms          2      Periodic    "0.005"
    TPSSecondaryRun5ms      3      Periodic    "0.005"
    MonitorRun5ms           4      Periodic    "0.005"
    ControllerRun5ms        5      Periodic    "0.005"
    D2                      6      Periodic    "0.005"

인덱스 변수를 사용하여 RateSections 내에서 분할을 이동합니다.

schedule.RateSections(2).Order.Index('ActuatorRun5ms') = 5;
schedule.Order
ans =

  9x3 table

                          Index      Type      Trigger
                          _____    ________    _______

    Cont                    1      Periodic    "0"    
    TPSSecondaryRun5ms      2      Periodic    "0.005"
    MonitorRun5ms           3      Periodic    "0.005"
    ControllerRun5ms        4      Periodic    "0.005"
    ActuatorRun5ms          5      Periodic    "0.005"
    D2                      6      Periodic    "0.005"
    D3                      7      Periodic    "0.01" 
    APPSnsrRun              8      Periodic    "0.01" 
    TPSPrimaryRun10ms       9      Periodic    "0.01" 

무작위 스케줄을 생성하는 함수 만들기

이 섹션에서는 3개의 함수 randomSchedule, generateSimulationInputs, simulateRandomSchedules를 만듭니다.

randomSchedule 함수는 임의 치환을 사용하여 schedule 객체의 인덱스를 수정하여 무작위 스케줄을 만듭니다. schedule 객체의 Order 속성과 RateSections 속성을 사용하여 스케줄 내 분할을 서로 다르게 무작위로 조합된 순서로 이동시킵니다. 이렇게 무작위로 생성된 스케줄을 사용하여 모델을 시뮬레이션하고 비교하여 서로 다른 스케줄이 시뮬레이션에 미치는 효과를 연구합니다. 함수 randomSchedule에서 입력값은 모델 이름입니다. 그런 다음, get_param을 사용하여 모델의 simulink.schedule.OrderedSchedule 객체를 가져옵니다. schedule 객체와 그 속성을 사용하여 스케줄을 수정하고 무작위로 생성된 스케줄을 만듭니다. 모델의 첫 번째 레이트 섹션에 대해 변수 firstExecutionOrder를 만듭니다. rateSections(1).ExecutionOrder = [firstExecutionOrder(1,:); reSchedule(firstExecutionOrder(2:end,:))] 코드 라인은 인덱스의 임의 치환을 만드는 함수 reSchedule을 호출합니다.

type randomSchedule
function schedule = randomSchedule(model)
    % schedule = randomSchedule(model) Produces a
    % simulink.schedule.OrderedSchedule that has a randomized permutation
    % of the model's original execution order schedule
    
    arguments
        model char = bdroot
    end
    
    schedule = get_param(model, 'Schedule');
    
    rateSections = schedule.RateSections;
    firstOrder = rateSections(1).Order;
    
    % This assumes that the slowest discrete rate is at index 1. This may
    % not be the case for all models (ex. JMAAB-B).
    rateSections(1).Order = [firstOrder(1,:); reSchedule(firstOrder(2:end,:))];    
    
    for i=2:length(rateSections)
        rateSections(i).Order = reSchedule(rateSections(i).Order);
    end
    
    schedule.RateSections = rateSections;
end

function out = reSchedule(in)
    numPartitions = height(in);
    in.Index = in.Index(randperm(numPartitions));
    out = in;
end

서로 다른 스케줄이 모델에 미치는 효과를 분석하기 위해 여러 다른 스케줄로 모델을 시뮬레이션합니다. 이 함수에서 Simulink.SimulationInput 객체로 구성된 배열을 만듭니다. Simulink.SimulationInput 객체로 구성된 이 배열을 통해 Simulink.SimulationInput 객체의 setModelParameters 메서드를 사용하여 모델에 스케줄을 적용할 수 있습니다.

type generateSimulationInputs
function in = generateSimulationInputs(model, numSimulations)
    % in = generateSimulationInputs(model, numSimulations) Generates
    % numSimulations Simulink.SimulationInput objects each containing a
    % different, randomized execution order schedule
    arguments
        model char = bdroot
        numSimulations double = 10
    end
    
    in(numSimulations) = Simulink.SimulationInput();
    in = in.setModelName(model);
    for idx = 1:numSimulations
        in(idx) = in(idx).setModelParameter('Schedule', randomSchedule(model));
    end
end

마지막 함수에서 Simulink.SimulationInput 객체로 구성된 배열을 사용하여 여러 시뮬레이션을 실행합니다. 시뮬레이션이 완료되면 시뮬레이션 데이터 인스펙터에서 모든 시뮬레이션의 출력을 플로팅할 수 있습니다.

type simulateRandomSchedules
function out = simulateRandomSchedules(model, numSimulations)
    % out = simulateRandomSchedules(model, numSimulations) Simulates a 
    % model numSimulations number of times.  Each simulation has a
    % randomized execution order schedule.
    arguments
        model char = bdroot
        numSimulations double = 10
    end
        
    in = generateSimulationInputs(model, numSimulations);
    out = sim(in);
    plot(out);
end

함수 실행하기

이제 ScheduleEditorAPIWithSubsystemPartitions 모델에 대해 위 함수를 실행합니다. 먼저 randomSchedule 함수를 사용하여 무작위로 생성된 스케줄을 만들고 generateSimulationInputs 함수를 사용하여 Simulink.SimulationInput 객체로 구성된 배열을 생성한 다음, simulateRandomSchedule 함수를 사용하여 여러 스케줄로 모델을 시뮬레이션하고 비교를 위해 결과를 플로팅합니다. 무작위로 생성된 15개의 스케줄을 사용하여 시뮬레이션을 실행해 보겠습니다.

simulateRandomSchedules(model,15)
[27-Jul-2023 18:35:41] Running simulations...
[27-Jul-2023 18:35:53] Completed 1 of 15 simulation runs
[27-Jul-2023 18:35:55] Completed 2 of 15 simulation runs
[27-Jul-2023 18:35:56] Completed 3 of 15 simulation runs
[27-Jul-2023 18:35:58] Completed 4 of 15 simulation runs
[27-Jul-2023 18:35:59] Completed 5 of 15 simulation runs
[27-Jul-2023 18:36:00] Completed 6 of 15 simulation runs
[27-Jul-2023 18:36:01] Completed 7 of 15 simulation runs
[27-Jul-2023 18:36:02] Completed 8 of 15 simulation runs
[27-Jul-2023 18:36:03] Completed 9 of 15 simulation runs
[27-Jul-2023 18:36:04] Completed 10 of 15 simulation runs
[27-Jul-2023 18:36:05] Completed 11 of 15 simulation runs
[27-Jul-2023 18:36:06] Completed 12 of 15 simulation runs
[27-Jul-2023 18:36:07] Completed 13 of 15 simulation runs
[27-Jul-2023 18:36:08] Completed 14 of 15 simulation runs
[27-Jul-2023 18:36:09] Completed 15 of 15 simulation runs

ans = 

1x15 Simulink.SimulationOutput array