change rgb dynamically in netlogo - dynamic

So I'm writing a short programme in net logo where I want to color code my turtles based on a variable that they own view which varies between -1 and 1. I tried using color-scale in netlogo to define the colour, but it doesn't do quite what I want.
I wrote this to describe what I want, but netlogo seems to be getting confused when I pass the col variable to the set color command.
to colorise;;------------------------------------------------------------
; this changes the agent's colour based on their current [view] score.
; We could use the color-scale in netlogo, but it only works for one colour
; and it's easy to end up with a colour so dark we can't see it against black patches.
moderate ; resets any agents which have somehow ended up with a view score outside -1 to +1
ifelse view > 0
[ let col ( 1 - view )
set col col * 255
set color [ 255 col col ]
]
[ let col ( 1 + view )
set col col * 255
set color [ col col 255 ]
]
end
does anyone have any ideas?
Thanks!
Will

Assuming you have correctly limited the range of view, you will just run into a list creation problem: you cannot use the bracket notation with variables. Instead try
set color (list col col 255)
etc

Related

How do you update the formatting of a cell using xlsxwriter?

I am generating an excel file from Python using XlsxWriter.
I'm trying to "layer" formatting depending on the cell value. In pseudocode:
for rowId in rows:
for colId in cols:
value = table.loc[rowId, colId]
if value < 0:
# Make font red
for rowId in rows:
for colId in cols:
value = table.loc[rowId, colId]
if value > 1e6:
# Make bg orange
for rowId in rows:
for colId in cols:
value = table.loc[rowId, colId]
if value is False:
# Make bg purple
In practice I'm finding it hard to layer formatting without either creating a format object for each combination of attributes, or creating a format object per cell.
Any ideas of how this could be achieved?

Dynamic turtle creation in netlogo 2 [contd..]

In the interface tab i have a slider whose value ranges between 2 & 10. Depending on the value defined by the user using this slider, that many number of turtles should be created.
I tried using multiple if statements but there is a problem in the succeeding steps.
if (slider-value = 2) [create2]
if (slider-value = 3) [create3]
if (slider-value = 4) [create4]
if (slider-value = 5) [create5]
After creating the turtles using the above if conditions, i have to assign additional rules to each individual turtle like co-ordinate position, rules for how they should move etc. and i tried again using multiple if statements. But it doesn't seem to work.
for example the sub-query for create is as follows
to create2
create-challengers 2
ask turtle 0 [set color blue set label-color blue set size 2
set xcor party1-left-right ]
ask turtle 1 [set color red set label-color red set size 2
set xcor party2-left-right ]
ask turtles [ update-rule set old-mysize 0 set shape "default"]
end
for creating 3 turtles:
to create3
create-challengers 3
ask turtle 0 [set color blue set label-color blue set size 2
set xcor party1-left-right ]
ask turtle 1 [set color red set label-color red set size 2
set xcor party2-left-right ]
ask turtle 2 [set color green set label-color green set size 2
set xcor party3-left-right ]
ask turtles [ update-rule set old-mysize 0 set shape "default"]
end
so on and so forth.
The main problem is even though program works irrespective of how many turtles the user has defined, all the 10 gets created but only user defined number of turtles move, i.e. if the user has assined 3 then when i run the program 10 turtles are created but only 3 move.
Is there a way to get around like in other programming languages where one can simply use an if-else statement.
Can someone suggest a way, would really appreciate the help.
Thanks in advance!
After the turtles are created i assign certain rules for them to move:
to update-rule
if (slider-values = 2) [update2]
if (slider-values = 3) [update3]
if (slider-values = 4) [update4]
if (slider-values = 5) [update5]
end
And once again i create multiple sub-rule for update2, update3 which are basically for the each turtle depending on how many the user has defined:
If there are 2 turtles:
to update2
ask turtle 0 [set my-rule party1-rule]
ask turtle 1 [set my-rule party2-rule]
end
in case of 3 turtle:
to update3
ask turtle 0 [set my-rule party1-rule]
ask turtle 1 [set my-rule party2-rule]
ask turtle 2 [set my-rule party3-rule]
end
Below are the movement rules
to adapt
if (my-rule = "hunter") [hunt]
;;NB stickers do nothing
if (my-rule = "aggregator") [aggregate]
end
to hunt
ifelse (mysize > old-mysize) [jump 1] [set heading heading + 90 + random-float 180 jump 1]
set old-mysize mysize
end
to run-general
create2 create3 create4 create5 create-voters update-support
ask challengers [adapt] update-support
end
to go
run-general
tick
end
As noted by Seth, the problem is likely to be in the move part of the code since the turtles are being created. Just as general comments, it looks like your code for creating 3 turtles is identical to that for creating 2 turtles except for the additional turtle. If this is generally true, your code would be much easier if you used a format like:
to setup-challengers
create-challengers 1 [set color blue set label-color blue set size 2
set xcor party1-left-right ]
create-challengers 1 [set color red set label-color red set size 2
set xcor party2-left-right ]
if slider-value >= 3
[ create-challengers 1 [ set color green set label-color green set size 2
set xcor party3-left-right ] ]
if slider-value >= 4
[ create-challengers 1 [ <whatever you want number 4 to look like> ] ]
ask challengers [<do stuff>]
end
This way you only need the code for each turtle to be written once so if you change your mind about colours or something, it is much easier to change.
Okay, try editing your code to look like this:
to adapt
type "Hunters: " print count turtles with [my-rule = "hunter"]
type "Aggregators: " print count turtles with [my-rule = "aggregator"]
if (my-rule = "hunter") [hunt]
if (my-rule = "aggregator") [aggregate]
end
This is a standard debugging trick. It will print the number of hunters and aggregators to the Command Center (bottom of the interface tab). This allows you to work out where code is going wrong. For example, in your case, it will let you know if the problem is with the movement code or with the code that assigns rules to turtles.
Another trick is to have something like print "Got to hunt procedure" if you are not sure whether a procedure is even being reached.
this if-else thing for a slider based setup is very nasty looking.
an alternative is to have a set of (global) lists for all properties that are set seperately for each individual.
then you can use the following code
;assuming global_list1 and global_list2 exist
to setup
let index 0
create-turtles slidervalue [
set turtle_value1 item index global_list1
set turtle_value2 item index global_list2
set index index + 1
]
end
then you only need to make sure that each turtles will know what to do when asked to do something
Geerten

How to change color in a column in lazyhighcharts?

I am using LazyHighCharts in rails 3. My requirement is to show different color in a column.
when i use
series = {
:type=> 'bar',
:name=> [],
:data=> #rooms,
:color=> 'pink'
}
it displays whole column in pink color. Suppose i have 5 rows in a column and i want to show first row in pink color and the rest four row in green color. can anyone suggest me solution for this.
thnks
i don't know if you have resolved this issue, but this is how i resolved it.
#Controller before set data to the series option
data = []
your_array.each do |t|
data << {:y=>t.value, :color=>"#"+("%06x" % (rand * 0xffffff))}
end
then set data to series option
f.series({:name=>"Subcurso", :data=>data} )

How to make points one color when a third column equals zero, and another color otherwise, in Gnuplot?

I need to vary the point color for a row of values based on the color in one column. The data:
# x y z
1, 3, 0
1, 5, 6
3, 5, 2
4, 5, 0
The color should be one value if the column is zero and a different color if the value in the third column is non-zero.
So, I'm assuming:
plot "./file.dat" u 1:2:3 with points palette
as found here: https://stackoverflow.com/a/4115001 will not quite work.
In the above example data, that gnuplot command provides three different colors instead of the two I'm looking for.
This is probably close to what you want:
set palette model RGB defined ( 0 'red', 1 'green' )
plot[0:5][0:6] "file.dat" u 1:2:( $3 == 0 ? 0 : 1 ) with points palette
You could go one step further and remove the "noise":
unset key
unset colorbox
plot[0:5][0:6] "file.dat" u 1:2:( $3 == 0 ? 0 : 1 ) with points pt 7 ps 3 palette
if only the differentiation between zero and non-zero matters.
You can adjust the palette by
set palette defined (-0.1 "blue", 0 "red", 0.1 "blue")

Convert a Dynamic[] construct to a numerical list

I have been trying to put together something that allows me to extract points from a ListPlot in order to use them in further computations. My current approach is to select points with a Locator[]. This works fine for displaying points, but I cannot figure out how to extract numerical values from a construct with head Dynamic[]. Below is a self-contained example. By dragging the gray locator, you should be able to select points (indicated by the pink locator and stored in q, a list of two elements). This is the second line below the plot. Now I would like to pass q[[2]] to a function, or perhaps simply display it. However, Mathematica treats q as a single entity with head Dynamic, and thus taking the second part is impossible (hence the error message). Can anyone shed light on how to convert q into a regular list?
EuclideanDistanceMod[p1_List, p2_List, fac_: {1, 1}] /;
Length[p1] == Length[p2] :=
Plus ## (fac.MapThread[Abs[#1 - #2]^2 &, {p1, p2}]) // Sqrt;
test1 = {{1.`, 6.340196001221532`}, {1.`,
13.78779876355869`}, {1.045`, 6.2634018978377295`}, {1.045`,
13.754947081416544`}, {1.09`, 6.178367702583522`}, {1.09`,
13.72055251752498`}, {1.135`, 1.8183153704413153`}, {1.135`,
6.082497198000075`}, {1.135`, 13.684582525399742`}, {1.18`,
1.6809452373465104`}, {1.18`, 5.971583107298081`}, {1.18`,
13.646996905469383`}, {1.225`, 1.9480537697339537`}, {1.225`,
5.838386922625636`}, {1.225`, 13.607746407088161`}, {1.27`,
2.1183174369679234`}, {1.27`, 5.669799095595362`}, {1.27`,
13.566771130126131`}, {1.315`, 2.2572975468163463`}, {1.315`,
5.444014254828522`}, {1.315`, 13.523998701347882`}, {1.36`,
2.380307009155079`}, {1.36`, 5.153024664297602`}, {1.36`,
13.479342200528283`}, {1.405`, 2.4941312539733285`}, {1.405`,
4.861423833512566`}, {1.405`, 13.432697814928654`}, {1.45`,
2.6028066447609426`}, {1.45`, 4.619367407525507`}, {1.45`,
13.383942212133244`}};
DynamicModule[{p = {1.2, 10}, q = {1.3, 11}},
q := Dynamic#
First#test1[[
Ordering[{#, EuclideanDistanceMod[p, #, {1, .1}]} & /# test1,
1, #1[[2]] < #2[[2]] &]]];
Grid[{{Show[{ListPlot[test1, Frame -> True, ImageSize -> 300],
Graphics#Locator[Dynamic[p]],
Graphics#
Locator[q, Appearance -> {Small},
Background -> Pink]}]}, {Dynamic#p}, {q},{q[[2]]}}]]
There are several ways to extract values from a dynamic expression. What you probably want is Setting (documentation), which resolves all dynamic values into their values at the time Setting is evaluated.
In[75]:= Slider[Dynamic[x]] (* evaluate then move the slider *)
In[76]:= FullForm[Dynamic[x]]
Out[76]//FullForm= Dynamic[x]
In[77]:= FullForm[Setting[Dynamic[x]]]
Out[77]//FullForm= 0.384`
Here's a slightly more complicated example:
DynamicModule[{x},
{Dynamic[x], Slider[Dynamic[x]],
Button["Set y to the current value of x", y = Setting[Dynamic[x]]]}
]
If you evaluate the above expression, move the slider and then click the button, the current value of x as set by the slider is assigned to y. If you then move the slider again, the value of y doesn't change until you update it again by clicking the button.
Instead of assigning to a variable, you can of course paste values into the notebook, call a function, export a file, etc.
After a little more research, it appears that the answer revolves around the fact that Dynamic[] is a wrapper for updating and displaying the expression. Any computations that you want dynamically updated must be placed inside the wrapper: for instance, instead of doing something like q = Dynamic[p] + 1 one should use something like Dynamic[q = p + 1; q]}]. For my example, where I wanted to split q into two parts, here's the updated code:
DynamicModule[{p = {1.2, 10}, q = {1.3, 11}, qq, q1, q2},
q := Dynamic[
qq = First#
test1[[Ordering[{#, EuclideanDistanceMod[p, #, {1, .1}]} & /#
test1, 1, #1[[2]] < #2[[2]] &]]];
{q1, q2} = qq;
qq
];
Grid[{{Show[{ListPlot[test1, Frame -> True, ImageSize -> 300],
Graphics#Locator[Dynamic[p]],
Graphics#
Locator[q, Appearance -> {Small},
Background -> Pink]}]}, {Dynamic#p}, {Dynamic#q}, {Dynamic#
q1}}]]
If I am still missing something, or if there's a cleaner way to do this, I welcome any suggestions...