Package net.javlov.world.phys2d

Source Code of net.javlov.world.phys2d.Phys2DAgentBody

/*
* Javlov - a Java toolkit for reinforcement learning with multi-agent support.
*
* Copyright (c) 2009 Matthijs Snel
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package net.javlov.world.phys2d;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import net.javlov.Action;
import net.javlov.Agent;
import net.javlov.State;
import net.javlov.VectorState;
import net.javlov.world.AgentBody;
import net.javlov.world.PhysicalSensor;
import net.javlov.world.Sensor;

import net.phys2d.math.ROVector2f;
import net.phys2d.raw.shapes.DynamicShape;

public class Phys2DAgentBody extends Phys2DBody implements AgentBody {

  protected List<Sensor<double[]>> sensors;
  protected List<PhysicalSensor> physSensors;

  public Phys2DAgentBody(DynamicShape shape, float m) {
    super(shape, m, false);
    sensors = new ArrayList<Sensor<double[]>>();
    physSensors = new ArrayList<PhysicalSensor>();
  }
 
  public void add(Sensor s) {
    sensors.add(s);
    if ( s instanceof PhysicalSensor )
      physSensors.add((PhysicalSensor) s);
  }
 
  @SuppressWarnings("unchecked")
  public List<Sensor<double[]>> getSensors() {
    return Collections.unmodifiableList(sensors);
  }
 
  //// OVERRIDE BODY METHODS TO SYNC SENSORS WITH MOVEMENT ////
  @Override
  public void adjustPosition(ROVector2f delta, float scale) {
    super.adjustPosition(delta, scale);
    translateSensors(delta.getX()*scale, delta.getY()*scale);
  }

  @Override
  public void adjustRotation(float delta) {
    super.adjustRotation(delta);
    rotateSensors(delta);
  }
 
  private void translateSensors(double dx, double dy) {
    for ( PhysicalSensor s : physSensors )
      s.translate(dx, dy);
  }
 
  private void rotateSensors(double angle) {
    ROVector2f pos = getPosition();
    for ( PhysicalSensor s : physSensors )
      s.rotate(angle, pos.getX(), pos.getY());
  }
 
  //// ENVIRONMENT METHODS //////
  @Override
  public double executeAction(Action act, Agent a) {
    throw new UnsupportedOperationException();
  }

  @Override
  public State<double[]> getObservation(Agent agent) {
    VectorState st = new VectorState();
    for ( Sensor<double[]> s : sensors )
      st.append(s.getReading());
    return st;
  }

  @Override
  public void init() {
    for ( Sensor s : sensors )
      s.init();
  }
 
  @Override
  public void reset() {
    for ( Sensor s : sensors )
      s.reset();
  }

  @Override
  public int getObservationDim() {
    int dim = 0;
    for ( Sensor<double[]> s : sensors )
      dim += s.getReadingDim();
    return dim;
  }
}
TOP

Related Classes of net.javlov.world.phys2d.Phys2DAgentBody

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.