Manager_base.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2006-2008 by Antonello Lobianco                         *
00003  *   http://regmas.org                                                     *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 3 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #include <math.h>
00021 #include <algorithm>
00022 #include "Manager_base.h"
00023 #include "ThreadManager.h"
00024 #include "SuperAgentManager.h"
00025 
00026 //constructor
00027 Manager_base::Manager_base(ThreadManager* MTHREAD_h, string name_h){
00028     MTHREAD=MTHREAD_h;
00029     mouldLocalIDCounter=0;
00030     agentLocalIDCounter=0;
00031     name = name_h;
00032     managerID = MTHREAD->SAM->getNewUniqueManagerID();
00033     MTHREAD_h->treeViewerAddManager(name_h);
00034     MTHREAD->treeViewerAddManagerProperty(name_h, "managed agents");
00035 }
00036 
00037 //destructor
00038 Manager_base::~Manager_base(){
00039     MTHREAD->treeViewerRemoveManager(getName());
00040 
00041     for(uint i=0;i<agentsMoulds.size();i++){
00042         delete agentsMoulds[i];
00043     }
00044     for(uint i=0;i<managedAgents.size();i++){
00045         delete managedAgents[i];
00046     }
00047     for(uint i=0;i<removedAgents.size();i++){
00048         delete removedAgents[i];
00049     }
00050 
00051 }
00052 
00054 //functions:
00055 
00056 Agent_base*
00057 Manager_base::getAgentByLocalID(double localID_h){
00058     for (uint i=0;i< managedAgents.size();i++){
00059         if (managedAgents.at(i)->getAgentLocalID() == localID_h){
00060             return managedAgents.at(i);
00061         }
00062     }
00063 
00064     Agent_base * noAgent;
00065     noAgent = 0;
00066     msgOut(MSG_CRITICAL_ERROR, "Search for an unknow agent localID!");
00067     return noAgent;
00068 }
00069 
00073 void
00074 Manager_base::assignObjectsToAgents(){
00075     msgOut(MSG_DEBUG,"Manager_base::assignObjectsToAgents()");
00076     vector <ModelObject> assignedInitialObjects;
00077     for (uint i=0;i<managedAgents.size();i++){
00078         map<string,double> agentInitialResources = managedAgents.at(i)->getInitialResources();
00079         map<string,double>::iterator p;
00080         for(p=agentInitialResources.begin();p!=agentInitialResources.end();p++){ // loop over a map
00081             assignedInitialObjects = MTHREAD->RD->getBestMatchingInitialObjects (p->first, p->second);
00082             for (uint j=0;j<assignedInitialObjects.size();j++){
00083                 managedAgents.at(i)->acquireObject(assignedInitialObjects.at(j),STAGE_INIT);
00084             }
00085         }
00086     }   
00087 };
00088 
00089 void
00090 Manager_base::rescaleMoulds(double factor_h){
00091     for (uint i=0; i<agentsMoulds.size(); i++){
00092         double oldCoeff = agentsMoulds.at(i)->getMouldCoeff();
00093         int newCoeff = ((int) round(oldCoeff*factor_h) );
00094         agentsMoulds.at(i)->setMouldCoeff(newCoeff);
00095     }
00096 }
00097 
00098 vector <Agent_base*>
00099 Manager_base::getRandomAgents(int size_h){
00100     vector <Agent_base*>  mixedAgents = managedAgents;
00101     if(((uint)size_h) > managedAgents.size()){
00102         msgOut(MSG_CRITICAL_ERROR, "You call the getRandomAgents(size) with size too big ("+i2s(size_h)+")! Not enought agents!"); // 2007.10.18 Antonello, corrected bossible bug
00103     }
00104     random_shuffle(mixedAgents.begin(), mixedAgents.end()); // randomize the elements of the array.
00105     mixedAgents.resize(size_h);
00106     return mixedAgents;
00107 }
00108 
00109 
00110 vector<int>
00111 Manager_base::getMouldIDs(){
00112     vector<int> toReturn;
00113     for (int i=0; i<mouldLocalIDCounter; i++){ toReturn.push_back(((int)i)); }
00114     return toReturn;
00115 }