How to refresh a patch variable after tick? - loops

I have a patch variable X which i want to be computed after each tick. I basically have a condition where at each tick i want to highlight only those patches whose X value is greater than the limit that i have placed.
This is what i have coded:
ask patches with [votes-with-benefit > 0] [ ifelse (b-c <= threshold)
[ set votes-with-benefit 0 set pcolor red ]
[ set votes-with-benefit votes
set pcolor scale-color white vote-share 0 max-voteshare ]
]
The problem is after the first tick even though there are patches who value is greater than the threshold they still appear red instead of reverting back to white.
Thanks in adavance. Appreciate the help.
Regards

It sounds like you need to switch the filter and the condition:
ask patches with [b-c <= threshold] [ ifelse (votes-with-benefit > 0)
[ set votes-with-benefit 0 set pcolor red ]
[ set votes-with-benefit votes
set pcolor scale-color white vote-share 0 max-voteshare ]
]
In any case, as you have it now, you will never reset those whose vote-with-benefits you set to zero, because you filter them out.

i figured it out. Sorry to have spammed. The patch variable was not updating after each tick because of two counter arguments i coded one after the other using two different 'ask patches command'. Once i combined them into one statement it started working. Thanks

Related

iMacros - Detect {{LOOP}} value number and scrol down the page

I found a solution for what I need, but maybe someone can help me optimize it.
When the {{LOOP}} value have 20 to 40,60,80,100,120,140,160, ... 2000... 10000... etc. or more, I need to Scroll Down the page with the value I need.
Because the WebPage is big and I should reach maybe 20000 as the value of {{LOOP}}.
In this case, my solution seems a little out of hand. I would have to repeat the code in many lines.
SET !LOOP 20
SET !EXTRACT {{!LOOP}}
SET scrolla EVAL("var x='{{!EXTRACT}}'; var z; if(x=='20'){z=\"javascript:window.scrollBy(0,5000)\";} else{z=\"javascript:window.scrollBy(0,500)\";}; z;")
SET scrolla EVAL("var x='{{!EXTRACT}}'; var z; if(x=='40'){z=\"javascript:window.scrollBy(0,5000)\";} else{z=\"javascript:window.scrollBy(0,500)\";}; z;")
SET scrolla EVAL("var x='{{!EXTRACT}}'; var z; if(x=='60'){z=\"javascript:window.scrollBy(0,5000)\";} else{z=\"javascript:window.scrollBy(0,500)\";}; z;")
'URL GOTO={{scrolla}}
PROMPT {{scrolla}}
Someone have another solution for my problem? More optimized? and not using so many lines?
thank you so much
I am using (FCI):
iMacros for CR v10.1.1 'PE', CR v105.0.5195.102 (_x64), Win10_x64.
('CR' = 'Chrome' / 'PE' = 'Personal Edition')
Later edit:
Another idea that crossed my mind would be to use a .csv and put 20, 40, 50 ... 1000 on the specific line to match the {{LOOP}}, and the other lines 0,0,0,0,. and in EVAL function I will use it (x=='{{!COL1}}') instead of (x=='20'), (x=='40')..

NetLogo: 2048 bot optimisation

I am trying to make a Netlogo simulation of a 2048 game. I have implemented three heuristic functions determined by weight parameters and want to use behaviour space to run simulations and check what is the best strategy for winning this game.
Procedure search uses export/import-world primitives to search over possible moves and chooses the move for which the heuristic function has the highest value.
The problem is that this procedure is very slow (due to the import-world function which is being called four times each turn). Do you have any ideas how to implement this without exporting and importing world so often?
This is a project for my Introduction to AI class. It is due in a couple of days and I can't seem to find any solutions.
The relevant part of the code is below. Procedures move-(direction) all work properly and variable moveable? is true if the square can move in said direction and false otherwise. It is checked in procedure moveable-check called by move-(direction).
I would very much appreciate your help. :)
to search
let x 0
let direction "down"
export-world "state.csv"
move-up
ifelse not any? squares with [moveable?]
[set h-value -5000]
[set x h-value
set direction "up"
import-world "state.csv"]
export-world "state.csv"
move-down
ifelse not any? squares with [moveable?]
[set h-value -5000]
[if h-value > x
[set x h-value
set direction "down"]
import-world "state.csv"]
export-world "state.csv"
move-left
ifelse not any? squares with [moveable?]
[set h-value -5000]
[if h-value > x
[set x h-value
set direction "left"]
import-world "state.csv"]
export-world "state.csv"
move-right
ifelse not any? squares with [moveable?]
[set h-value -5000]
[if h-value > x
[set x h-value
set direction "right"]
import-world "state.csv"]
ifelse direction = "up"
[move-up
print "up"]
[ifelse direction = "down"
[move-down
print "down"]
[ifelse direction = "right"
[move-right
print "right"]
[move-left
print "left"]]]
if not any? squares with [moveable?]
[
ask squares [set heading heading + 90]
moveable-check
if not any? squares with [moveable?]
[ask squares [set heading heading + 90]
moveable-check
if not any? squares with [moveable?]
[ask squares [set heading heading + 90]
moveable-check
if not any? squares with [moveable?]
[stop]]]
]
end
The most important, and difficult, information you need to be able to save and restore is the squares. This is pretty easy to do without import-world and export-world (note that the following uses NetLogo 6 syntax; if you're still on NetLogo 5, you'll need to use the old task syntax in the foreach):
to-report serialize-state
report [(list xcor ycor value)] of squares
end
to restore-state [ state ]
clear-squares
foreach state [ [sq] ->
create-squares 1 [
setxy (item 0 sq) (item 1 sq)
set heading 0 ;; or whatever
set value item 2 sq
]
]
end
value above just shows how to store arbitrary variables of your squares. I'm not sure what data you have associated with them or need to restore. The idea behind this code is that you're storing the information about the squares in a list of lists, where each inner list contains the data for one square. The way you use this then is:
let state serialize-state
;; make changes to state that you want to investigate
restore-state state
You may need to store some globals and such as well. Those can be stored in local variables or in the state list (which is more general, but more difficult to implement).
A few other ideas:
Right now it looks like you're only looking one state ahead, and at only one possible position for the new square that's going to be placed (make sure you're not cheating by know where exactly the new square is going to be). Eventually, you may want to do arbitrary look ahead using a kind of tree search. This tree gets really big really fast. If you do that, you'll want to use pruning strategies such as: https://en.wikipedia.org/wiki/Alpha%E2%80%93beta_pruning . Also, that makes the state restoration stuff more difficult, but still doable. You'll be storing a stack of states rather than a single state.
Instead of set heading heading + 90 you can just do right 90 or rt 90.

define geofence around location stored in array

I am working on an app where I have a set of pre-defined co-ordinates stored in an array like the one below:
var SpeedcameraLocationDictionary: [[String : AnyObject]] = [
["camindex": "1a", "Latitude":xxxx, "Longitude":xxxx,"Distance":34, "legalspeed":110],
["camindex": "1b", "Latitude":xxxx, "Longitude":xxxx,"Distance":34, "legalspeed":110],
["camindex": "2a", "Latitude":xxxx, "Longitude":xxxx,"Distance":26, "legalspeed":110],
["camindex": "2b", "Latitude":xxxx, "Longitude":xxxx,"Distance":26, "legalspeed":110]]
What I want to do is, when the users location matches any lat & long in the array (+ or - 5 meters) , start doing some calculations. I have variables set up to hold the lat & long of the user, and the relevant CLLocation manager. Implanting on checking the users location every 30 secs, so i have a timer set up to trigger at 30sec intervals.
I believe I need to set up a geofence around the relevant point, but I'm not really sure how to do the check & set up.
I imagine it would be along the lines of the following:
if Timer = 30secs
let region = CLCircularRegion(center: Latitudefromarray,longitudefromarray, radius: 5,)
if (Latitude +/- 5m == region AND longitude +/- 5m == region)
do calculations....
could somebody either point me in the direction of a tutorial that could help me achieve this goal, or provide some sample code to achieve the goal I'm after? i have been following this tutorial but am struggling to adapt it to my needs.
Thanks

MultiTimeFrame Indicator BackTesting mql4

before posting any code I would like to understand if it is possible to backtest a Custom Indicator, for a MetaTrader4 Terminal, based on a multi time frame strategy.
I have looked on mql5 forum, but I could not find any clear indications or approach to the issue.
...before posting any answer I would like to understand, what do you consider a multi-time frame strategy in custom indicator context.
How to make the step forward?
Select your own way - The Approach
In any case, one can use function calls aimed to retrieve values, collected from a perspective of a different time-frame, with a use of proper indication of { PERIOD_M1 | .. | PERIOD_H1 | PERIOD_H4 | .. } in the function call protocol,
or
one can create and maintain one's own virtual super-framing / sub-framing independently of the current graph's "own" time-frame.
double v30SEC_O[], v30SEC_H[], v30SEC_L[], v30SEC_C[],
vM1_O[ ], vM1_H[ ], vM1_L[ ], vM1_C[ ],
vM3_O[ ], vM3_H[ ], vM3_L[ ], vM3_C[ ],
vH7_O[ ], vH7_H[ ], vH7_L[ ], vH7_C[ ]; // vTF as needed
bool v30SEC_newBarEVENT = False,
vM1_newBarEVENT = False,
vM3_newBarEVENT = False,
vH7_newBarEVENT = False;
void aNewBarEventMONITOR(){ ...
static int v30SEC_Bars = EMPTY,
vM1_Bars = EMPTY,
vM3_Bars = EMPTY,
vH7_Bars = EMPTY;
// check aNewBarEVENT:
// update state-vars:
}
Does it work in spite of many posts on failed MTF [StrategyTester] results?
In each of cases posted above, one may use the other one to check and proof the correctness of the outputs.
Yes, unit-tests are a good safety belts habit in this domain.
The recent "new"-MQL4.56789+ shifts and frequent interim compiler ( syntax ) live-updates ( you get a new Help to notice them ) make unit-testing a must-do part of the pre-release testing + production code maintenance.

How to make turtles face each other, wait 3 ticks and then keep wandering?

I am new to both, Netlogo and stackoverflow, but your other posts have already helped me a lot.
I am currently trying to program a model, where agents randomly wander a space and have them stop whenever they meet. "Meeting" here means "passing each other in-radius 2". They should face each other, wait for 2 ticks and then keep moving until they find the next agent.
I tried to use NzHelen's question on a timer, but did not really succeed.
So far, I managed to have them face each other. I have trouble putting the tick-command at the right place in my code. (EDIT: This got solved by taking out the wait-command, thanks to Seth. --> And I don't want all turtles to stop moving, but only the ones which are meeting each other).
One other thing which I am striving for is some kind of visual representation of them meeting, for instance have the patch blink for the time when they are meeting or a circle which shows up around them when they meet. With the wait-command, everything stops again, which I want to prevent.
Below the code so far.
to go
tick
ask turtles
[
wander
find-neighbourhood
]
ask turtles with [found-neighbour = "yes"]
[
face-each-other
]
ask turtles with [found-neighbour = "no" or found-neighbour = "unknown"]
[ wander ]
end
;-------
;Go commands
to wander
right random 50
left random 50
forward 1
end
to find-neighbourhood
set neighbourhood other turtles in-radius 2
if neighbourhood != nobody [wander]
find-nearest-neighbour
end
to find-nearest-neighbour
set nearest-neighbour one-of neighbourhood with-min [distance myself]
ifelse nearest-neighbour != nobody [set found-neighbour "yes"][set found-neighbour "no"]
end
to face-each-other ;;neighbour-procedure
face nearest-neighbour
set found-neighbour "no"
ask patch-here [ ;; patch-procedure
set pcolor red + 2
;wait 0.2
set pcolor grey + 2
]
if nearest-neighbour != nobody [wander]
rt 180
jump 2
ask nearest-neighbour
[
face myself
rt 180
jump 2
set found-neighbour "no"
]
end
With the help of a colleague I managed to solve my timer-issue. As Seth pointed out wait was not the right command and too many to-end-loops confused my turtles as well. The code now looks like the following and works. The turtles get close to each other, face each other, change their shape to stars, wait three ticks and then jump in the opposite directions.
to setup
clear-all
ask turtles
[
set count-down 3
]
reset-ticks
end
;---------
to go
ask turtles
[
if occupied = "yes" [
ifelse count-down > 0
[
set count-down (count-down - 1)
set shape "star"
][
set shape "default"
rt 180
fd 2
set occupied "no"
set count-down 3
]
]
if occupied = "no" [
; Wandering around, ignoring occupied agents
set neighbourhood other turtles in-radius 2
; If someone 'free' is near, communicate!
set nearest-neighbour one-of neighbourhood with-min [distance myself]
ifelse nearest-neighbour != nobody [
if ([occupied] of nearest-neighbour = "no") [
face nearest-neighbour
set occupied "yes"
ask nearest-neighbour [
face myself
set occupied "yes"
]]
][
; No one found, keep on wandering
wander
]]]
tick
end
;-------
;Go commands
to wander
right random 50
left random 50
forward 1
end
You're right to link to Nzhelen's question. Essentially the answer to your question is that you need to do the same thing. When you tried to do that, you were on the right track. I'd suggest taking another stab at it, and if you get stuck, show us exactly where you got stuck.

Resources