All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jadex.micro.examples.heatbugs.HeatbugAgent Maven / Gradle / Ivy

Go to download

The Jadex micro applications package contains several example applications, benchmarks and testcases using micro agents.

There is a newer version: 4.0.267
Show newest version
package jadex.micro.examples.heatbugs;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import jadex.application.EnvironmentService;
import jadex.bridge.IComponentStep;
import jadex.bridge.IInternalAccess;
import jadex.bridge.component.IExecutionFeature;
import jadex.commons.future.ExceptionDelegationResultListener;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;
import jadex.extension.envsupport.environment.ISpaceAction;
import jadex.extension.envsupport.environment.ISpaceObject;
import jadex.extension.envsupport.environment.space2d.Grid2D;
import jadex.extension.envsupport.environment.space2d.Space2D;
import jadex.extension.envsupport.math.IVector2;
import jadex.extension.envsupport.math.Vector1Int;
import jadex.micro.annotation.Agent;
import jadex.micro.annotation.AgentBody;

/**
 *  The heatbug agent.
 */
@Agent
public class HeatbugAgent
{
	//-------- attributes --------
	
	/** The agent. */
	@Agent
	protected IInternalAccess agent;
	
	/** The probability of a random move. */
	protected double randomchance;
	
	/** The desired temperature. */
	protected double ideal_temp;
	
	/** The current temperature. */
	protected double mytemp;
	
	/** The current unhappiness. */
	protected double unhappiness;


	//-------- methods --------
	
	/**
	 *  Execute an agent step.
	 */
	@AgentBody
	public IFuture executeBody()
	{
		final Future	ret	= new Future();
		
		EnvironmentService.getSpace(agent, "mygc2dspace")
			.addResultListener(new ExceptionDelegationResultListener(ret)
		{
			public void customResultAvailable(Object result)
			{
				final Grid2D grid = (Grid2D)result;
				ISpaceObject avatar = grid.getAvatar(agent.getComponentDescription());
				
//						unhappiness = Math.abs(ideal_temp - temp);
				randomchance = ((Number)avatar.getProperty("random_move_chance")).doubleValue();
				ideal_temp = ((Number)avatar.getProperty("ideal_temp")).doubleValue();
//						System.out.println("ideal_temp: "+ideal_temp+" "+getArgument("ideal_temp"));
				
				IComponentStep com = new IComponentStep()
				{
					public IFuture execute(IInternalAccess ia)
					{
						ISpaceObject avatar = grid.getAvatar(agent.getComponentDescription());
						IVector2 mypos = (IVector2)avatar.getProperty(Space2D.PROPERTY_POSITION);
						Collection coll	= grid.getSpaceObjectsByGridPosition(mypos, "patch");
						if(coll!=null)
						{
							ISpaceObject patch = (ISpaceObject)coll.iterator().next();
							mytemp = ((Number)patch.getProperty("heat")).doubleValue();
	
							unhappiness = ((Number)avatar.getProperty("unhappiness")).doubleValue();
							if(unhappiness>0)
							{
								Set tmp = grid.getNearObjects((IVector2)avatar.getProperty(
									Space2D.PROPERTY_POSITION), new Vector1Int(1), "patch");
								tmp.remove(patch);
								ISpaceObject[] neighbors = (ISpaceObject[])tmp.toArray(new ISpaceObject[tmp.size()]); 
								
								IVector2 target = null;
								if(Math.random()ideal_temp)
									{
										ISpaceObject min = patch;
										double minheat = mytemp;
										for(int i=0; imaxheat)
											{
												max = neighbors[i];
												maxheat = heat;
											}
										}
										target = (IVector2)max.getProperty(Space2D.PROPERTY_POSITION);
									}
								}
								
	//									if(!target.equals(mypos))
								{
	//										System.out.println("res: "+avatar.getProperty(ISpaceObject.PROPERTY_OWNER)+" "+target);
									Map params = new HashMap();
									params.put(ISpaceAction.OBJECT_ID, avatar.getId());
									params.put(MoveAction.PARAMETER_POSITION, target);
									grid.performSpaceAction("move", params, null);
								}
							}
						}
						
						agent.getComponentFeature(IExecutionFeature.class).waitForTick(this);
						return IFuture.DONE;
					}
					
					public String toString()
					{
						return "heatbug.body()";
					}
				};
				
				agent.getComponentFeature(IExecutionFeature.class).waitForTick(com);
			}
		});
		
		return ret; // never kill!
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy