📄 swarm.overview.mag4.build-agents.sect1.html
字号:
<HTML><HEAD><TITLE>Building Agents</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.53"><LINKREL="HOME"TITLE="Documentation Set for Swarm 2.1.1"HREF="set.html"><LINKREL="UP"TITLE="Mag 4x:Sketch of Code "HREF="swarm.overview.mag4.chapter.html"><LINKREL="PREVIOUS"TITLE="Defining an Agent"HREF="swarm.overview.mag4.define-agent.sect1.html"><LINKREL="NEXT"TITLE="Building Space objects"HREF="swarm.overview.mag4.build-space.sect1.html"></HEAD><BODYCLASS="SECT1"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#0000FF"VLINK="#840084"ALINK="#0000FF"><DIVCLASS="NAVHEADER"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Documentation Set for Swarm 2.1.1</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="swarm.overview.mag4.define-agent.sect1.html">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 4. Mag 4x:Sketch of Code</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="swarm.overview.mag4.build-space.sect1.html">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="SWARM.OVERVIEW.MAG4.BUILD-AGENTS.SECT1">4.3. Building Agents</A></H1><P>Now that Heatbugs have been defined, the model swarm needs to create them. This code fragment is from the <TTCLASS="LITERAL">buildObjects</TT> method on HeatbugModelSwarm.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> // A loop to create a bunch of heatbugs.for (i = 0; i < numBugs; i++) { Heatbug * hbug; int idealTemp, outputHeat; // Choose a random ideal temperature, output heat from the specified // range (model parameters). idealTemp = [uniformRandom rMin: minIdealTemp Max: maxIdealTemp]; outputHeat = [uniformRandom rMin: minOutputHeat Max: maxOutputHeat]; // Create the heatbug, set the creation time variables hbug = [Heatbug createBegin: [self getZone]]; [hbug setWorld: world Heat: heat]; hbug = [hbug createEnd]; // Add the bug to the end of the list. [heatbugList addLast: hbug]; // Now initialize the rest of the heatbug's state. [hbug setIdealTemperature: idealTemp]; [hbug setOutputHeat: outputHeat]; [hbug setX: [uniformRandom rMax: worldXSize] // random position Y: [uniformRandom rMax: worldYSize]];} </PRE></TD></TR></TABLE><P>The details of this code are best explained in reading the documentation for the libraries and the heatbugs demo application itself. Essentially, we first generate two random numbers: an ideal temperature and an output heat for the new Heatbug. We then create the Hheatbug itself with <TTCLASS="LITERAL">createBegin</TT> and fill in the required parameters of world and heat. Once those are set, we can send <TTCLASS="LITERAL">createEnd</TT> to the Heatbug and it is finished being created. After it's done being created we add it into a list of Heatbugs in the model and set a few parameters on it like the ideal temperature and the initial position.</P></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="swarm.overview.mag4.define-agent.sect1.html">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="set.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="swarm.overview.mag4.build-space.sect1.html">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Defining an Agent</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="swarm.overview.mag4.chapter.html">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Building Space objects</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -