Netlogo:通过调整其他行/列来替换矩阵元素

时间:2018-06-18 20:48:01

标签: if-statement matrix netlogo

我在Netlogo上开发了一系列制作大量使用矩阵的模型。简而言之,模型包括不同品种的许多状态变量,其中状态变量通常是类似股票的项目。举个简单的例子,考虑一下模型:

extensions [ matrix ]

globals
[
]


turtles-own 
[
  n-items
  stock-list
]

to setup
  clear-all
  reset-ticks
  create-turtles 2
  ask turtles
  [
    setxy random-xcor random-ycor
    set n-items 10
    let n-vars 3
    set stock-list matrix:make-constant n-items n-vars [0] ; empty matrix
    let stock-item n-values n-items [i -> i]
    let stock-cost n-values n-items [ random-normal 10 2 ]
    let stock-age n-values n-items [ random 50 ]
    matrix:set-column stock-list 0 stock-item
    matrix:set-column stock-list 1 stock-cost
    matrix:set-column stock-list 2 stock-age
  ]
 end

此处,每个海龟的矩阵stock-list初始化为空矩阵,然后根据变量stock-item(库存ID),stock-cost填充其列。 stock-age

想象一个go程序,每个步骤将股票年龄增加一个:

to go
  ask turtles
   [
     let current-age matrix:get-column stock-list 1
     let new-age map [x -> x + 1] current-age
     matrix:set-column stock-list 2 new-age
   ]
  tick
end

我想要做的是stock-cost上的操作,只有当年龄大于某个值时,例如10

;; pseudo-code
for( i = 1 to I = number of items )
 {
   if ( stock-age[i] > 10 ) 
    {
      stock-cost[i] - 1
    }
 }

我知道如何使用stock-cost原语更改map条件,使其符合自己的值,例如:

to decrease-stock-value
  ask turtles
  [
    let current-cost matrix:get-column stock-list 1
    set current-cost map [[?] -> ifelse-value (? > 10) [? - 1][?]] current-cost
    matrix:set-column stock-list 1 current-cost
  ]

但是,我将此概括为使用不同列表中的值进行调整的努力失败了。

感谢您的帮助!此外,任何洞察这是否是一个很好的方法来建模状态变量,如股票将是有用的。

1 个答案:

答案 0 :(得分:1)

我想我使用以下方法进行了整理:

to decrease-stock-value
  ask turtles
   [
     let current-cost matrix:get-column stock-list 1
     let current-age matrix:get-column stock-list 2
     let new-cost ( map [ [ a b ] -> 
        ifelse-value ( a > 10 ) [ b - 1 ] [ b ] ] 
        current-age current-cost
        )
     matrix:set-column stock-list 1 new-cost
   ]
end