NetLogo加权有向图观念扩散模型:动态效用函数设计与p值更新实现求助
Let's break down how to implement the dynamic mechanism you want—where agents' p values update based on incoming influences from red vs. gray agents—while refining your model's core logic.
Key Changes to Implement
1. Adjust Initial p Values to Continuous Range
Your original assign-p sets p to either 0 or 1, which is too rigid for dynamic updates. Let's switch to a continuous 0-1 range (since p represents a probability):
to assign-p ;assign probability to agents (0 to 1, continuous) ask group [set p random-float 1] end
2. Add a Dynamic p Update Procedure
Create a new procedure that calculates each agent's net influence from incoming links, then adjusts their p value. We'll clamp p between 0 and 1 to keep it valid as a probability:
to update-p ask group [ ; Calculate total weight from red sending agents (incoming links where end1 is red) let red-influence sum [weight] of influences with [end1 = myself and end1.color = red] ; Calculate total weight from gray sending agents let gray-influence sum [weight] of influences with [end1 = myself and end1.color = gray] ; Update p: add red influence, subtract gray influence set p p + red-influence - gray-influence ; Clamp p to 0-1 to prevent invalid values set p max (list 0 (min (list 1 p))) ] end
Note: We use myself here to target incoming links where the current agent is the recipient (end2 of the link). This aligns with your goal of agents receiving influence from others via directed links.
3. Refine the behavior Procedure
Your original behavior loop directly sets color based on static p values. Now we'll first update p dynamically, then set color based on the updated p (we use 0.5 as your original trigger, but you can adjust this threshold):
to behavior ; First, update all agents' p values based on incoming influences update-p ; Update agent colors based on their new p values ask group [ if p > 0.5 [ set color red ; Optional: Mark outgoing links from red agents to visualize influence flow ask my-out-links [set color red] ] [ set color gray ask my-out-links [set color gray] ] ] tick end
This ensures color reflects the agent's current state after receiving influence, rather than a static initial p value.
4. Optimize the Utility Function
Your original static utility function can be modified to run dynamically each tick, reflecting the current state of the network. Here's an updated version that calculates weighted diffusion effectiveness in real-time:
to plot-utility let total-red-p sum [p] of group with [color = red] let total-p sum [p] of group let total-red-weight sum [weight] of influences with [color = red and weight > 0.5] let total-weight sum [weight] of influences with [weight > 0.5] ; Avoid division by zero if there are no valid links or agents let utility ifelse-value (total-p = 0 or total-weight = 0) [ 0 ] [ (total-red-p * total-red-weight) / (total-p * total-weight) ] plot utility end
You can call this procedure in your behavior loop (after update-p) to plot the dynamic utility over time.
Full Modified Code
Here's the complete code with all changes integrated:
extensions [nw] ;network extension breed [group g] ;turtles are called group directed-link-breed [influences inf] ;directed link are called influences links-own [weight] ;weight of directed link, related to links only group-own [p] ;probability to take or not red idea, related to turtles only ; SETUP to setup-pa ;generate preferential-attachment network ca nw:generate-preferential-attachment group influences members 1 [ set color grey setxy random-xcor random-ycor set shape "person" set size 2] assign-weight assign-p reset-ticks end to setup-small-world ;generate small-world network ca nw:generate-watts-strogatz group influences members 1 1 [ set color grey setxy random-xcor random-ycor set shape "person" set size 2] assign-weight assign-p reset-ticks end to setup-random ;generate random network ca nw:generate-random group influences members 1 [ set color grey setxy random-xcor random-ycor set shape "person" set size 2] assign-weight assign-p reset-ticks end to assign-weight ;assign random-float weight to links in a range from 0 to 1 ask influences [set weight random-float 1 ] end to assign-p ;assign probability to agents (0 to 1, continuous) ask group [set p random-float 1] end ; EXPERIMENT to update-p ask group [ let red-influence sum [weight] of influences with [end1 = myself and end1.color = red] let gray-influence sum [weight] of influences with [end1 = myself and end1.color = gray] set p p + red-influence - gray-influence set p max (list 0 (min (list 1 p))) ] end to behavior update-p ask group [ if p > 0.5 [ set color red ask my-out-links [set color red] ] [ set color gray ask my-out-links [set color gray] ] ] ; Uncomment the line below to plot the dynamic utility each tick ; plot-utility tick end to plot-utility let total-red-p sum [p] of group with [color = red] let total-p sum [p] of group let total-red-weight sum [weight] of influences with [color = red and weight > 0.5] let total-weight sum [weight] of influences with [weight > 0.5] let utility ifelse-value (total-p = 0 or total-weight = 0) [ 0 ] [ (total-red-p * total-red-weight) / (total-p * total-weight) ] plot utility end
Additional Notes
- Link Color Visualization: The code marks outgoing links with the agent's color to help you visualize how influence spreads through the directed network. You can remove this if it's not needed.
- Threshold Adjustment: If you want a different threshold for agents turning red, just change the
p > 0.5condition in thebehaviorprocedure. - Rate of Change: If you want slower or faster updates to
p, multiply the influence difference by a scaling factor (e.g.,set p p + 0.3 * (red-influence - gray-influence)).
内容的提问来源于stack exchange,提问作者Alice

