设为首页 加入收藏

TOP

10.4.4 agent解决方案模型的PBS(1)
2013-10-07 13:00:07 来源: 作者: 【 】 浏览:56
Tags:10.4.4 agent 解决方案 模型 PBS

10.4.4  agent解决方案模型的PBS(1)

分解1:如果您的猜测是正确且及时的,您就会在游戏中获胜。

分解2:如果猜测是由6字符编码组成且编码只包含字符(a~z,0~9)的组合,考虑到重复是允许的,而且该编码是我的agent递交给我的,则称猜测是正确的。

分解3:如果您的猜测是正确的,并且是在2分钟之内产生的,那么您的猜测是及时的。

分解4:如果有足够多的agent进行搜索,那么对编码进行穷举搜索将会成功。

分解5:N个agent足以在2分钟内从四百万个编码的样本中找到正确的编码。

分解6:如果编码每15秒改变一次,需要4N个代理2分钟内从四百万个编码的样本中找到正确的编码。

1. PBS的声明式实现

程序清单10-1遵从PBS的声明式语义。

程序清单10-1

  1. // Listing 10-1   A declarative implementation of the guess_it program.  
  2.                 
  3.  1  #include "posix_process.h"  
  4.  2  #include "posix_queue.h"  
  5.  3  #include "valid_code.h"  
  6.  4  
  7.  5  
  8.  6  char **av;  
  9.  7  char **env;  
  10.  8  
  11.  9  
  12. 10  int main(int argc,char *argv[],char *envp[])  
  13. 11  {  
  14. 12  
  15. 13     valid_code  ValidCode;  
  16. 14     ValidCode.determination("ofind_code");  
  17. 15     cout <<  (ValidCode()   "you win" : "you lose)";  
  18. 16  } 

这样,在第13行声明了ValidCode谓词。这个谓词用来表示如下陈述:

  1. This is the code the trusted agent handed you. 

在第14行,您通过调用谓词ValidCode( )来检测这个陈述。ValidCode( )谓词产生4个进程,每个进程又会依次产生两个线程。因此,实际上ValidCode( )谓词是使用并行编程(www.cppentry.com)来实现的。然而,它的实现被封装了起来。程序清单10-2显示了valid_code谓词类的声明。

程序清单10-2

  1. //Listing 10-2  Declaration of the valid_code predicate class.  
  2.                 
  3.                 
  4.  1  #ifndef __VALID_CODE_H  
  5.  2  #define __VALID_CODE_H  
  6.  3  using namespace std;  
  7.  4  
  8.  5  #include <string> 
  9.  6  class valid_code{  
  10.  7  private:  
  11.  8     string Code;  
  12.  9     float  TimeFrame;  
  13. 10     string Determination;  
  14. 11     bool InTime;  
  15. 12  public:  
  16. 13     bool operator()(void);  
  17. 14     void determination(string X);  
  18. 15  };  
  19. 16  
  20. 17  #endif 

C++(www.cppentry.com)中,谓词是有着返回布尔值的operator( )方法的类。应使用C++(www.cppentry.com)谓词来模拟PBS中谓词的概念。由于谓词是一个C++(www.cppentry.com)类,因此可以与容器及算法共同使用。程序清单10-3显示了谓词类的定义。

程序清单10-3

  1. // Listing 10-3 Definition of the valid_code predicate.  
  2.                 
  3.  1  #include "valid_code.h"  
  4.  2  #include "posix_process.h"  
  5.  3  #include "posix_queue.h"  
  6.  4  
  7.  5  extern char **av;  
  8.  6  extern char **env;  
  9.  7  
  10.  8  
  11.  9  bool valid_code::operator()(void)  
  12. 10  {  
  13. 11     int Status;  
  14. 12     int N;  
  15. 13     string Result;  
  16. 14     posix_process Child[4];  
  17. 15     for(N = 0; N < 2; N++)  
  18. 16     {  
  19. 17        Child[N].binary(Determination);  
  20. 18        Child[N].arguments(av);  
  21. 19        Child[N].environment(env);  
  22. 20        Child[N].run();  
  23. 21        Child[N].pwait(Status);  
  24. 22     }  
  25. 23     posix_queue PosixQueue("queue_name");  
  26. 24     PosixQueue.receive(Result);  
  27. 25     if((Result == Code) && InTime){  
  28. 26        return(true);  
  29. 27     }  
  30. 28     return(false);  
  31. 29  }  
  32. 30  
  33. 31  
  34. 32  void valid_code::determination(string X)  
  35. 33  {  
  36. 34     Determination = X;  
  37. 35  } 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇10.4.4 agent解决方案模型的PBS(.. 下一篇10.4.2 简单策略和粗解决方案模型

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: