PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/assign3.cpp

https://github.com/ravikumarj/Alpha-beta-pruning
C++ | 1589 lines | 1269 code | 192 blank | 128 comment | 230 complexity | 5873b3b55f81c57a49f3b6dd742c9b9e MD5 | raw file
  1. #include<iostream>
  2. #include <vector>
  3. #include <iostream>
  4. #include <string>
  5. #include <queue>
  6. #include <stack>
  7. #include <list>
  8. #include <stdio.h>
  9. #include <fstream>
  10. #include <string.h>
  11. #include<stdlib.h>
  12. #include <stddef.h>
  13. #include <map>
  14. #include <cmath>
  15. using namespace std;
  16. #define PINF 65535
  17. #define NINF -65535
  18. int global_turn=0;
  19. FILE *opf=NULL;
  20. FILE *opl=NULL;
  21. class Vertex;
  22. class Edge
  23. {
  24. public:
  25. Edge(Vertex *org, Vertex *dest)
  26. {
  27. origin = org;
  28. destination = dest;
  29. }
  30. Vertex* getOrigin() {return origin;}
  31. Vertex* getDestination() {return destination;}
  32. private:
  33. Vertex* origin;
  34. Vertex* destination;
  35. };
  36. class Vertex
  37. {
  38. public:
  39. float cost;
  40. bool explored;
  41. //Vertex *parent;
  42. int depth;
  43. int group;
  44. int temp_group;
  45. int commit_group;
  46. Vertex(string id,float cost,int group)
  47. {
  48. depth=-1;
  49. //parent=NULL;
  50. explored=false;
  51. name = id;
  52. this->cost=cost;
  53. this->group=group;
  54. this->temp_group=group;
  55. }
  56. void addEdge(Vertex *v)
  57. {
  58. Edge newEdge(this, v);
  59. edges.push_back(newEdge);
  60. }
  61. std::list<string> getFreeNeihbor(int &len)
  62. {
  63. int j=0;
  64. std::list<string> adj;
  65. //char *adj=new char[30];
  66. for (int i = 0; i < edges.size(); i++)
  67. {
  68. Edge e = edges[i];
  69. //cout<<"Neihbor "<<e.getOrigin()->getName()<<"-->"<<e.getDestination()->getName()[0]<<endl;
  70. if(e.getDestination()->group == 0)
  71. {
  72. adj.push_back(e.getDestination()->getName());
  73. //adj[j]= e.getDestination()->getName()[0];
  74. // cout<<"Free ones-->"<<adj[j]<<endl;
  75. j++;
  76. }
  77. }
  78. len=j;
  79. return adj;
  80. }
  81. int opponentNeighborCost(int groupl)
  82. {
  83. float c=0;
  84. for (int i = 0; i < edges.size(); i++)
  85. {
  86. Edge e = edges[i];
  87. if(e.getDestination()->group == groupl)
  88. {
  89. //cout<<"\ngroupl\t"<<e.getDestination()->group<<","<<groupl;
  90. c=c+ e.getDestination()->cost;
  91. }
  92. }
  93. //cout<<"\nOpponent's Neihbor cost -->"<<c;
  94. return c;
  95. }
  96. void captureOpponent()
  97. {
  98. //cout<<"\ncapture"<<!(this->group)<<endl;
  99. int groupl=0;
  100. if(this->group == 1)
  101. groupl=-1;
  102. else if (this->group == -1)
  103. groupl=1;
  104. for (int i = 0; i < edges.size(); i++)
  105. {
  106. Edge e = edges[i];
  107. if(e.getDestination()->group == groupl)
  108. {
  109. e.getDestination()->group=this->group;
  110. }
  111. }
  112. }
  113. void printEdges()
  114. {
  115. cout << name << ":" << endl;
  116. for (int i = 0; i < edges.size(); i++)
  117. {
  118. Edge e = edges[i];
  119. cout <<e.getOrigin()->getName()<<"-->"<< e.getDestination()->getName() << endl;
  120. }
  121. cout << endl;
  122. }
  123. string getName() {return name;}
  124. vector<Edge> getEdges() {return edges;}
  125. string name;
  126. vector<Edge> edges;
  127. };
  128. class obj
  129. {
  130. public:
  131. float cost;
  132. int group;
  133. int dest;
  134. int action;
  135. float alpha;
  136. float beta;
  137. };
  138. struct CompareNode : public std::binary_function<obj,obj,bool>
  139. {
  140. bool operator()(obj lhs, obj rhs) const
  141. {
  142. return lhs.cost < rhs.cost;
  143. }
  144. };
  145. class Graph
  146. {
  147. public:
  148. Graph() {
  149. this->depth_terminal=0;
  150. alpha_stat=-65535;
  151. }
  152. void insert(Vertex *v)
  153. {
  154. vertices.push_back(v);
  155. }
  156. void printGraph()
  157. {
  158. for (int i = 0; i < vertices.size(); i++)
  159. vertices[i]->printEdges();
  160. }
  161. void reset()
  162. {
  163. for (int i = 0; i < vertices.size(); i++)
  164. {
  165. vertices[i]->explored=false;
  166. // vertices[i]->parent=NULL;
  167. }
  168. }
  169. int getIndex(string nodename)
  170. {
  171. for(int i = 0; i < vertices.size(); i++)
  172. {
  173. //cout<<vertices[i]->name[0]<<"getInd"<<endl;
  174. if(vertices[i]->name ==nodename)
  175. {
  176. return i;
  177. }
  178. }
  179. return -1;
  180. }
  181. void print_glog(obj ob)
  182. {
  183. fprintf(opl,"Union,");
  184. if(ob.action==0)
  185. {
  186. fprintf(opl,"Force March,");
  187. }
  188. else
  189. {
  190. fprintf(opl,"Paratroop Drop,");
  191. }
  192. fprintf(opl,"%s,1,%.1f\n",vertices[ob.dest]->getName().c_str(),ob.cost);
  193. }
  194. void print_minmaxlog(int dest=-1,float value=-1,int action=-1,int player=-1,int depth=-1)
  195. {
  196. if(global_turn == 0)
  197. {
  198. if(dest == -1)
  199. {
  200. fprintf(opl,"N/A,N/A,N/A,0,");
  201. if(value <= -30000)
  202. {
  203. fprintf(opl,"-Infinity\n");
  204. }
  205. else if(value >= 30000)
  206. {
  207. fprintf(opl,"Infinity\n");
  208. }
  209. else
  210. {
  211. fprintf(opl,"%.1f\n",value);
  212. }
  213. }
  214. else
  215. {
  216. if(player == 1)
  217. {
  218. fprintf(opl,"Union,");
  219. }
  220. else
  221. {
  222. fprintf(opl,"Confederacy,");
  223. }
  224. if(action==0)
  225. {
  226. fprintf(opl,"Force March,");
  227. }
  228. else
  229. {
  230. fprintf(opl,"Paratroop Drop,");
  231. }
  232. fprintf(opl,"%s,%d,",vertices[dest]->getName().c_str(),depth);
  233. if(value <= -30000)
  234. {
  235. fprintf(opl,"-Infinity\n");
  236. }
  237. else if(value >= 30000)
  238. {
  239. fprintf(opl,"Infinity\n");
  240. }
  241. else
  242. {
  243. fprintf(opl,"%.1f\n",value);
  244. }
  245. }
  246. }
  247. }
  248. void print_prunedlog(int dest=-1,float value=-1,int action=-1,int player=-1,int depth=-1,float alpha=-1,float beta=-1,int cutoff=-1)
  249. {
  250. if(global_turn == 0)
  251. {
  252. if(dest == -1)
  253. {
  254. fprintf(opl,"N/A,N/A,N/A,0,");
  255. if(value <= -30000)
  256. {
  257. fprintf(opl,"-Infinity,");
  258. }
  259. else if(value >= 30000)
  260. {
  261. fprintf(opl,"Infinity,");
  262. }
  263. else
  264. {
  265. fprintf(opl,"%.1f,",value);
  266. }
  267. if(alpha <= -30000)
  268. {
  269. fprintf(opl,"-Infinity,");
  270. }
  271. else if(alpha >= 30000)
  272. {
  273. fprintf(opl,"Infinity,");
  274. }
  275. else
  276. {
  277. fprintf(opl,"%.1f,",alpha);
  278. }
  279. if(beta <= -30000)
  280. {
  281. fprintf(opl,"-Infinity");
  282. }
  283. else if(beta >= 30000)
  284. {
  285. fprintf(opl,"Infinity");
  286. }
  287. else
  288. {
  289. fprintf(opl,"%.1f",beta);
  290. }
  291. }
  292. else
  293. {
  294. if(player == 1)
  295. {
  296. fprintf(opl,"Union,");
  297. }
  298. else
  299. {
  300. fprintf(opl,"Confederacy,");
  301. }
  302. if(action==0)
  303. {
  304. fprintf(opl,"Force March,");
  305. }
  306. else
  307. {
  308. fprintf(opl,"Paratroop Drop,");
  309. }
  310. fprintf(opl,"%s,%d,",vertices[dest]->getName().c_str(),depth);
  311. if(value <= -30000)
  312. {
  313. fprintf(opl,"-Infinity,");
  314. }
  315. else if(value >= 30000)
  316. {
  317. fprintf(opl,"Infinity,");
  318. }
  319. else
  320. {
  321. fprintf(opl,"%.1f,",value);
  322. }
  323. if(alpha <= -30000)
  324. {
  325. fprintf(opl,"-Infinity,");
  326. }
  327. else if(alpha >= 30000)
  328. {
  329. fprintf(opl,"Infinity,");
  330. }
  331. else
  332. {
  333. fprintf(opl,"%.1f,",alpha);
  334. }
  335. if(beta <= -30000)
  336. {
  337. fprintf(opl,"-Infinity");
  338. }
  339. else if(beta >= 30000)
  340. {
  341. fprintf(opl,"Infinity");
  342. }
  343. else
  344. {
  345. fprintf(opl,"%.1f",beta);
  346. }
  347. }
  348. if(cutoff == 1)
  349. {
  350. fprintf(opl,",CUT-OFF\n");
  351. }
  352. else
  353. {
  354. fprintf(opl,"\n");
  355. }
  356. }
  357. }
  358. std::list<int> fmpdCandidates(int groupl)
  359. {
  360. //int fmpdlist=new int[30];
  361. std::list<int> fmlist;
  362. int k=0;
  363. int len=0;
  364. for(int i = 0; i < vertices.size(); i++)
  365. {
  366. //cout<<"In fmpd"<<endl;
  367. if(vertices[i]->group == groupl)
  368. {
  369. //cout<<"\nInside";
  370. std::list <string>adj=vertices[i]->getFreeNeihbor(len);
  371. for (std::list<string>::iterator it=adj.begin(); it!=adj.end(); ++it)
  372. fmlist.push_back(getIndex(*it));
  373. }
  374. }
  375. fmlist.sort();
  376. fmlist.unique();
  377. //fmpdlist.unique(same_int);
  378. return fmlist;
  379. }
  380. std::list<int> pdCandidates(int group)
  381. {
  382. std::list<int> pdlist;
  383. //int *pdlist=new int[30];
  384. int k=0;
  385. for(int i = 0; i < vertices.size(); i++)
  386. {
  387. if(vertices[i]->group == 0)
  388. {
  389. // pdlist[k]=i;
  390. pdlist.push_back(i);
  391. // k++;
  392. }
  393. }
  394. pdlist.sort();
  395. pdlist.unique();
  396. return pdlist;
  397. }
  398. float Eval(int groupl)
  399. {
  400. float A=0,B=0;
  401. for(int i = 0; i < vertices.size(); i++)
  402. {
  403. if(vertices[i]->group == 1)
  404. {
  405. A=A+vertices[i]->cost;
  406. }
  407. if(vertices[i]->group == -1)
  408. {
  409. B=B+vertices[i]->cost;
  410. }
  411. }
  412. if(groupl == 1)
  413. {
  414. return A-B;
  415. }
  416. else if(groupl == -1)
  417. {
  418. return B-A;
  419. }
  420. }
  421. bool gameend()
  422. {
  423. int flag=0;
  424. for(int i = 0; i < vertices.size(); i++)
  425. {
  426. if(vertices[i]->group == 0)
  427. {
  428. flag=1;
  429. }
  430. }
  431. if(flag == 1)
  432. return false;
  433. else
  434. return true;
  435. }
  436. void print_path(int dest=-1,int action=-1,int group=0)
  437. {
  438. static int turn=0;
  439. fprintf(opf,"\nTURN = %d\n",turn);
  440. //cout<<"dest "<<dest<<endl;
  441. if(turn ==0)
  442. {
  443. fprintf(opf,"Player = N/A\n");
  444. fprintf(opf,"Action = N/A\n");
  445. fprintf(opf,"Destination = N/A\n");
  446. }
  447. else
  448. {
  449. if (group==1)
  450. fprintf(opf,"Player =Union\n");
  451. else
  452. fprintf(opf,"Player =Confederacy\n");
  453. if(action ==0)
  454. fprintf(opf,"Action = Force March\n");
  455. else
  456. fprintf(opf,"Action = Paratroop Drop\n");
  457. fprintf(opf,"Destination =%s\n",vertices[dest]->getName().c_str());
  458. }
  459. turn++;
  460. float cost=0;
  461. fprintf(opf,"Union ,{");
  462. for(int i = 0; i < vertices.size(); i++)
  463. {
  464. if(vertices[i]->group == 1)
  465. {
  466. fprintf(opf,"%s,",vertices[i]->getName().c_str());
  467. cost=cost+vertices[i]->cost;
  468. }
  469. }
  470. fprintf(opf,"},%.1f\n",cost);
  471. cost=0;
  472. fprintf(opf,"Confederacy ,{");
  473. for(int i = 0; i < vertices.size(); i++)
  474. {
  475. if(vertices[i]->group == -1)
  476. {
  477. fprintf(opf,"%s,",vertices[i]->getName().c_str());
  478. // cout<<vertices[i]->getName();
  479. cost=cost+vertices[i]->cost;
  480. }
  481. }
  482. fprintf(opf,"},%.1f\n",cost);
  483. fprintf(opf,"---------------------------------\n");
  484. }
  485. void greedy()
  486. {
  487. int first_step=1;
  488. std::list<int> fm;
  489. std::list<int> pd;
  490. obj ob;
  491. obj temp;
  492. float es=Eval(1);
  493. print_path();
  494. while(1)
  495. {
  496. es=Eval(1);
  497. priority_queue<obj ,std::vector<obj >,CompareNode > pq;
  498. fm=fmpdCandidates(1);
  499. //cout<<"\nUnion es\t"<<es;
  500. //cout<<"\nfm Candidates";
  501. for (std::list<int>::iterator it=fm.begin(); it!=fm.end(); ++it)
  502. {
  503. obj ob;
  504. ob.cost=es+vertices[*it]->opponentNeighborCost(-1)+vertices[*it]->cost+vertices[*it]->opponentNeighborCost(-1);
  505. //cout<<"ob cost "<<ob.cost<<endl;
  506. ob.dest=*it;
  507. ob.group=1;
  508. ob.action=0;
  509. if(first_step == 1)
  510. {
  511. print_glog(ob);
  512. }
  513. pq.push(ob);
  514. }
  515. pd=pdCandidates(1);
  516. //cout<<"\npd Candidates";
  517. for (std::list<int>::iterator it=pd.begin(); it!=pd.end(); ++it)
  518. {
  519. obj ob;
  520. ob.cost=es+vertices[*it]->cost;
  521. ob.dest=*it;
  522. ob.group=1;
  523. ob.action=1;
  524. if(first_step == 1)
  525. {
  526. print_glog(ob);
  527. }
  528. //cout<<"ob=="<<vertices[ob.dest]->getName()<<","<<ob.cost<<endl;
  529. pq.push(ob);
  530. }
  531. first_step=0;
  532. // cout<<"\n And the Winner is \n";
  533. temp=pq.top();
  534. // pq.pop();
  535. //cout<<"Dest -->"<<temp.dest<<",action -->"<<temp.cost<<"\t"<<pq.top().cost;
  536. priority_queue<obj ,std::vector<obj >,CompareNode > pq1;
  537. vertices[temp.dest]->group=1;
  538. if(temp.action == 0)
  539. {
  540. vertices[temp.dest]->captureOpponent();
  541. }
  542. print_path(temp.dest,temp.action,temp.group);
  543. if(gameend())
  544. break;
  545. es=Eval(-1);
  546. //cout<<"\nConf es"<<es;
  547. fm=fmpdCandidates(-1);
  548. for (std::list<int>::iterator it=fm.begin(); it!=fm.end(); ++it)
  549. {
  550. ob.cost=es+vertices[*it]->opponentNeighborCost(1)+vertices[*it]->cost+vertices[*it]->opponentNeighborCost(1);
  551. ob.dest=*it;
  552. ob.group=-1;
  553. ob.action=0;
  554. //cout<<"\nob=="<<vertices[ob.dest]->getName()<<","<<ob.cost<<endl;
  555. pq1.push(ob);
  556. }
  557. pd=pdCandidates(-1);
  558. for (std::list<int>::iterator it=pd.begin(); it!=pd.end(); ++it)
  559. {
  560. ob.cost=es+vertices[*it]->cost;
  561. ob.dest=*it;
  562. ob.group=-1;
  563. ob.action=1;
  564. //cout<<"\nob=="<<vertices[ob.dest]->getName()<<","<<ob.cost<<endl;
  565. pq1.push(ob);
  566. }
  567. //cout<<"\n And the Winner is \n";
  568. temp=pq1.top();
  569. // pq1.pop();
  570. //cout<<"Dest1 -->"<<temp.dest<<",action -->"<<temp.cost<<"\t"<<pq1.top().cost;
  571. vertices[temp.dest]->group=-1;
  572. if(temp.action == 0)
  573. {
  574. vertices[temp.dest]->captureOpponent();
  575. }
  576. print_path(temp.dest,temp.action,temp.group);
  577. if(gameend())
  578. break;
  579. }
  580. }
  581. obj MaxValue(int vertex,int actionl)
  582. {
  583. obj temp;
  584. //print_minmaxlog(vertex,value,actionl);
  585. std::list<int> fm;
  586. std::list<int> pd;
  587. int commitgroup[300];
  588. static int print_flag=2;
  589. this->depth_terminal++;
  590. //Save State
  591. for(int i=0;i<vertices.size();i++)
  592. {
  593. commitgroup[i]=vertices[i]->group;
  594. }
  595. vertices[vertex]->group=-1;
  596. if(actionl ==0) // Force March? Capture neihbors
  597. {
  598. vertices[vertex]->captureOpponent();
  599. }
  600. //cout<<"depth_term "<<this->depth_terminal<<"\tterminal "<<terminal<<endl;
  601. if((this->depth_terminal== terminal) || (gameend() ))
  602. {
  603. temp.cost=Eval(1);
  604. //cout<<"\ntemp,cost "<<temp.cost<<endl;
  605. temp.dest=vertex;
  606. temp.action=actionl;
  607. //resetState();
  608. for(int i=0;i<vertices.size();i++)
  609. {
  610. vertices[i]->group=commitgroup[i];
  611. }
  612. //print_path(temp.dest,temp.action,temp.group);
  613. print_minmaxlog(vertex,temp.cost,actionl,-1,this->depth_terminal);
  614. this->depth_terminal--;
  615. return temp;
  616. }
  617. //Apply the Action//Make Conf Moves
  618. // saveState();
  619. float value=-65535;
  620. fm=fmpdCandidates(1);
  621. pd=pdCandidates(1);
  622. print_flag=2;
  623. //print_minmaxlog(vertex,value,actionl);
  624. for (std::list<int>::iterator it=fm.begin(); it!=fm.end(); ++it)
  625. {
  626. if(print_flag !=1)
  627. print_minmaxlog(vertex,value,actionl,-1,this->depth_terminal);
  628. print_flag++;
  629. value=max(value,MinValue(*it,0).cost);
  630. print_minmaxlog(vertex,value,actionl,-1,this->depth_terminal);
  631. print_flag=1;
  632. }
  633. for (std::list<int>::iterator it=pd.begin(); it!=pd.end(); ++it)
  634. {
  635. if(print_flag !=1)
  636. print_minmaxlog(vertex,value,actionl,-1,this->depth_terminal);
  637. print_flag++;
  638. value=max(value,MinValue(*it,1).cost);
  639. print_minmaxlog(vertex,value,actionl,-1,this->depth_terminal);
  640. print_flag=1;
  641. }
  642. //resetState();
  643. for(int i=0;i<vertices.size();i++)
  644. {
  645. vertices[i]->group=commitgroup[i];
  646. }
  647. temp.cost=value;
  648. this->depth_terminal--;
  649. temp.dest=vertex;
  650. temp.action=actionl;
  651. //print_path(temp.dest,temp.action,temp.group);
  652. //print_minmaxlog(vertex,value,actionl);
  653. return temp;
  654. }
  655. obj Pruned_MaxValue(int vertex,int actionl,float alpha=-65535,float beta=65535)
  656. {
  657. obj temp;
  658. std::list<int> fm;
  659. std::list<int> pd;
  660. int commitgroup[300];
  661. static int print_flag=2;
  662. this->depth_terminal++;
  663. //Save State
  664. for(int i=0;i<vertices.size();i++)
  665. {
  666. commitgroup[i]=vertices[i]->group;
  667. }
  668. vertices[vertex]->group=-1;
  669. if(actionl ==0) // Force March? Capture neihbors
  670. {
  671. vertices[vertex]->captureOpponent();
  672. }
  673. //cout<<"depth_term "<<this->depth_terminal<<"\tterminal "<<terminal<<endl;
  674. if((this->depth_terminal== terminal) || (gameend() ))
  675. {
  676. temp.cost=Eval(1);
  677. temp.dest=vertex;
  678. temp.action=actionl;
  679. temp.alpha=alpha;
  680. temp.beta=beta;
  681. //resetState();
  682. for(int i=0;i<vertices.size();i++)
  683. {
  684. vertices[i]->group=commitgroup[i];
  685. }
  686. //print_path(temp.dest,temp.action,temp.group);
  687. print_prunedlog(temp.dest,temp.cost,actionl,-1,this->depth_terminal,alpha,beta);
  688. this->depth_terminal--;
  689. return temp;
  690. }
  691. //Apply the Action//Make Conf Moves
  692. // saveState();
  693. float value=-65535;
  694. fm=fmpdCandidates(1);
  695. pd=pdCandidates(1);
  696. print_flag=2;
  697. for (std::list<int>::iterator it=fm.begin(); it!=fm.end(); ++it)
  698. {
  699. if(print_flag != 1)
  700. print_prunedlog(vertex,value,actionl,-1,this->depth_terminal,alpha,beta);
  701. print_flag++;
  702. value=max(value,Pruned_MinValue(*it,0,alpha,beta).cost);
  703. alpha=max(alpha,value);
  704. temp.alpha=alpha;
  705. temp.beta=beta;
  706. if(value >= beta)
  707. {
  708. temp.cost=value;
  709. temp.dest=vertex;
  710. temp.action=actionl;
  711. for(int i=0;i<vertices.size();i++)
  712. {
  713. vertices[i]->group=commitgroup[i];
  714. }
  715. cout<<"\nCut-off";
  716. print_prunedlog(temp.dest,temp.cost,actionl,-1,this->depth_terminal,alpha,beta,1);
  717. this->depth_terminal--;
  718. temp.alpha=alpha;
  719. temp.beta=beta;
  720. return temp;
  721. }
  722. // alpha=max(alpha,value);
  723. // temp.alpha=alpha;
  724. // temp.beta=beta;
  725. print_prunedlog(vertex,value,actionl,-1,this->depth_terminal,alpha,beta);
  726. print_flag=1;
  727. }
  728. for (std::list<int>::iterator it=pd.begin(); it!=pd.end(); ++it)
  729. {
  730. if(print_flag !=1)
  731. print_prunedlog(vertex,value,actionl,-1,this->depth_terminal,alpha,beta);
  732. print_flag++;
  733. value=max(value,Pruned_MinValue(*it,1,alpha,beta).cost);
  734. alpha=max(alpha,value);
  735. temp.alpha=alpha;
  736. temp.beta=beta;
  737. if(value >= beta)
  738. {
  739. temp.cost=value;
  740. temp.dest=vertex;
  741. print_prunedlog(temp.dest,temp.cost,actionl,-1,this->depth_terminal,alpha,beta,1);
  742. this->depth_terminal--;
  743. temp.action=actionl;
  744. for(int i=0;i<vertices.size();i++)
  745. {
  746. vertices[i]->group=commitgroup[i];
  747. }
  748. temp.alpha=alpha;
  749. temp.beta=beta;
  750. return temp;
  751. }
  752. // alpha=max(alpha,value);
  753. // temp.alpha=alpha;
  754. // temp.beta=beta;
  755. print_prunedlog(vertex,value,actionl,-1,this->depth_terminal,alpha,beta);
  756. print_flag=1;
  757. }
  758. //resetState();
  759. for(int i=0;i<vertices.size();i++)
  760. {
  761. vertices[i]->group=commitgroup[i];
  762. }
  763. temp.cost=value;
  764. this->depth_terminal--;
  765. temp.dest=vertex;
  766. temp.action=actionl;
  767. temp.alpha=alpha;
  768. temp.beta=beta;
  769. //print_path(temp.dest,temp.action,temp.group);
  770. return temp;
  771. }
  772. obj MinValue(int vertex,int actionl)
  773. {
  774. obj temp;
  775. std::list<int> fm;
  776. std::list<int> pd;
  777. int commitgroup[300];
  778. static int print_flag=2;
  779. this->depth_terminal++;
  780. //Save State
  781. for(int i=0;i<vertices.size();i++)
  782. {
  783. commitgroup[i]=vertices[i]->group;
  784. }
  785. vertices[vertex]->group=1;
  786. if(actionl ==0) // Force March? Capture neihbors
  787. {
  788. vertices[vertex]->captureOpponent();
  789. }
  790. //cout<<"depth_term "<<this->depth_terminal<<"\tterminal "<<terminal<<endl;
  791. if((this->depth_terminal== terminal) || (gameend() ))
  792. {
  793. temp.cost=Eval(1);
  794. //cout<<"\ntemp.cost "<<temp.cost<<endl;
  795. //this->depth_terminal--;
  796. //resetState();
  797. for(int i=0;i<vertices.size();i++)
  798. {
  799. vertices[i]->group=commitgroup[i];
  800. }
  801. temp.dest=vertex;
  802. temp.action=actionl;
  803. //print_path(temp.dest,temp.action,temp.group);
  804. print_minmaxlog(vertex,temp.cost,actionl,1,this->depth_terminal);
  805. this->depth_terminal--;
  806. return temp;
  807. }
  808. //Apply the Action//Make Conf Moves
  809. // saveState();
  810. float value=65535;
  811. //print_minmaxlog(vertex,value,actionl);
  812. fm=fmpdCandidates(-1);
  813. pd=pdCandidates(-1);
  814. print_flag=2;
  815. for (std::list<int>::iterator it=fm.begin(); it!=fm.end(); ++it)
  816. {
  817. if(print_flag != 1)
  818. print_minmaxlog(vertex,value,actionl,1,this->depth_terminal);
  819. print_flag++;
  820. value=min(value,MaxValue(*it,0).cost);
  821. print_minmaxlog(vertex,value,actionl,1,this->depth_terminal);
  822. print_flag=1;
  823. }
  824. for (std::list<int>::iterator it=pd.begin(); it!=pd.end(); ++it)
  825. {
  826. if(print_flag != 1)
  827. print_minmaxlog(vertex,value,actionl,1,this->depth_terminal);
  828. print_flag++;
  829. //cout<<"\nvalue "<<value<<endl;
  830. value=min(value,MaxValue(*it,1).cost);
  831. //cout<<"\nvalue "<<value<<endl;
  832. print_minmaxlog(vertex,value,actionl,1,this->depth_terminal);
  833. print_flag=1;
  834. }
  835. //resetState();
  836. for(int i=0;i<vertices.size();i++)
  837. {
  838. vertices[i]->group=commitgroup[i];
  839. }
  840. temp.cost=value;
  841. this->depth_terminal--;
  842. temp.dest=vertex;
  843. temp.action=actionl;
  844. //print_path(temp.dest,temp.action,temp.group);
  845. // print_minmaxlog(vertex,value,actionl);
  846. return temp;
  847. }
  848. obj Pruned_MinValue(int vertex,int actionl,float alpha,float beta)
  849. {
  850. obj temp;
  851. std::list<int> fm;
  852. std::list<int> pd;
  853. int commitgroup[300];
  854. static int print_flag=2;
  855. this->depth_terminal++;
  856. //Save State
  857. for(int i=0;i<vertices.size();i++)
  858. {
  859. commitgroup[i]=vertices[i]->group;
  860. }
  861. vertices[vertex]->group=1;
  862. if(actionl ==0) // Force March? Capture neihbors
  863. {
  864. vertices[vertex]->captureOpponent();
  865. }
  866. //cout<<"depth_term "<<this->depth_terminal<<"\tterminal "<<terminal<<endl;
  867. if((this->depth_terminal== terminal) || (gameend() ))
  868. {
  869. temp.cost=Eval(1);
  870. print_prunedlog(vertex,temp.cost,actionl,1,this->depth_terminal,alpha,beta);
  871. //this->depth_terminal--;
  872. //resetState();
  873. for(int i=0;i<vertices.size();i++)
  874. {
  875. vertices[i]->group=commitgroup[i];
  876. }
  877. temp.dest=vertex;
  878. temp.action=actionl;
  879. if((this->alpha_stat<temp.cost) && (this->depth_terminal == 1))
  880. {
  881. this->alpha_stat=temp.cost;
  882. }
  883. this->depth_terminal--;
  884. temp.alpha=alpha;
  885. temp.beta=beta;
  886. //print_path(temp.dest,temp.action,temp.group);
  887. return temp;
  888. }
  889. //Apply the Action//Make Conf Moves
  890. // saveState();
  891. float value=65535;
  892. fm=fmpdCandidates(-1);
  893. pd=pdCandidates(-1);
  894. print_flag=2;
  895. for (std::list<int>::iterator it=fm.begin(); it!=fm.end(); ++it)
  896. {
  897. if(print_flag !=1)
  898. print_prunedlog(vertex,value,actionl,1,this->depth_terminal,alpha,beta);
  899. print_flag++;
  900. value=min(value,Pruned_MaxValue(*it,0,alpha,beta).cost);
  901. beta=min(beta,value);
  902. temp.alpha=alpha;
  903. temp.beta=beta;
  904. if(value<=alpha)
  905. {
  906. for(int i=0;i<vertices.size();i++)
  907. {
  908. vertices[i]->group=commitgroup[i];
  909. }
  910. if((this->alpha_stat<value)&&((this->depth_terminal == 1)))
  911. {
  912. // this->alpha_stat=value;
  913. }
  914. temp.cost=value;
  915. print_prunedlog(vertex,value,actionl,1,this->depth_terminal,alpha,beta,1);
  916. this->depth_terminal--;
  917. temp.dest=vertex;
  918. temp.action=actionl;
  919. return temp;
  920. }
  921. // beta=min(beta,value);
  922. if((this->alpha_stat<value) &&(this->depth_terminal == 1))
  923. {
  924. this->alpha_stat=value;
  925. }
  926. print_prunedlog(vertex,value,actionl,1,this->depth_terminal,alpha,beta);
  927. print_flag=1;
  928. }
  929. for (std::list<int>::iterator it=pd.begin(); it!=pd.end(); ++it)
  930. {
  931. if(print_flag !=1)
  932. print_prunedlog(vertex,value,actionl,1,this->depth_terminal,alpha,beta);
  933. print_flag++;
  934. value=min(value,Pruned_MaxValue(*it,1,alpha,beta).cost);
  935. beta=min(beta,value);
  936. temp.alpha=alpha;
  937. temp.beta=beta;
  938. if(value<=alpha)
  939. {
  940. for(int i=0;i<vertices.size();i++)
  941. {
  942. vertices[i]->group=commitgroup[i];
  943. }
  944. if((this->alpha_stat<value)&&(this->depth_terminal == 1))
  945. {
  946. //this->alpha_stat=value;
  947. }
  948. temp.cost=value;
  949. print_prunedlog(vertex,value,actionl,1,this->depth_terminal,alpha,beta,1);
  950. this->depth_terminal--;
  951. temp.dest=vertex;
  952. temp.action=actionl;
  953. temp.alpha=alpha;
  954. temp.beta=beta;
  955. return temp;
  956. }
  957. // beta=min(beta,value);
  958. if((this->alpha_stat<value)&&(this->depth_terminal == 1))
  959. {
  960. this->alpha_stat=value;
  961. }
  962. print_prunedlog(vertex,value,actionl,1,this->depth_terminal,alpha,beta);
  963. print_flag=1;
  964. }
  965. //resetState();
  966. for(int i=0;i<vertices.size();i++)
  967. {
  968. vertices[i]->group=commitgroup[i];
  969. }
  970. temp.cost=value;
  971. this->depth_terminal--;
  972. temp.dest=vertex;
  973. temp.action=actionl;
  974. temp.alpha=alpha;
  975. temp.beta=beta;
  976. //print_path(temp.dest,temp.action,temp.group);
  977. return temp;
  978. }
  979. //Returns obj with Vertex Number
  980. obj greedy_move()
  981. {
  982. std::list<int> fm;
  983. std::list<int> pd;
  984. obj ob;
  985. obj temp;
  986. priority_queue<obj ,std::vector<obj >,CompareNode > pq1;
  987. float es=Eval(-1);
  988. //cout<<"\nConf es"<<es;
  989. fm=fmpdCandidates(-1);
  990. for (std::list<int>::iterator it=fm.begin(); it!=fm.end(); ++it)
  991. {
  992. ob.cost=es+vertices[*it]->opponentNeighborCost(1)+vertices[*it]->cost+vertices[*it]->opponentNeighborCost(1);
  993. ob.dest=*it;
  994. ob.group=-1;
  995. ob.action=0;
  996. //cout<<"\nob=="<<vertices[ob.dest]->getName()<<","<<ob.cost<<endl;
  997. pq1.push(ob);
  998. }
  999. pd=pdCandidates(-1);
  1000. for (std::list<int>::iterator it=pd.begin(); it!=pd.end(); ++it)
  1001. {
  1002. ob.cost=es+vertices[*it]->cost;
  1003. ob.dest=*it;
  1004. ob.group=-1;
  1005. ob.action=1;
  1006. //cout<<"\nob=="<<vertices[ob.dest]->getName()<<","<<ob.cost<<endl;
  1007. pq1.push(ob);
  1008. }
  1009. //cout<<"\n And the Winner is \n";
  1010. temp=pq1.top();
  1011. // pq1.pop();
  1012. //cout<<"Dest1 -->"<<temp.dest<<",action -->"<<temp.cost<<"\t"<<pq1.top().cost;
  1013. vertices[temp.dest]->group=-1;
  1014. if(temp.action == 0)
  1015. {
  1016. vertices[temp.dest]->captureOpponent();
  1017. }
  1018. return temp;
  1019. }
  1020. //Return obj with Vertex Number
  1021. obj Minimax_move()
  1022. {
  1023. //For all candidates of union return Max(MinValue())
  1024. std::list<int> fm;
  1025. std::list<int> pd;
  1026. obj max;
  1027. float cost=-65535;
  1028. static int print_flag=2;
  1029. std::list<obj> templist;
  1030. obj temp;
  1031. temp.group=1;
  1032. temp.cost=-65535;
  1033. max.cost=-65535;
  1034. fm=fmpdCandidates(1);
  1035. print_flag=2;
  1036. for (std::list<int>::iterator it=fm.begin(); it!=fm.end(); ++it)
  1037. {
  1038. this->depth_terminal=0;
  1039. if(print_flag != 1)
  1040. print_minmaxlog(-1,cost);
  1041. print_flag++;
  1042. temp=MinValue(*it,0);//pass Vertex number
  1043. temp.action=0;
  1044. templist.push_back(temp);
  1045. if(cost<temp.cost)
  1046. cost=temp.cost;
  1047. print_minmaxlog(-1,cost);
  1048. print_flag=1;
  1049. }
  1050. pd=pdCandidates(1);
  1051. for (std::list<int>::iterator it=pd.begin(); it!=pd.end(); ++it)
  1052. {
  1053. this->depth_terminal=0;
  1054. if(print_flag != 1)
  1055. print_minmaxlog(-1,cost);
  1056. print_flag++;
  1057. temp=MinValue(*it,1);//pass Vertex number
  1058. temp.action=1;
  1059. templist.push_back(temp);
  1060. if(cost<temp.cost)
  1061. cost=temp.cost;
  1062. print_minmaxlog(-1,cost);
  1063. print_flag=1;
  1064. }
  1065. //Find the Max in templist and return it
  1066. for (std::list<obj>::iterator it=templist.begin(); it!=templist.end(); ++it)
  1067. {
  1068. if(max.cost <(*it).cost)
  1069. {
  1070. //cout<<"\n*itcost "<<(*it).cost;
  1071. max.cost=(*it).cost;
  1072. max=*it;
  1073. }
  1074. }
  1075. vertices[max.dest]->group=1;
  1076. if(max.action == 0)
  1077. {
  1078. vertices[max.dest]->captureOpponent();
  1079. }
  1080. max.group=1;
  1081. global_turn++;
  1082. return max;
  1083. }
  1084. obj Pruned_Minimax_move()
  1085. {
  1086. //For all candidates of union return Max(MinValue())
  1087. std::list<int> fm;
  1088. std::list<int> pd;
  1089. obj max;
  1090. static int print_flag=2;
  1091. print_flag=2;
  1092. std::list<obj> templist;
  1093. obj temp;
  1094. float max_alpha=-65535;
  1095. temp.group=1;
  1096. max.cost=-65535;
  1097. //temp.alpha=-65535;
  1098. //temp.beta=65535;
  1099. fm=fmpdCandidates(1);
  1100. static int i=1;
  1101. i=1;
  1102. for (std::list<int>::iterator it=fm.begin(); it!=fm.end(); ++it)
  1103. {
  1104. this->depth_terminal=0;
  1105. if(i==1)
  1106. {
  1107. print_prunedlog(-1,-65535,-1,-1,0,-65535,65535);
  1108. temp=Pruned_MinValue(*it,0,-65535,65535);//pass Vertex number
  1109. i++;
  1110. }
  1111. else
  1112. {
  1113. if(max_alpha<temp.cost)
  1114. max_alpha=temp.cost;
  1115. if(print_flag != 1)
  1116. print_prunedlog(-1,max_alpha,-1,-1,0,max_alpha,65535);
  1117. print_flag++;
  1118. temp=Pruned_MinValue(*it,0,max_alpha,65535);//pass Vertex number
  1119. if(max_alpha<temp.cost)
  1120. max_alpha=temp.cost;
  1121. print_prunedlog(-1,max_alpha,-1,-1,0,max_alpha,65535);
  1122. print_flag=1;
  1123. //cout<<"\ncoss "<<temp.cost<<endl;
  1124. }
  1125. temp.action=0;
  1126. templist.push_back(temp);
  1127. }
  1128. pd=pdCandidates(1);
  1129. for (std::list<int>::iterator it=pd.begin(); it!=pd.end(); ++it)
  1130. {
  1131. this->depth_terminal=0;
  1132. if(i==1)
  1133. {
  1134. print_prunedlog(-1,max_alpha,-1,-1,0,max_alpha,65535);
  1135. temp=Pruned_MinValue(*it,1,-65535,65535);//pass Vertex number
  1136. i++;
  1137. }
  1138. else
  1139. {
  1140. if(max_alpha<temp.cost)
  1141. max_alpha=temp.cost;
  1142. if(print_flag != 1)
  1143. print_prunedlog(-1,max_alpha,-1,-1,0,max_alpha,65535);
  1144. print_flag++;
  1145. temp=Pruned_MinValue(*it,1,max_alpha,65535);//pass Vertex number
  1146. if(max_alpha<temp.cost)
  1147. max_alpha=temp.cost;
  1148. print_prunedlog(-1,max_alpha,-1,-1,0,max_alpha,65535);
  1149. print_flag=1;
  1150. }
  1151. temp.action=1;
  1152. templist.push_back(temp);
  1153. }
  1154. //Find the Max in templist and return it
  1155. for (std::list<obj>::iterator it=templist.begin(); it!=templist.end(); ++it)
  1156. {
  1157. if(max.cost <(*it).cost)
  1158. {
  1159. cout<<"\n*itcost "<<(*it).cost;
  1160. max.cost=(*it).cost;
  1161. max=*it;
  1162. }
  1163. }
  1164. vertices[max.dest]->group=1;
  1165. if(max.action == 0)
  1166. {
  1167. vertices[max.dest]->captureOpponent();
  1168. }
  1169. max.group=1;
  1170. global_turn++;
  1171. return max;
  1172. }
  1173. //Alternates between Minimax move and greedy move
  1174. void Minimax_game()
  1175. {
  1176. print_path();
  1177. while(1)
  1178. {
  1179. obj Union=Minimax_move();
  1180. //vertices[Union.dest]->group=1;
  1181. if(gameend())
  1182. break;
  1183. cout<<"\nChecking Minimax move == max"<<Union.cost<<"\n";
  1184. print_path(Union.dest,Union.action,Union.group);
  1185. obj Conf=greedy_move();
  1186. //vertices[Conf.dest]->group=-1;
  1187. print_path(Conf.dest,Conf.action,Conf.group);
  1188. if(gameend())
  1189. break;
  1190. }
  1191. }
  1192. void Pruned_game()
  1193. {
  1194. print_path();
  1195. while(1)
  1196. {
  1197. obj Union=Pruned_Minimax_move();
  1198. //vertices[Union.dest]->group=1;
  1199. if(gameend())
  1200. break;
  1201. cout<<"\nChecking Minimax move == max"<<Union.cost<<"\n";
  1202. print_path(Union.dest,Union.action,Union.group);
  1203. obj Conf=greedy_move();
  1204. //vertices[Conf.dest]->group=-1;
  1205. print_path(Conf.dest,Conf.action,Conf.group);
  1206. if(gameend())
  1207. break;
  1208. }
  1209. }
  1210. int terminal;
  1211. private:
  1212. vector<Vertex*> vertices;
  1213. int depth_terminal;
  1214. int alpha_stat;
  1215. };
  1216. main(int argc,char *argv[])
  1217. {
  1218. int task;
  1219. int depth;
  1220. char mapname[50];
  1221. char initmap[50];
  1222. char outputpath[100];
  1223. char outputlog[100];
  1224. if(argc!=13)
  1225. {
  1226. cout<<"Please enter valid Arguments\nUsage :\n";
  1227. cout<<"war -t <task> -d < cut_off_depth> -m<map_file> -i <init > -op <output_path> -ol <output_log>"<<endl;
  1228. exit(1);
  1229. }
  1230. else
  1231. {
  1232. task=atoi(argv[2]);
  1233. depth=atoi(argv[4]);
  1234. strcpy(mapname,argv[6]);
  1235. strcpy(initmap,argv[8]);
  1236. strcpy(outputpath,argv[10]);
  1237. strcpy(outputlog,argv[12]);
  1238. }
  1239. char source[300],dest[300],cost[300],group[300],node[300];
  1240. string line;
  1241. int edge_count=0,node_count=0;
  1242. int i=0;
  1243. int j=0;
  1244. int k=0,k1=0;
  1245. char *source_arr[100];
  1246. char *dest_arr[100];
  1247. char *nodes[100];
  1248. float cost_arr[100];
  1249. int group_arr[100];
  1250. //char *ranker[100];
  1251. map<string,bool> check;
  1252. ifstream myfile (mapname);
  1253. if (myfile.is_open())
  1254. {
  1255. while ( getline (myfile,line) )
  1256. {
  1257. memset(source,0,300);
  1258. memset(dest,0,300);
  1259. i=0;
  1260. j=0;
  1261. //cout << line << "\n";
  1262. while (line[i] != ',')
  1263. {
  1264. source[j]=line[i];
  1265. i++;
  1266. j++;
  1267. }
  1268. i++;
  1269. j=0;
  1270. while(line[i]>= 'A'&&line[i]<='z')
  1271. {
  1272. dest[j]=line[i];
  1273. i++;
  1274. j++;
  1275. }
  1276. //nncout<<"\n"<<dest<<","<<j<<endl;
  1277. source_arr[k]=new char[300];
  1278. dest_arr[k]=new char[300];
  1279. strcpy(source_arr[k],source);
  1280. strcpy(dest_arr[k],dest);
  1281. k++;
  1282. }
  1283. myfile.close();
  1284. }
  1285. edge_count=k;
  1286. k=0;
  1287. for(int temp=0;temp<edge_count;temp++)
  1288. {
  1289. //cout<<source_arr[temp]<<"-->"<<dest_arr[temp]<<endl;
  1290. }
  1291. ifstream myfile1(initmap);
  1292. if (myfile1.is_open())
  1293. {
  1294. while ( getline (myfile1,line) )
  1295. {
  1296. memset(node,0,300);
  1297. memset(cost,0,300);
  1298. memset(group,0,300);
  1299. i=0;
  1300. j=0;
  1301. //cout << line << "\n";
  1302. while (line[i] != ',')
  1303. {
  1304. node[j]=line[i];
  1305. i++;
  1306. j++;
  1307. }
  1308. i++;
  1309. j=0;
  1310. while (line[i] != ',')
  1311. {
  1312. cost[j]=line[i];
  1313. i++;
  1314. j++;
  1315. }
  1316. i++;
  1317. j=0;
  1318. while (line[i] !='\0')
  1319. {
  1320. group[j]=line[i];
  1321. i++;
  1322. j++;
  1323. }
  1324. nodes[k]=new char[300];
  1325. strcpy(nodes[k],node);
  1326. cost_arr[k]=atof(cost);
  1327. group_arr[k]=atoi(group);
  1328. k++;
  1329. }
  1330. myfile1.close();
  1331. }
  1332. node_count=k;
  1333. for(int temp1=0;temp1<node_count;temp1++)
  1334. {
  1335. //cout<<nodes[temp1]<<","<<cost_arr[temp1]<<","<<group_arr[temp1]<<endl;
  1336. }
  1337. Vertex *v[100];
  1338. int source_ind=0,dest_ind=0;
  1339. for(int cur=0;cur<node_count;cur++)
  1340. {
  1341. v[cur]=new Vertex(nodes[cur],cost_arr[cur],group_arr[cur]);
  1342. cout<<"Vertex"<<v[cur]->name<<","<<v[cur]->group<<","<<v[cur]->cost<<endl;
  1343. }
  1344. for(int cur=0;cur<node_count;cur++)
  1345. {
  1346. for(int cur1=0;cur1<edge_count;cur1++)
  1347. {
  1348. if(v[cur]->name==source_arr[cur1])
  1349. {
  1350. for(int i = 0; i < node_count; i++)
  1351. {
  1352. //cout<<"\n1"<<v[i]->name<<","<<dest_arr[cur1]<<endl;
  1353. if(v[i]->name ==dest_arr[cur1])
  1354. {
  1355. // cout<<"\n2";
  1356. v[cur]->addEdge(v[i]);
  1357. v[i]->addEdge(v[cur]);
  1358. }
  1359. }
  1360. }
  1361. }
  1362. }
  1363. opf=fopen(outputpath,"w");
  1364. opl=fopen(outputlog,"w");
  1365. Graph g1;
  1366. g1.terminal=depth;
  1367. for(int cur=0;cur<node_count;cur++)
  1368. {
  1369. g1.insert(v[cur]);
  1370. }
  1371. g1.printGraph();
  1372. /* std::list<int> fmpd=g1.fmpdCandidates(-1);
  1373. std::cout << "fmlist contains:";
  1374. for (std::list<int>::iterator it=fmpd.begin(); it!=fmpd.end(); ++it)
  1375. std::cout << ' ' << *it;
  1376. std::cout << '\n';
  1377. std::list<int> pd=g1.pdCandidates(-1);
  1378. std::cout << "pdlist contains:";
  1379. for (std::list<int>::iterator it=pd.begin(); it!=pd.end(); ++it)
  1380. std::cout << ' ' << *it;*/
  1381. std::cout << '\n';
  1382. if(task ==1)
  1383. {
  1384. g1.greedy();
  1385. }
  1386. else if(task ==3)
  1387. {
  1388. g1.Pruned_game();
  1389. }
  1390. else if(task == 2)
  1391. {
  1392. g1.Minimax_game();
  1393. }
  1394. }