Monday, January 28, 2013

Visualizing Fitness Function Landscapes

I'm currently preparing a project about game animation tools and technology. The project is exploring and leveraging technologies like physics simulation, optimization and machine learning. If you are interested, the recent review by Geijtenbeek and Pronost is a good starting point.

Related to the optimization of the control parameters of physics simulation, I'm trying to form an intuition about the properties of the related fitness function landscapes. It's impossible to accurately visualize the landscapes of high-dimensional biped motor control and planning problems, but here's a simple 2d physics example: throwing a ball in 2d with the angle and speed as the optimized variables, and allowing damped rebounds from the ground so that the ball trajectory is not just a simple parabola. This isn't exactly rocket science, but I wrote the code to as an exercise in interactive visualization in Matlab, which I stopped using while working outside Aalto.


It was my first time I tried to do implement the "up and down the ladder of abstraction", that is, letting the user explore a summary view (the fitness landscape) to browse concrete examples (the ball trajectories). There was some non-intuitive searching for the right places to put the getCursorInfo(), pause() and subplot() commands to prevent the display from freezing - hope this helps if anyone wants to do the same. I also tried to implement the UI using Matlab's linked data, but couldn't get the display refreshed.

Edit 29.1.2013: I was asked why there's peaks where there should be a continuous ridge. It's apparently because of the large timestep that causes a simulation error that varies as a function of how the floor contacts align with the timestep grid. It also depends on how the contact handling is implemented. The Matlab code below uses a smaller timestep and produces a more continuous ridge.

Here's the Matlab code:


minVel=1;
maxVel=5.0;
minAngle=-0.5*pi;
maxAngle=0.5*pi;
timeStep=0.02;
nSteps=100;
startPos=[0,1.5];
velSteps=64;
angleSteps=64;
E=zeros(velSteps,angleSteps);
target=[2, 0.5];
curve=zeros(nSteps,2);
curves=zeros(nSteps,2,velSteps,angleSteps);

%step over all ball launch speed and angle combinations
for velStep=0:velSteps,
    speed=minVel+(maxVel-minVel)*velStep/velSteps;
    for angleStep=0:angleSteps,
        angle=minAngle+(maxAngle-minAngle)*angleStep/angleSteps;
        pos=startPos;
        vel=[speed*cos(angle), speed*sin(angle)];
        closestDist=10000;
        closest=[0; 0];
        closestTime=0;
        
        %simulate the ball trajectory for this launch velocity
        for n=1:nSteps,
            %save this trajectory point
            curve(n,:)=pos;
            
            %update closest to target
            dist=norm(pos-target,2);
            if (dist<closestDist)
                closestDist=dist;
                closest=pos;
                closestTime=n*timeStep;
            end
            
            %physics update
            pos=pos+vel*timeStep;
            vel(2)=vel(2)-9.81*timeStep;
            
            %bounce from floor
            if (pos(2)<0 && vel(2)<0)
                timeInsideGround=pos(2)/vel(2);
                vel(2)=-0.7*vel(2);
                timeAfterBounce=max(0,timeStep-timeInsideGround);
                pos(2)=timeAfterBounce*vel(2);
            end
            
        end    
        %store the whole trajectory
        curves(:,:,velStep+1,angleStep+1)=curve;

        %update fitness
distanceThreshold=0.1;
        fitness=0.5+0.5*tanh(-(closestDist-distanceThreshold)/0.05);

        %soft threshold for hitting time
        timeThreshold=1;
        fitness=fitness*(0.5+0.5*tanh(-(closestTime-timeThreshold)/0.05));
        
        %store fitness function value for these params
        E(velStep+1,angleStep+1)=fitness;
    end
end
%plot the fitness surface
subplot(1,2,1);
surf(E);

%get the handle for querying data cursor info about the fitness
h=gcf;
dcm_obj = datacursormode(h)

%plot a trajectory of the ball
subplot(1,2,2);
curve=curves(:,:,1,1);
plot(curve(:,1),curve(:,2));

%in a loop, keep plotting the ball trajectories
pause on;
while 1
    info_struct = getCursorInfo(dcm_obj);
    if size(info_struct)~=0    
        curve=curves(:,:,floor(info_struct.Position(2)),floor(info_struct.Position(1)));
        subplot(1,2,2);
        plot(curve(:,1),curve(:,2));
        axis([0 4.5 0 3])
        hold on;
        plot(target(1),target(2),'c+:')
        hold off;
    end
    %have to pause to let the display refresh
    pause(0.01);
end

16 comments:

  1. I also tried to implement the UI using Matlab's linked data, but couldn't get the display refreshed.improve your health and fitness

    ReplyDelete
  2. It Could Be FREE is one of the best online store in US to buy all products on wholesale and cheap prices Gold Straps Open High Heel Sandal Bootie - Born Free, Live Free, Buy Free etc

    ReplyDelete
  3. It means that you are kept aware of choosing the right food options correlating to your body. It will effectively make healthier and more fit, http://findmybud.com/

    ReplyDelete
  4. But for tens of thousands of children whose growth and development are stymied by a deficiency of growth hormone. hgh for sale

    ReplyDelete
  5. The project is exploring and leveraging technologies like physics simulation, optimization and machine learning. If you are interested, the recent review by Geijtenbeek and Pronost is a good starting point. hard wired gaming

    ReplyDelete
  6. MetaBoost - All Natural Metabolism Boost Supports a healthy metabolism. Supports healthy fat oxidation. Increase energy levels. Fight fatigue. Provides a healthy, safe metabolism boost.

    ReplyDelete
  7. CPMC Total Body Fitness group exercise class offers interval training to adults of all ages. Classes are in San Francisco and offered throughout the week. bodyfitnessexpert.com/how-to-regrow-hair-naturally/

    ReplyDelete
  8. This isn't exactly rocket science, but I wrote the code to as an exercise in interactive visualization in Matlab, which I stopped using while working outside Aalto. fat loss factor

    ReplyDelete
  9. HGH doctor provides HGH products, HGH supplements and therapy. Human growth hormone deficiency research. Laurel, Columbia, Marylandhttps://www.flexxlabs.com

    ReplyDelete
  10. Increase Nutrient Absorption. Patented, pharmaceutical grade formula works on proteins, carbohydrates, and fats. Begins working within minutes.
    http://www.droz-forskolin.com/

    ReplyDelete
  11. If you are interested, the recent review by Geijtenbeek and Pronost is a good starting point.anabolic supplements

    ReplyDelete
  12. Nice post! This is a very nice blog that I will definitively come back to more times ! Thanks for informative post. Touche Spa Pune

    ReplyDelete
  13. If you are interested, the recent review by Geijtenbeek and Pronost is a good starting point.
    dianobol

    ReplyDelete


  14. water does not dilute "digestive juices" - they are acidic in nature, it does not change its ability to break down your food products. also note that calories come from fat and protein, not just carbs (for those would there who are worried about calories). I would suggest trying to stay away from "informing" people. Overall, awesome meal ideas, wish they were also wallet friendly. maca root

    ReplyDelete