1

I am fairly new to Anylogic and I am trying to figure out how to use the "CustomDistributionOfOptions" constructor to manually create a random distribution of items defined in an "Options list". My goal is to dynamically define the custom distribution of the options-list-items based on a user input. After researching solutions online on how to dynamically define a custom distribution, I came upon this solution: Dynamically Changing Distribution in AnyLogic. In this post, the user uses a constructor to create a custom distribution, which is what I want to do as well. However, every single time I try and initialize the constructor in my "Main" agent as an action on startup, I keep getting an error that states that that method is undefined for the type Main.

I do not understand why this error keeps popping up, as the documentation states that I can just use the function "CustomDistributionOfOptions()" as a constructor. Please let me know what I am not understanding and/or missing.

  • Please provide the code of your attempt, and the error as code-formatted text in your question. Don't rely on external source, nor use images for things that are text. In any case, the error suggest you're trying to call a **method** `CustomDistributionOfOptions_Distribution()` on the class `Main` instead of invoking a constructor (e.g. using `new Distribution(...)`) – Mark Rotteveel Jun 14 '22 at 17:33
  • Well, you seem to use `CustomDistributionOfOptions_Distribution()` instead of `CustomDistributionOfOptions()`. The former is not known to AL, hence the error. You must use the constructor exactly as defined in the help. If that is too confusing, study some base materials on Java constructors, this is all Java stuff in the end :) – Benjamin Jun 15 '22 at 05:46
  • @MarkRotteveel The code that Ia m using is as follows: 'CustomDistributionOfOptions();' And the error it is giving me is as follows: "Description: The method CustomDistributionOfOptions() is undefined for the type Main. Location: ####### - Agent Type" – IE_programmer Jun 15 '22 at 12:29
  • @Benjamin Thanks for your reply. I just realized I made that syntax error, however, I still seem to be getting the same error... I think you might be correct about my lack of knowledge on Java constructors, is there any specific source material that you could recommend? – IE_programmer Jun 15 '22 at 12:34
  • Invoking a constructor requires using the keyword `new`. Please show a [mre]. – Mark Rotteveel Jun 15 '22 at 13:47
  • @MarkRotteveel the minimal reproducible example is: `new CustomDistributionOfOptions();` which I have implemented in the "On startup" field in the "Main - Agent Type" properties window. This, however, still gives the following error: *Description: The constructor CustomDistributionOfOptions() is undefined. Location: #### Main - Agent Type* – IE_programmer Jun 17 '22 at 08:06
  • The question you link (and the [documentation linked](https://anylogic.help/api/com/anylogic/engine/CustomDistribution.html?resultof=%22custom%22%20%22distribution%22%20%22distribut%22%20) from the answer to that question) indicate the class is called `CustomDistribution`, so the constructor is called `CustomDistribution` (refer to the link to see which constructors are available). I'm not sure why you insist on trying to use `CustomDistributionOfOptions`... (but to be clear, I don't know AnyLogic). – Mark Rotteveel Jun 17 '22 at 08:09

1 Answers1

0

Here is an example of initializing a Custom Distribution from one of my models:

CustomDistributionOfOptions<VaccineAttitude> customDistributionVaccineAttitude = 
new CustomDistributionOfOptions(
    VaccineAttitude.values(),
    new double[]{
        weight_vacc_acceptor,
        weight_vacc_hesitator,
        weight_vacc_rejector
    },
    this
);

What it is doing is declaring a new variable of type CustomDistributionOfOptions<VaccineAttitude>, where VaccineAttitude is the Option List that lists the options for the distribution. The name of the variable is customDistributionVaccineAttitude. As per the AnyLogic documentation, the constructor takes the values of the Option List, VaccineAttitude.values(), an array of weights of type double, and the agent that owns the variable. In this example, the code is located in Main, so this refers to Main.

You may query the distribution as follows:

VaccineAttitude result = customDistributionVaccineAttitude.get();

result will contain random value from the Option List VaccineAttitude drawn from the distribution according to the weights specified in the constructor.

Wade
  • 1