netlogo错误无法在长度为x

时间:2017-02-14 02:31:05

标签: netlogo

以下程序旨在从众多海龟中选择一个“赞助人”并将其颜色更改为白色。选择是基于海龟财富的概率。

运行此代码,特别是行item i ranking-in-radius我收到类似这样的错误消息:“cannot find element 43 in list...(shows list of turtles' wealth)... which is only of length 43.”我明白我收到此错误,因为列表从0开始,因此列表为43元素从0到42,但我不明白为什么我写的代码不正确。我的i0开始,如果i不是<,则程序将停止,而不是查询的海龟数量(即应停在42)。谁有人建议解决方案?谢谢!

to choose-patron
  let i 0
  let patrons-in-radius count turtles in-radius radius with [wealth >= 80]
  let ranking-in-radius sort-by > [wealth] of turtles in-radius radius with  [wealth >= 80]
  let total-wealth-in-radius sum [wealth] of turtles in-radius radius with [wealth >= 80]
  while [i < (patrons-in-radius)][
    ask turtles in-radius radius with [wealth >= 80] [
      if [wealth] of self = item i ranking-in-radius [   
        if random 100 < ((wealth / total-wealth-in-radius) * 100) [
          set color white
          if any? turtles in-radius radius with [color = white] [stop]
      ]
      set i (i + 1)
    ]
  ]
]
end

1 个答案:

答案 0 :(得分:0)

看起来你的set i (i + 1)处于错误的区域。现在i每次if [wealth]...运行时都会增加i,但您应该while [i < (patrons-in-radius)]阻止while阻止 while [ i < (patrons-in-radius) ] [ ask turtles in-radius radius with [ wealth >= 80 ] [ if [ wealth ] of self = item i ranking-in-radius [ if random 100 < ( (wealth / total-wealth-in-radius) * 100) [ set color white if any? turtles in-radius radius with [color = white] [ stop ] ] ] ] set i i + 1 ] 增加。

请尝试使用以下代码替代@Dependent @Named("myItemReader") public class MyItemReader extends AbstractItemReader { @Resource(name="jdbc/somedb",shareable=false) private DataSource lavDb; private PreparedStatement stmt; private ResultSet rs; @Override public void open(Serializable checkpoint) throws Exception { Connection con = lavDb.getConnection(); con.setAutoCommit(false); stmt = con.prepareStatement("select id from sometable", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); rs = stmt.executeQuery(); } @Override public MyInputRecord readItem() throws Exception{ if(rs.next()){ return new MyInputRecord(rs.getInt(1)); } return null; } @Override public void close(){ try{ rs.close(); stmt.close(); } catch(Exception e){ e.printStackTrace(); } } } 声明:

<dataSource id="oracle-wasadmin" jdbcDriverRef="wasoracledriver" jndiName="jdbc/wasoracledb" type="javax.sql.XADataSource">
    <properties.oracle URL="${wasadmin.jdbcurl}" password="xxxxxx" user="yyyyyy"/>
    <conionManager agedTimeout="1m" maxIdleTime="15m" maxPoolSize="25" minPoolSize="0"/>
</dataSource>

<jdbcDriver id="wasoracledriver" javax.sql.XADataSource="oracle.jdbc.xa.client.OracleXADataSource">
    <library>
        <fileset dir="${shared.resource.dir}/oracle" includes="*.jar"/>
    </library>
</jdbcDriver>