How to use the "CustomDistributionOfOptions" constructor in Anylogic - java

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.

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.

Related

How to convert ArrayList to ExampleSet in Rapidminer?

I'm creating an extension for rapidminer using java. I have an array of elements of type Example and I need to covert it to a dataset of type ExampleSet.
Rapidminer's ExampleSet definition looks like this:
public interface ExampleSet extends ResultObject, Cloneable, Iterable<Example>
I need to pick certain elements from dataset and send it back, still as ExampleSet, however casting is not working and I can't simply create new ExampleSet object since it's an interface.
private ExampleSet generateSet(ExampleSet dataset){
List<Example> list = new ArrayList<Example>();
// pick elements from sent dataset and add them to newly created list above
return (ExampleSet)list;
}
You will need more than a simple explicit cast.
In RapidMiner, an ExampleSet is not just a collection of Example. It contains more complex information and logic.
Therefore, you need another approach to work with ExampleSets. Like you already said, it is just the interface, which lead us to choice of the right subtype.
For starters, (Since: 7.3) simply use one of ExampleSets class's methods .
You also need to define each Attribute this ExampleSet is going to have, namely the columns.
Below, I create one with a single Attribute called First
Attribute attributeFirst = AttributeFactory.createAttribute("First", Ontology.POLYNOMINAL);
ExampleSetBuilder builder = ExampleSets.from(attributeFirst);
builder.addDataRow(example.getDataRow());
ExampleSet result = builder.build();
You can also get the Attributes in a more generic way using:
Attribute[] attributes = example.getAttributes().createRegularAttributeArray();
ExampleSetBuilder builder = ExampleSets.from(attributes);
...
If you have many cases where you have to create or alter ExampleSet, I encourage you to write your own ExampleSetBuilder since the original implementation have many drawbacks.
You can also try searching for other extensions, which may already meet your requirements, and you do not need to create one of your own (belive me, it's not Headache-free).
the ExampleSet class is getting deprecated (but still perfectly fine to use).
You might want to consider switching over to the newer data set API called Belt (https://github.com/rapidminer/belt). It's faster and more intuitive to use. It's still actively developed, so feedback is also welcome.
Also if you have more specific questions, feel free to drop by the RapidMiner community (https://community.rapidminer.com/), where also many of the developers are very active.

Can not understand the sequence of method and constructor

According to sequence diagram I should create firstly method "regisreItem(Item item)" with argument "item" as an object. I see my problem that the constructor for "items" is called after the method "regisreItem(Item item)" so that I have nothing to insert into "regisreItem(Item item)" method according sequence diagramm. Or not ?
Sequence diagram
Class diagram
Here is a part of sequence diagram i am interested in
https://drive.google.com/open?id=1eJolWNoN32IubP3iaaXPc_cLM5Es08hK
Here is all my sort of code.
Please provide me some sort of code ho is it possible to implement.
And clarify the beginning of sequence diagram.
Since the operation registerItem expects an item as parameter, the Auctioneer object needs to create it, before calling the operation. That means the Auctioneer has to send a create message, not the Auction (using new Item() as a parameter is not possible in a sequence diagram - and it doesn’t change the creator anyway). i1 and i2 are attributes of the interaction. They can be used as parameters of registerItem.
addBid also expects a bidder. Again the attributes Max and Moritzof the interaction should be used here.
In a real program these interaction attributes would be temporary variables of the Auction::addBid operation or of the Auctioneer. The Auctioneer is probably not supposed to have variables, therefore the registerItem Operation should probably only have generic data types such as stringas parameters.
The Auction is supposed to send messages to i1 and i2, however, since these are attributes of the interaction, the Auction object does not know them. It’s Ok to omit this detail, but it would be better to show how the Auction finds the relevant Item, for example with a findItemByName Operation called on itself.
A better alternative is, to let the Auction send the messages to its own attribute allItems. Then two lifelines would represent the same attribute, but with different objects. A selector could be used to distinguish between the two objects in the slot defined by this attribute (allItems[0], allItems[1], this is optional). The same is applicable to allBids instead of b300EUR and the like.
You can get around the issue of the Item constructor being called after registerItem by using:
registerItem(new Item(...));
and passing in the attributes of Item i1 and i2. That will create the new Item and then it can be added to the auction Item list.
I'm assuming the start of the sequence diagram is the auctioneer creating or opening an already created auction and then adding a list of items that will be used in the auction by repeatedly calling registerItem(new Item(...)); which can then have bids added to them by Max and Moritz via the Auction object

Get variable name of object from an ArrayList

I am creating an ArrayList of JTextFields using the following code.
ArrayList<JTextField> cmp = new ArrayList<>();
cmp.add(txtAmount);
cmp.add(txtBillTo);
cmp.add(txtBranch);
After passing this ArrayList into a method, I need to print the "variable name" of the textfield. I can use the SetName and GetName to print some names. But I need the output as txtAmount, txtBillTo, txtBranch.
Is there anyway to find the variable name of textfield?
No, you can't do this. The information you are asking for is just not recorded. The only way to do it would be to store it yourself:
Map<String, JTextField> cmp = new HashMap<>();
cmp.put("txtAmount",txtAmount);
cmp.put("txtBillTo", txtBillTo);
cmp.put("txtBranch", txtBranch);
If you need to keep things in the same order you put them into the collection then use a LinkedHashMap instead of a HashMap as the HashMap may change the order.
What you are looking for would require reflection of method code, something supported by languages such as Scala via macros/compile time reflection. However Java itself does not support runtime or compile time reflection of method code, only runtime reflection of classes/fields/functions, although some people have pulled together their own Java parsers to grab the information from source files and others have pulled the information from the Class files debug information. Neither of which has an officially supported API.
Your best bet is to place the name that you want along with the field, which is obviously duplication and thus could diverge by accident but your options are limited.

Updating values of functional data properties doesn't remove old values, only adds new triples

I have an RDF Ontology with a functional property hasTrendValue which relates instances of a class with integer values. I want to change these values programmatically using Jena. I tried the following code:
Property hasTrend = ontModel.getDatatypeProperty(preFix+"hasTrendValue");
Individual regionQualifier = ontModel.getIndividual(activityName);
ontModel.addLiteral(regionQualifier,hasTrend,34);
PrintStream p = new PrintStream(ontoPath);
ontModel.write(p,null);
p.close();
This code executes correctly but, it does not update the already hasTrendValue value in the RDF; instead it adds a new hasTrendValue to the RDF ontology even though it declared as a functional property. What is the better way of doing this?
RDF does not have the concept of "change", only "add" and "remove". To change a value, you need to remove the old one and add the new one.
Declaring it as a functional property does not change this. Jena does not check the ontology on every operation. In fact, a functional property says that the object identifies one thing - it may be written in many ways. 001 and 1 are the same value. There may be multiple triples, it's not automatically wrong.

2D Array that can hold multiple values with no limits

I am quite new to java currently working on a not-so-simple web browser application in which I would like to record a permanent history file with a 2D array setup with 3 columns containing "Date Viewed", "URL", "How many times this URL has been viewed before".
Currently I have a temporary solution that only saves "URL" which is also used for "Back, Foward" features using an ArrayList.
private List tempHistory = new ArrayList();
I am reading through the Java documentation but I cannot put together a solution, unless I am missing the obvious there is no 2D array as flexible a ArrayList like in Python?
From your description it doesn't sound like you need a 2D array. You just have one dimension -- but of complex data types, right?
So define a HistoryItem class or something with a Date property for date viewed, URL for URL, int for view count.
Then you just want a List<HistoryItem> history = new ArrayList<HistoryItem>().
The reason I don't think you really want a 2D array-like thing is that it could only hold one data type, and you clearly have several data types at work here, like a date and a count. But if you really want a table-like abstraction, try Guava's Table.
No, there is no built-in 2D array type in Java (unless you use primitive arrays).
You could just use a list of lists (List<List>) - however, I think it is almost always better to use a custom type that you put into the list. In your case, you'd create a class HistoryEntry (with fields for "Date viewed", URL etc.), and use List<HistoryEntry>. That way, you get all the benefits a proper type gives you (typechecking, completion in an IDE, ability to put methods into the class etc.).
How do you plan to browse the history then? If you want to search the history for each url later on then ArrayList approach might not be efficient.
I would rather prefer a Map with URL as key.
Map<Url,UrlHistory> browseHistory = new HahMap<Url,UrlHistory> ();
UrlHistory will contains all the fields you want to associate with a url like no. of times page was accessed and all.

Categories

Resources