Skip to content

Example scenario model Robot Scenario

Junho Kim edited this page Jan 16, 2017 · 3 revisions

Robot scenario

This scenario is very simple to understand our ABC model concept and how simulation works.

Above figure is illustration of our case scenario. In scenario, there are three robots. Each robot grab their items and carries items until final lane.

public class Movement extends BaseAction {

	public Movement(){
		super.setDuration(1);
		super.setActionType(TYPE.NORMAL);
	}

	@Override
	public void addBenefit(int additionalBenefit) {

	}

	@Override
	public Movement clone() {

		Movement _copy = new Movement();
		_copy.setDuration(1);
		super.setActionType(TYPE.NORMAL);

		return _copy;
	}

	@Override
	public void reset() {

	}

	@Override
	public void randomGenerate() {
		// Not-used
	}

	@Override
	public String getDebugTrace() {
		return null;
	}

	@Override
	public DebugProperty getDebugProperty() {
		return null;
	}
}

As you can notice from this code, if you don't need to use the abstracted method, then you leave the overridden method as empty. The point is that you need to extend BaseAction class.

public class Robot extends BaseConstituent{

	private int xpos;
	private int ypos;
	private boolean token; // False -> drop, True -> no drop
	private Movement move;
	private Random ranNumGenerator;

	public Robot(int ypos, Movement move){
		this.xpos = 10;
		this.ypos = ypos;
		this.token = false;
		this.move = move;
		this.ranNumGenerator = new Random();
		this.setStatus(Status.IDLE);
	}

	@Override
	public void normalAction(int elapsedTime) {
		if(xpos != 10) {
			int ranNum = this.ranNumGenerator.nextInt(100);
			if (ranNum == 0) {
				token = false; // loose its token;
			}
		}

		if(token) {
			xpos++;
			this.setStatus(Status.IDLE);
		}
		else
			this.setStatus(Status.END);
		if(this.xpos > 20){
			this.updateCostBenefit(0, 0, 1); // SoS-benefit is complete the task
			this.setStatus(Status.END);
		}
	}

	@Override
	public BaseAction immediateAction() {
		if(this.xpos == 10)
		{
			token = true; // token pick
			Movement do_movement = this.move.clone();
			this.setStatus(Status.OPERATING);
			do_movement.startHandle();
			do_movement.addPerformer(this);
			return do_movement;
		}else if(token){
			Movement do_movement = this.move.clone();
			this.setStatus(Status.OPERATING);
			do_movement.startHandle();
			do_movement.addPerformer(this);
			return do_movement;
		}
		return null;
	}

	public BaseAction step(){
		if(this.getRemainBudget() == 0){
			return null; // voidAction, nothing happen
		}else{
			if(this.getStatus() == Status.IDLE){ // Select an action
				BaseAction a = new DummyAction("Immediate action", 0, 0);
				a.addPerformer(this);
				a.setActionType(BaseAction.TYPE.IMMEDIATE);
				return a;
			}else if(this.getStatus() == Status.OPERATING){ // Operation step
				return this.move;
			}
		}
		return null;
	}

	@Override
	public BaseConstituent clone() {
		return null;
	}

	@Override
	public BaseAction getCurrentAction() {
		return move;
	}

	@Override
	public void reset(){
		this.xpos = 10;
		this.token = false;
		super.reset();
	}

	@Override
	public DebugProperty getDebugProperty(){
		return null;
	}

	@Override
	public String getName() {
		return null;
	}
}

This is the Robot class. You must implement at least three methods; step, immediateAction, and normalAction method. step method is generating next action to operate in this CS. 'immediateAction' method is the action takes no time. For example, you can use this method as abstracted decision making mechanism. normalAction is the action takes time. Real movement implementation can be implemented here.

public class RobotScenario  extends BaseScenario {
	private int endTick;
	private ArrayList<BaseConstituent> csList;
	private BaseConstituent manager;
	private Environment env;
	private ArrayList<BaseAction> actionList;

	public RobotScenario(int type) {
		Movement right_move = new Movement();

		Robot r1, r2, r3;

		r1.addCapability(right_move, 1);
		r2.addCapability(right_move, 1);
		r3.addCapability(right_move, 1);

		BaseConstituent[] CSs = new BaseConstituent[] {r1, r2, r3};
		BaseAction[] moves = new BaseAction[] {right_move};

		this.csList = new ArrayList<>();
		this.csList.addAll(Arrays.asList(CSs));
		this.manager = null;

		this.actionList = new ArrayList<>();
		this.actionList.addAll(Arrays.asList(moves));

		this.env = new Environment(CSs, this.actionList.toArray(new BaseAction[this.actionList.size()]));

		this.endTick = 12;
	}

	@Override
	public void init() {
	}

	@Override
	public String getDescription() {
		return "Robot";
	}

	@Override
	public ArrayList<BaseConstituent> getCSList() {
		return this.csList;
	}

	@Override
	public BaseConstituent getManager() {
		return this.manager;
	}

	@Override
	public void setCSList(BaseConstituent[] CSs) {
		if(this.csList != null)
			this.csList.clear();
		else
			this.csList = new ArrayList<>();
		this.csList.addAll(Arrays.asList(CSs));
	}

	@Override
	public void setManager(BaseConstituent manager) {
		this.manager = manager;
	}

	@Override
	public void setEnvironment(Environment env) {
		this.env = env;
	}

	@Override
	public Environment getEnvironment() {
		return env;
	}

	@Override
	public ArrayList<BaseAction> getActionList() {
		return this.actionList;
	}

	@Override
	public void setActionList(ArrayList<BaseAction> aList) {
		this.actionList = aList;
	}

	@Override
	public int getEndTick() {
		return endTick;
	}

	@Override
	public void setEndTick(int endTick) {
		this.endTick = endTick;
	}
}

Above code is scenario file. You need to implement a scenario class for simulation. Simulator class takes a scenario class as an input for simulation. You need to extends the BaseScenario class. In the constructor, you need to include which CSs are joined in simulation and their capabilities and when simulation procedure ends.

Clone this wiki locally