
jadex.bdi.blocksworld.BenchmarkPlan Maven / Gradle / Ivy
The newest version!
package jadex.bdi.blocksworld;
import java.util.ArrayList;
import java.util.Random;
import java.util.Set;
import jadex.bdi.annotation.Plan;
import jadex.bdi.annotation.PlanAPI;
import jadex.bdi.annotation.PlanBody;
import jadex.bdi.annotation.PlanCapability;
import jadex.bdi.annotation.PlanReason;
import jadex.bdi.blocksworld.BlocksworldAgent.ConfigureGoal;
import jadex.bdi.runtime.IPlan;
import jadex.common.SUtil;
import jadex.execution.IExecutionFeature;
/**
* Measures Jadex performance by executing several configure goals.
*/
@Plan
public class BenchmarkPlan
{
//-------- attributes --------
/** The number of runs to be performed. */
protected int runs;
/** The number of different goals to be executed per run. */
protected int goals;
@PlanCapability
protected BlocksworldAgent capa;
@PlanAPI
protected IPlan rplan;
@PlanReason
protected ConfigureGoal goal;
/**
* Create new plan.
* @param runs
* @param goals
*/
public BenchmarkPlan(int runs, int goals)
{
this.runs = runs;
this.goals = goals;
}
//-------- methods --------
/**
* The plan body.
*/
@PlanBody
public void body()
{
System.out.println("Performing benchmark ("+runs+" runs with "+goals+" goals each).");
long[] times = new long[runs];
long total = 0;
// Perform runs (first run isn't counted).
for(int run=0; run<=runs; run++)
{
long time = getTime();
// Initialize random number generator.
Random rand = new Random(12345678L);
Block[] blocks = (Block[])capa.getBlocks().toArray(new Block[0]);
for(int i=1; i<=goals; i++)
{
// Create copies of existing blocks.
ArrayList newblocks = new ArrayList();
for(int j=0; j0)
{
Block source = (Block)newblocks.remove(rand.nextInt(newblocks.size()));
Block target = (Block)targets.get(rand.nextInt(targets.size()));
source.stackOn(target);
targets.add(source);
if(!target.isClear())
{
targets.remove(target);
}
}
Set bs = SUtil.arrayToSet(table.getAllBlocks());
ConfigureGoal configure = capa.new ConfigureGoal(table, bs);
rplan.dispatchSubgoal(configure).get();
}
if(run!=0)
{
// Print intermediate result.
times[run-1] = getTime()-time;
total += times[run-1];
System.out.println("Run "+run+" took "+times[run-1]+" milliseconds.");
}
else
System.out.println("Random generator test: "+rand.nextInt(1234));
}
// Print total result.
System.out.println("Benchmark completed.");
System.out.println("Total time (millis): "+total);
System.out.println("Avg. time per run (millis): "+(total/runs));
System.out.println("Avg. time per goal (millis): "+(total/runs/goals));
// Calculate deviation (= quality of measurement).
double deviation = 0;
double avg = total/(double)runs;
for(int i=0; i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy