BookmarkSubscribeSubscribe to RSS Feed
fugalnc

Community Trekker

Joined:

Nov 12, 2015

Programatically set profiler simulator distributions

I'm trying to make a Profiler with simulator options turned on and some of the inputs have random distributions. The type of distribution and parameters of that distribution are saved as table variables to be querried as necessary. But the JSL inside the Profiler() function doesn't seem to recognize if statements.

 

For example, this code works fine

Profiler(
  Y( :a, :b ),
  Profiler(
    1,
    Simulator(
      1,
      Factors( 
        c << Fixed( 3.5 ), 
        d << Random( Normal( :dist_p1, :dist_p2 ) )
      ),
      Resimulate
    )
  )
)

but this code doesn't load the distribution info for "d"

Profiler(
  Y( :a, :b ),
  Profiler(
    1,
    Simulator(
      1,
      Factors( 
        c << Fixed( 3.5 ), 
        if(
          dist_type == "Normal", d << Random( Normal( :dist_p1, :dist_p2 ) ),
          dist_type == "Weibull", d << Random( Weibull( :dist_p1, :dist_p2 ) )
        )
      ),
      Resimulate
    )
  )
)

 

1 ACCEPTED SOLUTION

Accepted Solutions
fugalnc

Community Trekker

Joined:

Nov 12, 2015

Solution

Re: Programatically set profiler simulator distributions

Ok, if I go back to the highest level function (Profiler in this case) and wrap it with with Eval, Substitute, and Expr I can get the behavior I want. It was confusing to me because I thought that the if should be evaluated first, then used as the argument of the function it was inside of, but I can understand now why that wouldn't work.

 

Eval(
  Substitute(
    Expr(
      Profiler(
        Y( :a, :b ),
        Simulator(
          Factors( 
            c << Fixed( 3.5 ), 
            d_
          ),
          Resimulate
        )
      )
    ),
    Expr( _d_ ), 
    if (:dist_type == "Normal", Parse( "d << Random( Normal( :dist_p1, :dist_p2 ))" ),
        :dist_type == "Weibull", Parse( "d << Random( Weibull( :dist_p2, :dist_p1 ))" )
    )
  )
)
3 REPLIES
David_Burnham

Super User

Joined:

Jul 13, 2011

Re: Programatically set profiler simulator distributions

Profiler is a function.  Everyhting inside the function should be function arguments required for the function to run.  So you can't just stick arbitrary JSL code inside it.  You need to move the conditional logic outside:

type = "Normal";
p1 = 3.0;
p2 = 0.5;
If (type == "Normal",
	Profiler(
	  Y( :a, :b ),
	  Profiler(
		1,
		Simulator(
		  1,
		  Factors( 
			c << Fixed( 3.5 ), 
			d << Random( Normal( p1, p2 ) ),
		  ),
		  Resimulate
		)
	  )
	)
);

 

-Dave
fugalnc

Community Trekker

Joined:

Nov 12, 2015

Solution

Re: Programatically set profiler simulator distributions

Ok, if I go back to the highest level function (Profiler in this case) and wrap it with with Eval, Substitute, and Expr I can get the behavior I want. It was confusing to me because I thought that the if should be evaluated first, then used as the argument of the function it was inside of, but I can understand now why that wouldn't work.

 

Eval(
  Substitute(
    Expr(
      Profiler(
        Y( :a, :b ),
        Simulator(
          Factors( 
            c << Fixed( 3.5 ), 
            d_
          ),
          Resimulate
        )
      )
    ),
    Expr( _d_ ), 
    if (:dist_type == "Normal", Parse( "d << Random( Normal( :dist_p1, :dist_p2 ))" ),
        :dist_type == "Weibull", Parse( "d << Random( Weibull( :dist_p2, :dist_p1 ))" )
    )
  )
)
fugalnc

Community Trekker

Joined:

Nov 12, 2015

Re: Programatically set profiler simulator distributions

I thought about this approach briefly, but in my real file it would be duplicating 100+ lines of code 9 times (3 random variables, 3 distribution types). It becomes unmanageable very quickly.

 

EDIT: This is in relply to David_Burnham. Still learning how to use these forums...