# \[AMPL 25055\] Warning: variable argument to random function Normal (simulation based optimization)

**URL:** <https://discuss.ampl.com/t/ampl-25055-warning-variable-argument-to-random-function-normal-simulation-based-optimization/1216>\
**Category:** Google Group Mirror\
**Created:** [September 20, 2024, 4:00pm UTC](https://discuss.ampl.com/t/ampl-25055-warning-variable-argument-to-random-function-normal-simulation-based-optimization/1216 "2024-09-20T16:00:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![beanzhaojun](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/beanzhaojun/32/847_2.png) [@beanzhaojun](https://discuss.ampl.com/u/beanzhaojun)\
**Post date:** [September 20, 2024, 4:00pm UTC](https://discuss.ampl.com/t/ampl-25055-warning-variable-argument-to-random-function-normal-simulation-based-optimization/1216/1 "2024-09-20T16:00:39Z")

</div>

Hi everyone,

Thanks very much for your help in advance!

I am trying to write an optimization problem based on simulation. Specifically, I have an unknown variable that is the variance of a normal distribution I would like to solve by maximizing some objective function. This objection function involves a sample mean of some complicated expression, where this sample mean is calculated based on a series (1000) of random draws from the normal distribution.

So I declare a variable, which is the variance first:  
var theta\_y \>= 0.00001;

Then I declare a series of random draws as:  
var state\_y {SIM};

where SIM is just a set of length 1000 (for example) meaning the number of draws.

Then I add the following constraint after the objective function:  
subject to Random\_State {j in SIM}: state\_y[j] = Normal(0, theta\_y);

However, this gives me the following warning:  
Warning:  
line 63 offset 2090  
variable argument to random function Normal  
context: subject to Random\_State {j in SIM}: state\_y[j] = Normal(0, \>\>\> theta\_y) \<\<\< ;

And I cannot solve this optimization problem

Thanks again very much for your help!

All the best,  
Jun

---

<div class="post-metadata">

**Author:** ![4er](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/4er/32/530_2.png) [@4er](https://discuss.ampl.com/u/4er)\
**Post date:** [September 23, 2024, 5:08pm UTC](https://discuss.ampl.com/t/ampl-25055-warning-variable-argument-to-random-function-normal-simulation-based-optimization/1216/2 "2024-09-23T17:08:22Z")

</div>

The AMPL function Normal(mean,var) returns a pseudo-random sample from the normal distribution with specified mean and variance. Called repeatedly, it returns a different value each time:

```auto
ampl: print Normal(0,3.5);
-1.8547285751205824
ampl: print Normal(0,3.5);
5.316445484560852
ampl: print Normal(0,3.5);
-2.078602454572513

```

Normal can be called from AMPL to generate random data for a model, but it is not accessible to solvers. Thus models that apply Normal to decision variables are rejected:

```auto
ampl: option solver knitro;
ampl: solve;
Error executing "solve" command:
error processing constraint Random_State[1]:
	can't evaluate Normal
	(random function of variable argument).

```

If you only want to assign random _initial_ values to the state\_y variables, based on some initial value of theta\_y, that is possible:

```auto
var theta_y >= 0.00001;
var state_y {SIM};
.......
let theta_y := 0.5;
let {j in SIM} state_y[j] := Normal(0, theta_y);
.......
solve;

```

Also, it is possible to use a variable as an argument to a Normal _distribution_ function in a model. Several forms of the normal distribution function are available from AMPL’s [extended function library](https://ampl.com/products/ampl/extended-function-library/).

---

<div class="post-metadata">

**Author:** ![AMPL\_Google\_Group1](https://avatars.discourse-cdn.com/v4/letter/a/a87d85/32.png) [@AMPL\_Google\_Group1](https://discuss.ampl.com/u/AMPL_Google_Group1)\
**Post date:** [September 23, 2024, 5:11pm UTC](https://discuss.ampl.com/t/ampl-25055-warning-variable-argument-to-random-function-normal-simulation-based-optimization/1216/3 "2024-09-23T17:11:15Z")

</div>

Your question has been moved to our new user forum at [discuss.ampl.com](http://discuss.ampl.com), and a response has been posted there. To see the response, use this link:

[https://discuss.ampl.com/t/ampl-25055-warning-variable-argument-to-random-function-normal-simulation-based-optimization/1216/2](https://discuss.ampl.com/t/ampl-25055-warning-variable-argument-to-random-function-normal-simulation-based-optimization/1216/2)

To reply, click the red “Reply” button that follows the response. (Do not send an email reply to this message.)

---

<div class="post-metadata">

**Author:** ![beanzhaojun](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/beanzhaojun/32/847_2.png) [@beanzhaojun](https://discuss.ampl.com/u/beanzhaojun)\
**Post date:** [September 23, 2024, 6:02pm UTC](https://discuss.ampl.com/t/ampl-25055-warning-variable-argument-to-random-function-normal-simulation-based-optimization/1216/4 "2024-09-23T18:02:54Z")

</div>

Thanks very much! Now I understand the issue! I will create a series of random numbers from Uniform distribution, and use the Normal distribution function!

Again, thanks a lot!
