User Tools

Site Tools


brain:brain.cpp

brain.cpp

#include <iostream>
#include <chrono>       // std::chrono::system_clock
#include <string>
#include <regex>
#include "net.h"
#include "verylong.h"
 
#include "string.h"
 
int main()
{
  std::vector<std::string> result;
 
  //std::string regex = "[\\s,]+";
  //std::string regex = "([A-Za-z])*";
  //std::string regex = "([\\d]+)";
  //std::string regex = "([\\d]+).*([\\d]+)";
//  std::string regex = "([\\d]+).+([\\d]+)";
  //std::string regex = "\\b([\\d])([^ ]*)";
  std::string regex = "([A-Z]+)([\\d]+)";
 
  std::regex reg(regex);
  std::cout << "RS=" << reg.mark_count() << std::endl;
 
 
  //std::string ss = "The1White2Rabbit,333is4very,late.";
  //std::string ss = "5yz0ab1cde2fgh";
  //std::string ss = "MAY14";
  std::string ss = "aaaMAY14bbbJUNE4";
 
  result = string_find(ss, regex);
 
  for (int i = 0; i < result.size(); i++)
  {
    std::cout << "[" << i << "]=" << result[i] << "#" << std::endl;
  }
 
  //   std::string s("This is a catfish");
  //   std::string regex("(cat)");
  //   std::string r("(dog)");
  ss = "This cat is a catfish";
  regex = "(cat)";
  std::string result2;
  result2 = string_replace(ss, regex, "dog", std::regex_constants::format_first_only);
  std::cout << result2 << std::endl;
  result2 = string_replace(ss, regex, "ha ha $$1 boo");
  std::cout << result2 << std::endl;
  result2 = string_replace(ss, regex, "ha ha $$1 boo", false);
  std::cout << result2 << std::endl;
 
 
  ss = "there is a subsequence in the string\n";
  regex = "(sub)";
  result2 = string_replace(ss, regex, "xyz");
  std::cout << result2 << std::endl;
 
  ss = "April 15, 2003";
  regex = "(\\w+) (\\d+), (\\d+)";
  result2 = string_replace(ss, regex, "$011,$3");
  std::cout << result2 << std::endl;
 
  ss = "  14MAY  15JUNE ";
  std::cout << "[" << ss << "]" << std::endl;
  //regex = "([\\d]+)([A+Z]+)";
  regex = "(\\d+)([A-Z]+)";
  result2 = string_replace(ss, regex, "$1 $2");
  std::cout << "[" << result2 << "]" << std::endl;
  result2 = string_replace(ss, regex, "$1 $2", std::regex_constants::format_no_copy);
  std::cout << "[" << result2 << "]" << std::endl;
  result2 = string_replace(ss, regex, "$1 $2", false);
  std::cout << "[" << result2 << "]" << std::endl;
 
 
  /*
  std::string s3("there is a subsequence in the string\n");
  std::regex e("\\b(sub)([^ ]*)");   // matches words beginning by "sub"
 
  // using string/c-string (3) version:
  std::cout << std::regex_replace(s3, e, "sub-$2");
 
  // using range/c-string (6) version:
  std::string result2;
  std::regex_replace(std::back_inserter(result2), s3.begin(), s3.end(), e, "$2");
  std::cout << result2;
 
  // with flags:
  std::cout << std::regex_replace(s3, e, "$1 and $2", std::regex_constants::format_no_copy);
  std::cout << std::endl;
  */
 
  std::cout << "HEAD" << std::endl;
  ss = "0 HEAD";
  regex = "([\\d]+)[\\s]+([A-Z]*)$";
  //regex = "([\\d]+)([A-Z]*)";
  // regex = "[\\d]+[A-Z]*";
 
  result.clear();
  result = string_tokenize(ss, regex);
  for (int i = 0; i < result.size(); i++)
  {
    std::cout << "[" << i << "]=" << result[i] << "#" << std::endl;
  }
 
 
 
  /*
 
  show_matches("abcdef", "abc|def");
  show_matches("abc", "ab|abc"); // left Alernative matched first
  show_matches("abc", "((a)|(ab))((c)|(bc))");
  */
#ifdef _MSC_VER
  std::string s1;
  std::getline(std::cin, s1);
#endif
 
 
  //return 0;
 
 
 
 
  /*
	Verylong v1("12345678901234567890123456789");
	v1 = 2;
	std::cout << "V1=" << v1 << std::endl;
	Verylong v2(1);
	v1 += v2;
	std::cout << "V1=" << v1 << std::endl;
	v1 ^= 80;
	//v1 ^= 5;
	std::cout << "V1=" << v1 << std::endl;
 
 
	Verylong v3(0);
	v1 = 12;
	v2 = 2;
	v3 = v1 ^ v2;
	std::cout << "V3=" << v3 << std::endl;
 
	Verylong v4(0);
	v4 = v1;
	std::cout << "V4=" << v4 << std::endl;
 
	//return 0;
  */
 
  std::vector<int> myvector;
 
  // set some values (from 1 to 10)
  for (int i = 1; i <= 10; i++) myvector.push_back(i);
 
  // erase the 6th element
  myvector.erase(myvector.begin() + 5);
 
  // erase the first 3 elements:
  myvector.erase(myvector.begin(), myvector.begin() + 3);
 
  std::cout << "myvector contains:";
  for (unsigned i = 0; i<myvector.size(); ++i)
    std::cout << ' ' << myvector[i];
  std::cout << '\n';
 
  myvector.push_back(2);
 
  std::cout << "myvector now contains:";
  for (unsigned i = 0; i<myvector.size(); ++i)
    std::cout << ' ' << myvector[i];
  std::cout << '\n';
 
 
  std::cout << "****************DQN***********************" << std::endl;
 
  /*
  To learn to get out to room 5 from any other room as quick as possible.
 
        [1]---
         |    |
         |     |
         |      |
  [2]---[3]    [5]
         |      |
         |    |
         |   |
  [0]---[4]---
 
  otherwise shown as 
 
  [0]---[1]---[5]
   |     |      
   |     |
   |     |
  [4]---[3]---[2]
   |
   |
   |
  [5]
 
  */
 
 
  srand((unsigned int)time(NULL));
  std::vector<unsigned int> myDQNTopology;
  myDQNTopology.push_back(6); // 6 neurons. (in layer 0).
 
  std::cout << myDQNTopology.size() << std::endl << std::endl; // myTopology == {3, 4, 2 ,1}
 
  Net myDQN(myDQNTopology);
 
  myDQN.setGoalAmount(100);
 
  const std::vector< std::vector<double> > connections = {
    //{ 0, 0, 0, 0, 0 },
    { 0, 0, 0, 4, 0 }, // from Layer 0, Neuron 0 to Layer 0, Neuron 4 give a value of zero.
    { 0, 1, 0, 1, 0 },
    { 0, 1, 0, 3, 0 },
    { 0, 1, 0, 5, 100 },
    //{ 0, 2, 0, 2, 0 },
    { 0, 2, 0, 3, 0 },
    //{ 0, 3, 0, 3, 0 },
    { 0, 3, 0, 1, 0 },
    { 0, 3, 0, 2, 0 },
    { 0, 3, 0, 4, 0 },
    //{ 0, 4, 0, 4, 0 },
    { 0, 4, 0, 0, 0 },
    { 0, 4, 0, 3, 0 },
    { 0, 4, 0, 5, 100 },
    { 0, 5, 0, 5, 100 },
    { 0, 5, 0, 1, 0 },
    { 0, 5, 0, 4, 0 }
  };
 
 
  /*
  const std::vector< std::vector<double> > connections = {
    { 0, 0, 0, 0, 0 },
    { 0, 0, 0, 1, 0 },
    { 0, 0, 0, 3, 0 },
    { 0, 1, 0, 1, 0 },
    { 0, 1, 0, 0, 0 },
    { 0, 1, 0, 2, 100 },
    { 0, 2, 0, 2, 100 },
    { 0, 2, 0, 1, 0 },
    { 0, 2, 0, 5, 0 },
    { 0, 3, 0, 3, 0 },
    { 0, 3, 0, 0, 0 },
    { 0, 2, 0, 4, 0 },
    { 0, 4, 0, 4, 0 },
    { 0, 4, 0, 1, 0 },
    { 0, 4, 0, 3, 0 },
    { 0, 4, 0, 5, 0 },
    { 0, 5, 0, 5, 0 },
    { 0, 5, 0, 2, 100 },
    { 0, 5, 0, 4, 0 }
  };
  */
 
  myDQN.connect(connections);
 
 
  /*
  unsigned int connection_idx = 0;
 
  myDQN.connect(0, 0, 0, 0, 0, connection_idx++);
  myDQN.connect(0, 0, 0, 4, 0, connection_idx++);
  myDQN.connect(0, 1, 0, 1, 0, connection_idx++);
  myDQN.connect(0, 1, 0, 3, 0, connection_idx++);
  myDQN.connect(0, 1, 0, 5, 100, connection_idx++);
  myDQN.connect(0, 2, 0, 2, 0, connection_idx++);
  myDQN.connect(0, 2, 0, 3, 0, connection_idx++);
  myDQN.connect(0, 3, 0, 3, 0, connection_idx++);
  myDQN.connect(0, 3, 0, 1, 0, connection_idx++);
  myDQN.connect(0, 3, 0, 2, 0, connection_idx++);
  myDQN.connect(0, 3, 0, 4, 0, connection_idx++);
  myDQN.connect(0, 4, 0, 4, 0, connection_idx++);
  myDQN.connect(0, 4, 0, 0, 0, connection_idx++);
  myDQN.connect(0, 4, 0, 3, 0, connection_idx++);
  myDQN.connect(0, 4, 0, 5, 100, connection_idx++);
  myDQN.connect(0, 5, 0, 5, 100, connection_idx++);
  myDQN.connect(0, 5, 0, 1, 0, connection_idx++);
  myDQN.connect(0, 5, 0, 4, 0, connection_idx++);
  */
 
  /*
  myDQN.connect(0, 0, 0, 0, 0, connection_idx++);
  myDQN.connect(0, 0, 0, 1, 0, connection_idx++);
  myDQN.connect(0, 0, 0, 3, 0, connection_idx++);
  myDQN.connect(0, 1, 0, 1, 0, connection_idx++);
  myDQN.connect(0, 1, 0, 0, 0, connection_idx++);
  myDQN.connect(0, 1, 0, 2, 100, connection_idx++);
  myDQN.connect(0, 2, 0, 2, 100, connection_idx++);
  myDQN.connect(0, 2, 0, 1, 0, connection_idx++);
  myDQN.connect(0, 2, 0, 5, 0, connection_idx++);
  myDQN.connect(0, 3, 0, 3, 0, connection_idx++);
  myDQN.connect(0, 3, 0, 0, 0, connection_idx++);
  myDQN.connect(0, 3, 0, 4, 0, connection_idx++);
  myDQN.connect(0, 4, 0, 4, 0, connection_idx++);
  myDQN.connect(0, 4, 0, 1, 0, connection_idx++);
  myDQN.connect(0, 4, 0, 3, 0, connection_idx++);
  myDQN.connect(0, 4, 0, 5, 0, connection_idx++);
  myDQN.connect(0, 5, 0, 5, 0, connection_idx++);
  myDQN.connect(0, 5, 0, 4, 0, connection_idx++);
  myDQN.connect(0, 5, 0, 2, 100, connection_idx++);
  */
 
 
  myDQN.printOutput();
 
  for (unsigned int i = 0; i<1000; ++i)
    myDQN.DQN();
 
  myDQN.printOutput();
  myDQN.printResult();
  myDQN.showPolicy();
 
#ifdef _MSC_VER
  std::string s2;
  std::getline(std::cin, s2);
#endif
 
//  return 0;
 
  std::cout << "****************BACKPROP***********************" << std::endl;
	srand((unsigned int)time(NULL));
	std::vector<unsigned int> myTopology;
	myTopology.push_back(2);
	//myTopology.push_back(3); 1 for bias.
	myTopology.push_back(4);
	//myTopology.push_back(8);
	//myTopology.push_back(2);
	myTopology.push_back(1);
 
	std::cout << myTopology.size() << std::endl << std::endl; // myTopology == {3, 4, 2 ,1}
	//Neural::Net myNet(myTopology);
	Net myNet(myTopology);
 
 
	std::cout << "****************SET VALS***********************" << std::endl;
 
  myNet.connectForward();
	//myNet.setTest();
 
	std::cout << "****************FORWARD***********************" << std::endl;
 
	/*
	for (unsigned int i = 0; i<50; ++i)
	{
		myNet.feedForward({ 1, 1 });
		myNet.backPropagate({ 0 });
	}
 
	for (unsigned int i = 0; i<50; ++i)
	{
		myNet.feedForward({ 0, 0 });
		myNet.backPropagate({ 1 });
	}
	*/
 
	for (unsigned int i = 0; i<50000; ++i)
  //for (unsigned int i = 0; i<100000; ++i)
  //for (unsigned int i = 0; i<500000; ++i)
	{
		myNet.feedForward({ 0, 0 });
		myNet.backPropagate({ 0 });
 
		myNet.feedForward({ 1, 0 });
		myNet.backPropagate({ 1 });
 
		myNet.feedForward({ 0, 1 });
		myNet.backPropagate({ 1 });
 
		myNet.feedForward({ 1, 1 });
		myNet.backPropagate({ 0 });
	}
 
	/*
	std::cout << "Feeding 0,0" << std::endl;
	myNet.feedForward({ 0, 0 });
	myNet.printOutput();
 
	std::cout << "Feeding 1,1" << std::endl;
	myNet.feedForward({ 1, 1 });
	myNet.printOutput();
	*/
 
	std::cout << "****************BACK**************************" << std::endl;
 
	//myNet.backPropagate({0});
	std::cout << "Feeding 0,0" << std::endl;
	myNet.feedForward({ 0, 0 });
	//myNet.printOutput();
	myNet.printResult();
 
	//myNet.backPropagate({1});
	std::cout << "Feeding 1,0" << std::endl;
	myNet.feedForward({ 1, 0 });
	//myNet.printOutput();
	myNet.printResult();
 
	//myNet.backPropagate({1});
	std::cout << "Feeding 1,1" << std::endl;
	myNet.feedForward({ 1, 1 });
	//myNet.printOutput();
	myNet.printResult();
 
	//myNet.backPropagate({1});
	std::cout << "Feeding 0,1" << std::endl;
	myNet.feedForward({ 0, 1 });
	//myNet.printOutput();
	myNet.printResult();
 
 
#ifdef _MSC_VER
  std::string s;
  std::getline(std::cin, s);
#endif
 
 
	return 0;
}
brain/brain.cpp.txt · Last modified: 2020/07/15 10:30 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki