Talk:Calculate Results from a Model/Notes
In Synthesis applications, models can represent probabilities, durations or costs, either fixed or time-dependent. They are used by other resources to represent the reliability of a component, the duration of a task, the expected cost of a repair and many other characteristics.
In this tutorial, you'll learn how to obtain calculated results from models.
Prerequisites
Before you begin:
- Create a test repository, perform a simple analysis and then publish a model from the analysis. You'll want to use a published model with this tutorial so you can compare the results returned by the API with the results obtained from the Quick Calculation Pad (QCP)
- Note the published model's object ID number. This will help you trace the response of the API. (To display the object IDs, choose File > Application Setup, then click Other under the Synthesis Settings heading in the navigation panel. Select the Display Object IDs check box. The object ID of the model is displayed in the Resource Manager.)
Tutorial: Calculate Results from a Model
The following example demonstrates how to obtain calculated results from a model resource. A discussion of the example follows.
A VBA version of the code sample is available here.
VB.NET Imports SynthesisAPI Module Module1 Sub Main() 'Connect to a Synthesis repository and project. Dim MyRepository As New Repository MyRepository.ConnectToRepository("C:\RSRepository1.rsr10") 'Replace with name and path to test repository. MyRepository.Project.SetCurrentProject(1) 'Replace with the object ID of test project. 'Retrieve a model from the repository. Dim AModel As cModel AModel = MyRepository.Model.GetModel(21) 'Replace with the object ID of test model. 'Declare some variables to store the results. Dim Result1 As Double Dim Result2 As Double 'Calculate the model's reliability at time = 100 hrs and mean time. Result1 = AModel.Reliability(100) Result2 = AModel.MeanTime 'Set the confidence level to 90% two-sided bounds. 'Declare a string variable for any errors found during this method. Dim ErrorMsg As String AModel.SetConfidenceLevel(0.9, ConfBoundsSides.TwoSidedBoth, False, ErrorMsg) 'To store the confidence bounds results, declare new BoundsValues objects. Dim BResult1 As BoundsValues Dim BResult2 As BoundsValues 'Calculate bounds for the reliability at 100 hrs and the mean time. BResult1 = AModel.Bounds_Reliability(100) BResult2 = AModel.Bounds_MeanTime 'Display the output. MsgBox ("Reliability = " & Result1 & vbNewLine & _ "Upper Bound = " & BResult1.upper & vbNewLine & _ "Lower Bound = " & BResult1.lower & vbNewLine) MsgBox ("Mean Time = " & Result2 & vbNewLine & _ "Upper Bound = " & BResult2.upper & vbNewLine & _ "Lower Bound = " & BResult2.lower & vbNewLine) End Sub End Module
Discussion
Begin by connecting to a Synthesis repository and project (for details, see this tutorial.) The section of code below demonstrates how to connect to a standard repository and open one of its projects. It assumes that a standard repository called "RSRepository1.rsr10" exists in the C drive and that we're accessing project ID#1. If you're copying this section of code, be sure to replace the inputs with appropriate data for your test repository.
'Connect to a Synthesis repository and project. Dim MyRepository As New Repository MyRepository.ConnectToRepository("C:\RSRepository1.rsr10") 'Replace with name and path to test repository. MyRepository.Project.SetCurrentProject(1) 'Replace with the object ID of test project.
========================
1. Create a new module and begin by connecting to a Synthesis repository and project. The following code assumes that a standard repository called "RSRepository1.rsr10" exists in the C drive.
'Connect to the Synthesis repository and set project ID#1 as the current project. Dim MyRepository As New Repository MyRepository.ConnectToRepository("C:\RSRepository1.rsr10") MyRepository.Project.SetCurrentProject(1)
2. Retrieve a model from the repository. The following code assumes that the repository contains a model with ID number 21.
VBA 'Retrieve the model with ID# 21 from the repository. Dim Amodel As New cModel Set AModel = MyRepository.Model.GetModel(21)
VB.NET 'Retrieve the model with ID# 21 from the repository. Dim Amodel As New cModel AModel = MyRepository.Model.GetModel(21)
2. Use the Reliability
method to calculate for the reliability at 100 hrs and the MeanTime
method to return the mean time to failure.
'Declare variables. Dim Result1 As Double Dim Result2 As Double 'Calculate the model's reliability at time = 100 hrs and mean time. Result1 = AModel.Reliability(100) Result2 = AModel.MeanTime
3. Let's add confidence bounds calculations to the results. Use the SetConfidenceLevel
method to specify 90% two-sided confidence bounds.
VBA 'Set the confidence level to 90% two-sided bounds. 'Declare a string variable for any errors found during this method. Dim ErrorMsg As String Call AModel.SetConfidenceLevel(0.9, ConfBoundsSides_TwoSidedBoth, False, ErrorMsg) 'Initiate new instances of the BoundsValues class. Dim BResult1 As BoundsValues Dim BResult2 As BoundsValues 'Calculate bounds for the reliability at 100 hrs and the mean time. Set BResult1 = AModel.Bounds_Reliability(100) Set BResult2 = AModel.Bounds_MeanTime 'Display the output. MsgBox ("Reliability = " & Result1 & vbNewLine & _ "Upper Bound = " & BResult1.upper & vbNewLine & _ "Lower Bound = " & BResult1.lower & vbNewLine) MsgBox ("Mean Time = " & Result2 & vbNewLine & _ "Upper Bound = " & BResult2.upper & vbNewLine & _ "Lower Bound = " & BResult2.lower & vbNewLine)
VB.NET 'Set the confidence level to 90% two-sided bounds. 'Declare a string variable for any errors found during this method. Dim ErrorMsg As String AModel.SetConfidenceLevel(0.9, ConfBoundsSides.TwoSidedBoth, False, ErrorMsg) 'Initiate new instances of the BoundsValues class. Dim BResult1 As BoundsValues Dim BResult2 As BoundsValues 'Calculate bounds for the reliability at 100 hrs and the mean time. BResult1 = AModel.Bounds_Reliability(100) BResult2 = AModel.Bounds_MeanTime 'Display the output. MsgBox ("Reliability = " & Result1 & vbNewLine & _ "Upper Bound = " & BResult1.upper & vbNewLine & _ "Lower Bound = " & BResult1.lower & vbNewLine) MsgBox ("Mean Time = " & Result2 & vbNewLine & _ "Upper Bound = " & BResult2.upper & vbNewLine & _ "Lower Bound = " & BResult2.lower & vbNewLine)
Test the Code
Below are the complete code lists for this example. To test them, run the application by clicking Start on the Debug menu. Compare the results returned by the API with the results obtained from the Quick Calculation Pad (QCP).
VBA Sub Main() 'Connect to the Synthesis repository and set project ID#1 as the current project. Dim MyRepository As New Repository MyRepository.ConnectToRepository("C:\RSRepository1.rsr10") MyRepository.Project.SetCurrentProject(1) 'Retrieve the model with ID# 21 from the repository. Dim AModel As cModel Set AModel = MyRepository.Model.GetModel(21) 'Declare variables. Dim Result1 As Double Dim Result2 As Double 'Calculate the model's reliability at time = 100 hrs and mean time. Result1 = AModel.Reliability(100) Result2 = AModel.MeanTime 'Set the confidence level to 90% two-sided bounds. 'Declare a string variable for any errors found during this method. Dim ErrorMsg As String Call AModel.SetConfidenceLevel(0.9, ConfBoundsSides_TwoSidedBoth, False, ErrorMsg) 'Initiate new instances of the BoundsValues class. Dim BResult1 As BoundsValues Dim BResult2 As BoundsValues 'Calculate bounds for the reliability at 100 hrs and the mean time. Set BResult1 = AModel.Bounds_Reliability(100) Set BResult2 = AModel.Bounds_MeanTime 'Display the output. MsgBox ("Reliability = " & Result1 & vbNewLine & _ "Upper Bound = " & BResult1.upper & vbNewLine & _ "Lower Bound = " & BResult1.lower & vbNewLine) MsgBox ("Mean Time = " & Result2 & vbNewLine & _ "Upper Bound = " & BResult2.upper & vbNewLine & _ "Lower Bound = " & BResult2.lower & vbNewLine) End Sub
Notes
See cModel
class for a list of all available calculation methods for models.
References
To learn more, see the reference documentation for the class and methods discussed in this tutorial: