hopfield_algo.cpp
Go to the documentation of this file.
00001 /*
00002 * Software License Agreement (BSD License)
00003 *Copyright (c) 2015, micROS Team
00004  http://micros.nudt.edu.cn/
00005 *All rights reserved.
00006 * Copyright (c) 2009, Willow Garage, Inc.
00007 * All rights reserved.
00008 *
00009 * Redistribution and use in source and binary forms, with or without
00010 * modification, are permitted provided that the following conditions
00011 * are met:
00012 *
00013 * * Redistributions of source code must retain the above copyright
00014 * notice, this list of conditions and the following disclaimer.
00015 * * Redistributions in binary form must reproduce the above
00016 * copyright notice, this list of conditions and the following
00017 * disclaimer in the documentation and/or other materials provided
00018 * with the distribution.
00019 * * Neither the name of Willow Garage, Inc. nor the names of its
00020 * contributors may be used to endorse or promote products derived
00021 * from this software without specific prior written permission.
00022 *
00023 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00027 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00033 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00034 * POSSIBILITY OF SUCH DAMAGE.
00035 */
00036 #include "hopfield_algo.h"
00037 
00038 //-------------------------------------------------------------
00039 
00041 //              {-1,-2},        { 1,-2},
00042 //      {-2,-1},{-1,-1},{ 0,-1},{ 1,-1},{ 2,-1},
00043 //              {-1, 0},        { 1, 0},
00044 //      {-2, 1},{-1, 1},{ 0, 1},{ 1, 1},{ 2, 1},
00045 //              {-1, 2},        { 1, 2}
00047 int DirAdd[16][2]={     { 0,-1},
00048                 {-1, 0},        { 1, 0},
00049                         { 0, 1},
00051                 {-1,-1},        { 1,-1},
00052 
00053                 {-1, 1},        { 1, 1},
00055                 {-1,-2},        { 1,-2},
00056         {-2,-1},                        { 2,-1},
00057         
00058         {-2, 1},                        { 2, 1},
00059                 {-1, 2},        { 1, 2}
00060 };
00061 int nDirect=8;
00063 
00065 //global varibles
00066 int g_MapWidth=-1;              //width of map
00067 int g_MapHeight=-1;             //height of map
00068 
00069 int g_FlyingHeight=100;         //height of client move 
00070 
00071 short * g_Terrain=NULL;         //rerrian map
00072 int** g_SearchMap=NULL;         //search map
00073 
00074 THREAT_STRUCT g_Threats[100];   //threat
00075 int g_ThreatsCount=0;           //number of threats
00076 
00077 int g_Sx=-1;                    //start point x
00078 int g_Sy=-1;                    //start point y
00079 int g_Ex=-1;                    //end point x
00080 int g_Ey=-1;                    //end point y
00082 
00084 //varibles for plan
00085 int **g_DisMap=NULL;
00086 double **g_Map=NULL;
00087 POINT *g_Road=NULL;
00088 POINT *g_Serial=NULL;
00089 int g_nSerial=0;
00090 int g_nStep=0;
00091 int g_MaxRepNumber=2;
00092 int g_A=10;
00093 double g_InitValue=1e200;
00095 
00097 //get the threats and the configure parameters
00098 bool InitThreat()
00099 {
00100     char Temp[255];
00101     FILE* fp=fopen("src//micros_hopfield-master//map//Environment.ppf","r"); 
00102     if (fp!=NULL)
00103     {
00104         fscanf(fp,"%s\n",Temp);//width of map
00105         g_MapWidth=atoi(Temp);
00106         fscanf(fp,"%s\n",Temp);//height of map
00107         g_MapHeight=atoi(Temp);
00108         fscanf(fp,"%s\n",Temp);//height of client move 
00109         g_FlyingHeight=atoi(Temp);
00110         fscanf(fp,"%s\n",Temp);//number of threats
00111         g_ThreatsCount=atoi(Temp);
00112         for (int i=0;i<g_ThreatsCount;i++)
00113         {
00114             fscanf(fp,"%s\n",Temp);
00115             g_Threats[i].center.x=atoi(Temp);
00116             fscanf(fp,"%s\n",Temp);
00117             g_Threats[i].center.y=atoi(Temp);
00118             fscanf(fp,"%s\n",Temp);
00119             g_Threats[i].range=atoi(Temp);
00120         }
00121         fclose(fp);
00122         cout<<"PPF File Read OK!"<<endl;
00123         return true;
00124     }
00125 
00126     cout<<"PPF File Open Error!"<<endl;
00127     return false;
00128 }
00129 
00130 //get the terrian map
00131 void InitTerrain()
00132 {
00133         if (g_Terrain!=NULL) delete g_Terrain;
00134         int DataSize=g_MapWidth*g_MapHeight;
00135         g_Terrain = new short[DataSize];
00136         FILE* fp = fopen("src//micros_hopfield-master//map//Terrain.raw","rb");
00137         if (fp)
00138         {
00139                 if (fread(g_Terrain,DataSize*2,1,fp)==DataSize*2);
00140                 {
00141                         fclose(fp);
00142                         cout<<"Terrain File Open OK!"<<endl;
00143                         return ;
00144                 }
00145         }
00146         if (g_Terrain!=NULL) delete g_Terrain;
00147         g_Terrain=NULL;
00148         cout<<"Terrain File Open Error!"<<endl;
00149 }
00150 
00151 //get the search map
00152 void BuildSearchMap()
00153 {
00154     //initialize
00155     if (g_SearchMap!=NULL)
00156     {
00157         for (int i=0;i<g_MapHeight;i++)
00158         {
00159             delete g_SearchMap[i];
00160         }
00161         delete g_SearchMap;
00162         g_SearchMap=NULL;
00163     } 
00164     g_SearchMap=new int*[g_MapHeight];
00165     for (int i=0;i<g_MapHeight;i++)
00166     {
00167         g_SearchMap[i]=new int[g_MapWidth];
00168         memset(g_SearchMap[i],0,sizeof(int)*g_MapWidth);
00169     }   
00170 
00171     
00172     if (g_Terrain!=NULL)
00173     {
00174         for (int i=0;i<g_MapHeight;i++)
00175                 for (int j=0;j<g_MapWidth;j++)
00176                 {
00177                         if (g_Terrain[i*g_MapWidth+j]>=g_FlyingHeight) 
00178                                  g_SearchMap[i][j]=-1;
00179                 }
00180     }
00181  
00182 
00183     for (int i=0;i<g_ThreatsCount;i++)
00184     {
00185         for (int ix=0;ix<g_Threats[i].range;ix++)
00186             for (int iy=0;iy<g_Threats[i].range;iy++)
00187             {
00188                 if (ix*ix+iy*iy<g_Threats[i].range*g_Threats[i].range)
00189                 {
00190                     g_SearchMap[g_Threats[i].center.y-iy][g_Threats[i].center.x-ix]=-1;
00191                     g_SearchMap[g_Threats[i].center.y+iy][g_Threats[i].center.x-ix]=-1;
00192                     g_SearchMap[g_Threats[i].center.y-iy][g_Threats[i].center.x+ix]=-1;
00193                     g_SearchMap[g_Threats[i].center.y+iy][g_Threats[i].center.x+ix]=-1;
00194                 }
00195             }
00196     }
00197 }
00198 
00199 void ConnectionSearch()
00200 {
00201     int NowInput=0;
00202     int NowOutput=1;
00203 
00204     g_DisMap[g_Ey][g_Ex]=1;
00205     g_Serial[0].x=g_Ex;
00206     g_Serial[0].y=g_Ey;
00207 
00208     while (NowInput<NowOutput)
00209     {
00210         for (int i=0;i<nDirect;i++)
00211         {
00212             int X=g_Serial[NowInput].x+DirAdd[i][0];
00213             int Y=g_Serial[NowInput].y+DirAdd[i][1];
00214             if ((X>=0)&&(X<g_MapWidth)&&(Y>=0)&&(Y<g_MapHeight))
00215             {
00216                 if (g_DisMap[Y][X]==0)
00217                 {
00218                     g_Map[Y][X]=0.0f;
00219                     g_Serial[NowOutput].x=X;
00220                     g_Serial[NowOutput].y=Y;
00221                     g_DisMap[Y][X]=g_DisMap[g_Serial[NowInput].y][g_Serial[NowInput].x]+1;
00222 
00223                     NowOutput++;
00224                 }
00225             }
00226         }
00227         NowInput++;
00228     }
00229     g_nSerial=NowOutput-1;
00230 }
00231 
00232 //Field
00233 void FieldBuilding()
00234 {
00235     for (int RepNumber=0;RepNumber<g_MaxRepNumber;RepNumber++)
00236     {
00237         int nowdis=1;
00238         int nowpos=0;
00239 
00240         int i=0;
00241         do {
00242             for (int j=0;j<g_A;j++)
00243             {
00244                 for (i=nowpos;i<g_nSerial;i++)
00245                 {
00246                     if (g_DisMap[g_Serial[i].y][g_Serial[i].x]>nowdis) break;
00247                     double Total=0;
00248                     if (i==0)
00249                     {
00250                         Total=g_InitValue;
00251                     }
00252 
00253                     for (int k=0;k<nDirect;k++)
00254                     {
00255                         int X=g_Serial[i].x+DirAdd[k][0];
00256                         int Y=g_Serial[i].y+DirAdd[k][1];
00257                         if ((X>=0)&&(X<g_MapWidth)&&(Y>=0)&&(Y<g_MapHeight))
00258                         {
00259                             if (g_DisMap[Y][X]>=0)
00260                             {
00261                                 Total+=g_Map[Y][X];
00262                             }
00263                         }
00264                     }
00265 
00266                     Total/=g_A;
00267                     g_Map[g_Serial[i].y][g_Serial[i].x]=Total;
00268                 }
00269             }
00270             nowdis++;
00271             nowpos=i;
00272         } while (nowpos<g_nSerial);
00273     }
00274 }
00275 
00276 //search
00277 void RoadSearch()
00278 {
00279     POINT m_Start;
00280     m_Start.x=g_Sx;
00281     m_Start.y=g_Sy;
00282     g_nStep=0;
00283     do
00284     {
00285         bool noget=true;
00286         double max=-1;
00287         POINT newstart={0,0};
00288 
00289         for (int k=0;k<nDirect;k++)
00290         {
00291             int X=m_Start.x+DirAdd[k][0];
00292             int Y=m_Start.y+DirAdd[k][1];
00293             if ((X>=0)&&(X<g_MapWidth)&&(Y>=0)&&(Y<g_MapHeight))
00294             {
00295                 if (((g_Map[Y][X]>=max)||(noget))&&(g_DisMap[Y][X]>0))
00296                 {
00297                     noget=false;
00298                     max=g_Map[Y][X];
00299                     newstart.x=X;
00300                     newstart.y=Y;
00301                 }
00302             }
00303         }
00304 
00305         m_Start.x=newstart.x;
00306         m_Start.y=newstart.y;
00307 
00308         g_Road[g_nStep]=m_Start;
00309 
00310         g_nStep++;
00311     }while(((m_Start.x!=g_Ex)||(m_Start.y!=g_Ey))&&(g_nStep<g_MapWidth*g_MapHeight));
00312 }
00314 
00315 
00316 
00318 //function for port
00319 
00320 //clean the RAM
00321 void Clean()
00322 {
00323     if (g_Map!=NULL)
00324     {
00325         for (int i=0;i<g_MapHeight;i++)
00326         {
00327             delete g_Map[i];
00328             delete g_DisMap[i];
00329         }
00330         delete g_Map;
00331         g_Map=NULL;
00332 
00333         delete g_DisMap;
00334         g_DisMap=NULL;
00335 
00336         delete g_Serial;
00337         g_Serial=NULL;
00338 
00339         delete g_Road;
00340         g_Road=NULL;
00341     }
00342 }
00343 
00344 //initialize
00345 bool InitData()
00346 {
00347     Clean();
00348 
00349     g_nSerial=0;
00350     g_Serial=new POINT[g_MapWidth*g_MapHeight];
00351     g_Road=new POINT[g_MapWidth*g_MapHeight];
00352     g_DisMap=new int*[g_MapHeight];
00353     g_Map=new double*[g_MapHeight];
00354 
00355     for (int i=0;i<g_MapHeight;i++)
00356     {
00357         g_DisMap[i]=new int[g_MapWidth];
00358         g_Map[i]=new double[g_MapWidth];
00359 
00360         memcpy(g_DisMap[i],g_SearchMap[i],sizeof(int)*g_MapWidth);
00361     }
00362 
00363     return true;
00364 }
00365 
00366 
00367 bool Search()
00368 {
00369     printf("Hopfield Planing...\n");
00370     ConnectionSearch();
00371     FieldBuilding();
00372     RoadSearch();
00373     return true;
00374 }
00375 
00376 
00377 int GetRoad(POINT** Road)
00378 {
00379     *Road=new POINT[g_nStep];
00380     memcpy(*Road,g_Road,sizeof(POINT)*g_nStep);
00381     return g_nStep;
00382 }
00383 
00384 //-------------------------------------------------------------
00385 
00386 


micros_hopfield
Author(s): Xiaojia Xiang
autogenerated on Thu Jun 6 2019 21:05:58