How to set agents location using for loop in Java - simulation

I am using this code for assigning dfferent node to different agents
int[] node = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
for (int i = 0; i < node.length; i++) {
if(agent.participant == i + 1){
agent.setLocation(node[i]);
}
}
The only problem is setLocation() function only accept point arguments not integer.
I also try to make the list of points but it does not work. Let me know how to solve this issue.

As per Emile:
Node[] nodes = {node0, node1, ...};
int counter=0;
for (Node currNode : nodes) {
if(agent.participant == counter + 1){
agent.setLocation(nodes [i]);
counter++;
}
}
You need to understand function arguments. setLocation(...) cannot work with an int, it needs something that is an actual location. Check the API, code-complete help, etc

Related

Restricting movement to a specific section

I am currently trying to working with restricting the agents to on 1/4 of the whole Manhattan map. The idea is simple and as follows;
Let us say we have 10 agents, their names being v1, v2 ... v10. I want them to move only in a specific section. Currently the method in whcih the MapBasedMovement has been made is such that it moves in all the place, but I need it to move in only specified coordinates.
Thanks,
I tried picking a specific set of coordinates i.e. (0, 0) and (700, 700). Tried to find all the MapNode that are between them and then ask the DTNNode to select only the MapNode in these coordinates. But this does not give me the correct results.
Adding code below
public Path getPath() {
Path p = new Path(generateSpeed());
MapNode curNode = lastMapNode;
MapNode prevNode = lastMapNode;
MapNode nextNode = null;
List<MapNode> neighbors;
Coord nextCoord;
assert lastMapNode != null: "Tried to get a path before placement";
// start paths from current node
p.addWaypoint(curNode.getLocation());
int pathLength = rng.nextInt(maxPathLength-minPathLength) +
minPathLength;
for (int i=0; i<pathLength; i++)
{
// neighbors = curNode.getNeighbors();
neighbors = host.possibleNodes;
Vector<MapNode> n2 = new Vector<MapNode>(neighbors);
if (!this.backAllowed)
{
n2.remove(prevNode); // to prevent going back
}
if (okMapNodeTypes != null) { //remove neighbor nodes that aren't ok
for (int j=0; j < n2.size(); ){
if (!n2.get(j).isType(okMapNodeTypes)) {
n2.remove(j);
}
else {
j++;
}
}
}
if (n2.size() == 0) { // only option is to go back
nextNode = prevNode;
}
else
{
// choose a random node from remaining neighbors
//nextNode = n2.get(rng.nextInt(n2.size()));
nextNode = host.possibleNodes.get(rng.nextInt(host.possibleNodes.size()));
System.out.println(lastMapNode);
List<MapNode> nodePath = pathFinder.getShortestPath(lastMapNode, nextNode);
for (MapNode node : nodePath)
{ // create a Path from the shortest path
p.addWaypoint(node.getLocation());
}
lastMapNode = nextNode;
prevNode = curNode;
nextCoord = nextNode.getLocation();
curNode = nextNode;
return p;
}
prevNode = curNode;
nextCoord = nextNode.getLocation();
curNode = nextNode;
p.addWaypoint(nextCoord);
}
lastMapNode = curNode;
return p;
}
Here, the parts where the host.possibleNodes have been mentioned are a method that I have made which basically tries to get the coordinates of each node and bins to a specific region. I can add that method as well if needed.

OpenSCAD: How to avoid extra grouping in CSG tree

I want to write a module that can optionally combine its children as either a union or a difference.
module u_or_d(option="d") {
if (option == "d") {
difference() children();
} else {
union() children();
}
}
module thing(option) {
u_or_d(option) {
cube(10, center=true);
cylinder(h=20, d=5, center=true);
}
}
translate([-15, 0, 0]) thing("d");
translate([ 15, 0, 0]) thing("u");
I was surprised that this doesn't work. Both instances of thing appear to create a union of the cube and the cylinder.
The CSG Tree Dump shows the problem. Here's the relevant excerpt:
difference() {
group() {
cube(size = [10, 10, 10], center = true);
cylinder($fn = 0, $fa = 12, $fs = 2, h = 20, r1 = 2.5, r2 = 2.5, center = true);
}
}
The children are wrapped in a group, so the difference() effectively has only one child, which happens to be an implicit union of the wrapped children.
Is there a way to invoke children() that avoids this unwanted grouping? If not, is there another way for a module to allow the invoker to select how the module combines its children?
I found a solution:
module u_or_d(option="d") {
if (option == "d" && $children > 1) {
difference() {
children([0]);
children([1:$children-1]);
}
} else {
union() children();
}
}
You still get a group wrapped around each invocation of children, but at least we can make two groups.

Learning Flutter

I am Ali from Senegal. I am 60 years old(maybe this is my real problem - smiley!!!).
I am learning Flutter and Dart. Today I wanted to use a list of given data model (it's name is Mortalite, please see in the code below).
I try to use dartpad. I am very sad because I do not understand why the following snipet does not run:
//https://www.dartpad.dev/
void main(){
print('Beginning');
List<Mortalite>pertes;
var causes = ['Bla0', 'Bla1', 'Bla2', 'Bla3'];
var lstDate = ['21/09/2020', '22/09/2020', '23/09/2020', '24/09/2020'];
var perteInst = [2, 4, 3, 1];
var total=0;
for (int i = 0; i < 4; i++) {
total += perteInst[i];
print(i); // prints only '0'
pertes[i] = new Mortalite(
causesPerte: causes[i],
datePerte: lstDate[i],
perteInstant:perteInst[i],
totalPertes: total);
};
print(pertes.length); // nothing printed
print('Why?'); // nothing printed
}
class Mortalite {
String datePerte;
String causesPerte;
int perteInstant; // pertes du jour
int totalPertes; // Total des pertes.
Mortalite(
{this.datePerte,
this.causesPerte,
this.perteInstant,
this.totalPertes}
);
}
Thank you well for help.
A.KOTE
The reason above code doesn't work is because you have List pertes initialized and you are passing to the elements of pertes. When you try to pass to 0 index of pertes, it cannot find it and it throws range error because pertes doesn't have an index 0 yet. Please take a look at fix below and let me know if you need help with anything.
void main(){
print('Beginning');
List<Mortalite>pertes=[];
var causes = ['Bla0', 'Bla1', 'Bla2', 'Bla3'];
var lstDate = ['21/09/2020', '22/09/2020', '23/09/2020', '24/09/2020'];
var perteInst = [2, 4, 3, 1];
var total=0;
for (int i = 0; i < causes.length; i++) {
total += perteInst[i]; // prints only '0'
pertes.add (new Mortalite(
causesPerte: causes[i],
datePerte: lstDate[i],
perteInstant:perteInst[i],
totalPertes: total));
};
print(pertes.length); // nothing printed // nothing printed
}
class Mortalite {
String datePerte;
String causesPerte;
int perteInstant; // pertes du jour
int totalPertes; // Total des pertes.
Mortalite(
{this.datePerte,
this.causesPerte,
this.perteInstant,
this.totalPertes}
);
}

How to fix : The object of type 'GameObject' has been destroyed but you are still trying to access it ? -Unity

I made a 5 second time bar to replace the wave. when wave1 has been 5 seconds it will move to wave2. then the first wave will be destroyed. when I got to wave3, an error came out. here's my code:
IEnumerator ChangeWave()
{
for (int i = 0; i < wave.Length - 1; i++)
{
yield return new WaitForSeconds(5f);
Destroy(wave[i]);
wave[i+1].SetActive(true);
}
}
the error said The object of type 'GameObject' has been destroyed but you are still trying to access it. - unity
sorry for my bad english.
There's a few things going on here.
Remove the -1 in the for loop, so it iterates to end
wave[i+1] will cause an error, so check if (i < wave.Length -1)
Destroy( array[index] ) will try and access Game Object but it's destroyed, so you should create a temporary var to hold gameobject, and set null to the array element.
Some fixes below - there might be a better way, but this is what I use:
Version 1 - Using the above fixes
IEnumerator ChangeWaveV2()
{
for (int i = 0; i < wave.Length; i++)
{
yield return new WaitForSeconds(.2f);
var t = wave[i];
wave[i] = null;
Destroy(t);
if(i < wave.Length - 1)
wave[i + 1].SetActive(true);
}
}
Version 2 - A variation without needing to null out the element and create a temporary var. If you iterate from end to front of the array, you can freely Destroy() GameObjects in an array. May not be useful as it changes the wave array.
IEnumerator ChangeWaveV3()
{
System.Array.Reverse(wave);
for (int i = wave.Length - 1; i >= 0; i--)
{
yield return new WaitForSeconds(.2f);
Destroy(wave[i]);
if(i > 0)
wave[i - 1].SetActive(true);
}
}

Linked List Parameterized constructor

I tried to do a Parameterized constructor for a linked list my program is about to implement a queue by using a liked list so i want to do a parameterized constructor like Queue(int value , int size) and it dose not run or doing a list
this is my code for this problem
Queue(int value,int _size)
{
for(int i = 0; i < _size; ++i)
{
Node* temp = new Node;
temp->data = value;
temp->next = nullptr;
if(head == nullptr)
{
head = tail = temp;
}
else
{
tail->next = temp;
tail = temp;
}
}
}
i expected that the result is to fill the lest by value times size like if i run this function Queue x(20,3) the linked list should be
20 20 20
Since that this is a constructor, The head and tail are not properly initialized to use them. I would suggest adding head = tail = nullptr just before the loop and see what happens.
Follow this code after your node creation. I hope this will work. And do use i++ instead of ++i, as the later will make the loop for size-1 times.
if(head == NULL)
head = temp;
else{
Node *x;
x= head;
while(x->next != NULL)
x = x->next;
x->next = temp;
}