Advertisement
Guest User

Untitled

a guest
May 26th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <queue>
  5. #include <fstream>
  6. using namespace std;
  7. /*
  8.  ifstream fin("in.ann");
  9.  ofstream fout("out.ann");
  10.  */
  11.  
  12.  
  13. class Neuron {
  14. private:
  15.     int battery; // Маша: аккумулятор
  16.     int condition; // Маша: состояние
  17.     int number; // Маша: номер нейрона
  18.    
  19. public:
  20.     Neuron(): battery(0), condition(0) {};
  21.     Neuron(int value, int number): battery(value), number(number), condition(0) {};
  22.    
  23.     Neuron activate_function(Neuron a);
  24.     Neuron binary_function(Neuron a);
  25.    
  26.     int get_condition(){ return condition; }
  27.     int get_battery(){ return battery; }
  28.     int get_number(){ return number; }
  29.    
  30.     void add_battety(int a){ battery += a; }
  31. };
  32.  
  33. Neuron Neuron::activate_function(Neuron a){
  34.     a.condition = a.battery;
  35.     return a;
  36. }
  37.  
  38. Neuron Neuron::binary_function(Neuron a){
  39.     if (a.battery > 50){
  40.         a.condition = 100;
  41.     }
  42.     else {
  43.         a.condition = 0;
  44.     }
  45.     return a;
  46. }
  47.  
  48.  
  49. class Synapse {
  50. private:
  51.     int donor;
  52.     int acceptor;
  53.     int weight_edge;
  54. public:
  55.     Synapse() : donor(0), acceptor(0), weight_edge(0) {};
  56.     Synapse(int donor, int acceptor, int weight_edge): donor(donor), acceptor(acceptor), weight_edge(weight_edge) {};
  57.    
  58.     int get_donor(){ return donor; }
  59.     int get_acceptor(){ return acceptor; }
  60.     int get_weight(){ return weight_edge; }
  61.    
  62.     friend istream &operator >> (istream &in, Synapse& a){
  63.         in >> a.donor >> a.acceptor >> a.weight_edge;
  64.         return in;
  65.     }
  66.     friend ostream &operator << (ostream &out, Synapse a){
  67.         out << a.donor << ' ' << a.acceptor << ' ' << a.weight_edge << '\n';
  68.         return out;
  69.     }
  70. };
  71.  
  72.  
  73. class Net {
  74. public:
  75.     Neuron neurons[100];
  76.     vector<Synapse> synapses; // Маша: Список ребер
  77.    
  78.     Net(string path){
  79.         ifstream in;
  80.         in.open(path.c_str());
  81.         while (!in.eof()){
  82.             Synapse add;
  83.             in >> add;
  84.             synapses.push_back(add);
  85.         }
  86.     }
  87.    
  88.     void run(vector<Neuron> first){
  89.         queue <Neuron> que; // очередь
  90.        
  91.         for (Neuron n : first){
  92.             que.push(n);
  93.         }
  94.         Neuron start = que.front();
  95.         Neuron vertex = que.front(); que.pop();
  96.         neurons[start.get_number()] = start;
  97.  
  98.         for (vertex; que.size() > 0; vertex = que.front(), que.pop()){
  99.             for (Synapse syn : synapses){
  100.                 if (syn.get_donor() == vertex.get_number()){
  101.                     Neuron* next = &neurons[syn.get_acceptor()];
  102.                     next->add_battety((vertex.get_condition() * syn.get_weight()) / 100);
  103.                     que.push(*next);
  104.                 }
  105.             }
  106.         }
  107.     }
  108.    
  109.     void add(Synapse newSyn){ synapses.push_back(newSyn); }
  110.    
  111.     void remove(Synapse delsyn){
  112.         for (int i = 0; i < synapses.size(); i++){
  113.             if (delsyn.get_donor() == synapses[i].get_donor() && delsyn.get_acceptor() == synapses[i].get_acceptor()
  114.                 && delsyn.get_weight() == synapses[i].get_weight()){ // Егор: почему не if(delsyn == synapses[i]) ?
  115.                 synapses.erase(synapses.begin() + i);
  116.             }
  117.         }
  118.     }
  119.  
  120.     // Егор: Проверка сети на связность после изменения
  121.     /*bool test(){
  122.         bool good_donor;
  123.         bool good_acceptor;
  124.         for (int i = 0; i < 100; i++){
  125.             good_donor = false;
  126.             good_acceptor = false;
  127.             if (neurons[i].get_battery() > 0){
  128.                 for (int j = 0; j < synapses.size(); j++){
  129.                     if (synapses[i].get_donor() == neurons[i].get_number()){
  130.                         good_donor = true;
  131.                     }
  132.                     if (synapses[i].get_acceptor() == neurons[i].get_number()){
  133.                         good_acceptor = true;
  134.                     }
  135.                 }
  136.                 if (!good_donor || !good_acceptor){
  137.                     return false;
  138.                 }
  139.             }
  140.            
  141.         }
  142.         return true;
  143.     }*/
  144.     bool test(){
  145.         bool isDonor[100], isAcceptor[100];
  146.        
  147.         for (Synapse syn : synapses){
  148.             if (syn.get_acceptor() <= syn.get_donor())
  149.                 return false;
  150.            
  151.             isDonor[syn.get_donor()] = 1;
  152.             isAcceptor[syn.get_acceptor()] = 1;
  153.         }
  154.        
  155.         bool foundInput=0, foundOutput=0;
  156.         for (int i=10; i<20; i++){
  157.             if (isDonor[i]) foundInput = 1;
  158.         }
  159.         for (int i=80; i<90; i++){
  160.             if (isAcceptor[i]) foundOutput = 1;
  161.         }
  162.         for (int i=20; i<80; i++){
  163.             if (isDonor[i] ^ isAcceptor[i])
  164.                 return false;
  165.         }
  166.        
  167.         return foundInput && foundOutput;
  168.     }
  169.    
  170. };
  171.  
  172. string analyzer(string path);
  173. // Егор: path - адрес входного файла
  174. // Егор: Функция обрабатывает его и записывает в файл .ann, возвращает имя файла
  175.  
  176.  
  177. class NeuroBot {
  178.     /* Егор: Ядро (модуль). Хранит в себе параметры объектов,
  179.      * Егор: реализует игровую логику и общение c пользователем.
  180.      * Егор: Ядро хранит мозги объектов и реализует обработку нейросетей.
  181.      */
  182. };
  183.  
  184. int main(){
  185.     Synapse a;
  186.     cin >> a;
  187.     while (cin >> a){
  188.         cout << a;
  189.         cin >> a; // Маша: Проверка работы перегруженных операторов
  190.     }
  191.     return 0;
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement