Преглед на файлове

Try to reanimate remote control
Migrate to new version of qtprotobuf

Alexey Edelev преди 5 години
родител
ревизия
1523b96667

+ 2 - 1
gui/main.cpp

@@ -27,6 +27,7 @@
 #include <QQmlApplicationEngine>
 #include <QQmlContext>
 #include <QDebug>
+#include <QtProtobufTypes>
 
 #include "remotecontrolclient.h"
 #include "qgrpchttp2channel.h"
@@ -44,7 +45,7 @@ public:
 int main(int argc, char *argv[])
 {
     QGuiApplication app(argc, argv);
-
+    QtProtobuf::registerProtoTypes();
     qmlRegisterUncreatableType<ValueIndicator>("NeuralNetworkUi", 0, 1, "ValueIndicator", "");
     qmlRegisterUncreatableType<LayerTrigger>("NeuralNetworkUi", 0, 1, "LayerTrigger", "");
     std::shared_ptr<remotecontrol::RemoteControlClient> client(new remotecontrol::RemoteControlClient);

+ 1 - 1
gui/qtprotobuf

@@ -1 +1 @@
-Subproject commit e0c89b7ece18445f56378d248e7f3e04b02879c9
+Subproject commit 1990f3caccc38fd62c3da39dfa09cd095b7ab103

+ 3 - 0
gui/visualizermodel.cpp

@@ -38,6 +38,9 @@ VisualizerModel::VisualizerModel(std::shared_ptr<RemoteControlClient> &client, Q
   , m_client(client)
   , m_networkState(new NetworkState{NetworkState::None})
 {
+    qRegisterProtobufType<remotecontrol::LayerMatrix>();
+    qRegisterProtobufType<remotecontrol::NetworkState>();
+
     m_client->getConfiguration({}, this, [this](QGrpcAsyncReply *reply) {
         qDeleteAll(m_layers);
         m_networkConfig = reply->read<Configuration>();

+ 0 - 6
neuralnetwork/main.go

@@ -3,19 +3,13 @@ package main
 import (
 	genetic "./genetic"
 	mutagen "./genetic/mutagen"
-	remotecontrol "./remotecontrol"
 	snakesimulator "./snakesimulator"
 )
 
 func main() {
-	rc := &remotecontrol.RemoteControl{}
-	go rc.Run()
 	s := snakesimulator.NewSnakeSimulator(400)
 	s.StartServer()
 	p := genetic.NewPopulation(s, mutagen.NewDummyMutagen(1.0, 1), genetic.PopulationConfig{PopulationSize: 2000, SelectionSize: 0.01, CrossbreedPart: 0.5}, []int{24, 18, 18, 4})
-	for _, net := range p.Networks {
-		net.SetStateWatcher(rc)
-	}
 	p.NaturalSelection(5000)
 	// s.Run()
 

+ 1 - 0
neuralnetwork/neuralnetwork/neuralnetwork.go

@@ -154,6 +154,7 @@ func (nn *NeuralNetwork) Copy() (outNN *NeuralNetwork) {
 		BGradient:                  make([]interface{}, nn.LayerCount),
 		WGradient:                  make([]interface{}, nn.LayerCount),
 		gradientDescentInitializer: nn.gradientDescentInitializer,
+		watcher:                    nn.watcher,
 	}
 	for l := 1; l < outNN.LayerCount; l++ {
 		outNN.Biases[l] = mat.DenseCopyOf(nn.Biases[l])

+ 9 - 6
neuralnetwork/remotecontrol/remotecontrol.go

@@ -51,6 +51,7 @@ type RemoteControl struct {
 	weightsQueue     chan *LayerMatrix
 	stateQueue       chan int
 	mutex            sync.Mutex
+	config           *Configuration
 }
 
 func (rw *RemoteControl) Init(nn *neuralnetwork.NeuralNetwork) {
@@ -59,6 +60,10 @@ func (rw *RemoteControl) Init(nn *neuralnetwork.NeuralNetwork) {
 	rw.biasesQueue = make(chan *LayerMatrix, 5)
 	rw.weightsQueue = make(chan *LayerMatrix, 5)
 	rw.stateQueue = make(chan int, 2)
+	rw.config = &Configuration{}
+	for _, size := range rw.nn.Sizes {
+		rw.config.Sizes = append(rw.config.Sizes, int32(size))
+	}
 }
 
 func (rw *RemoteControl) UpdateActivations(l int, a *mat.Dense) {
@@ -110,12 +115,7 @@ func NewLayerMatrix(l int, dense *mat.Dense, contentType LayerMatrix_ContentType
 }
 
 func (rw *RemoteControl) GetConfiguration(context.Context, *None) (*Configuration, error) {
-	config := &Configuration{}
-
-	for _, size := range rw.nn.Sizes {
-		config.Sizes = append(config.Sizes, int32(size))
-	}
-	return config, nil
+	return rw.config, nil
 }
 
 func (rw *RemoteControl) Activations(_ *None, srv RemoteControl_ActivationsServer) error {
@@ -127,6 +127,7 @@ func (rw *RemoteControl) Activations(_ *None, srv RemoteControl_ActivationsServe
 		default:
 		}
 		msg := <-rw.activationsQueue
+		fmt.Println("Send Activations")
 		srv.Send(msg)
 	}
 }
@@ -140,6 +141,7 @@ func (rw *RemoteControl) Biases(_ *None, srv RemoteControl_BiasesServer) error {
 		default:
 		}
 		msg := <-rw.biasesQueue
+		fmt.Println("Send Biases")
 		srv.Send(msg)
 	}
 }
@@ -153,6 +155,7 @@ func (rw *RemoteControl) Weights(_ *None, srv RemoteControl_WeightsServer) error
 		default:
 		}
 		msg := <-rw.weightsQueue
+		fmt.Println("Send Weights")
 		srv.Send(msg)
 	}
 }

+ 8 - 0
neuralnetwork/snakesimulator/snakesimulator.go

@@ -14,6 +14,7 @@ import (
 
 	genetic "../genetic"
 	neuralnetwork "../neuralnetwork"
+	remotecontrol "../remotecontrol"
 	grpc "google.golang.org/grpc"
 )
 
@@ -22,6 +23,7 @@ type SnakeSimulator struct {
 	snake                *Snake
 	maxVerificationSteps int
 	stats                *Stats
+	remoteControl        *remotecontrol.RemoteControl
 
 	//GUI interface part
 	speed            uint32
@@ -56,6 +58,7 @@ func NewSnakeSimulator(maxVerificationSteps int) (s *SnakeSimulator) {
 		statsUpdateQueue:     make(chan bool, 2),
 		speedQueue:           make(chan uint32, 1),
 		speed:                10,
+		remoteControl:        &remotecontrol.RemoteControl{},
 	}
 	return
 }
@@ -63,6 +66,7 @@ func NewSnakeSimulator(maxVerificationSteps int) (s *SnakeSimulator) {
 // Population test method
 // Verifies population and returns unsorted finteses for each individual
 func (s *SnakeSimulator) Verify(population *genetic.Population) (fitnesses []*genetic.IndividalFitness) {
+	s.remoteControl.Init(population.Networks[0])
 	s.stats.Generation++
 	s.statsUpdateQueue <- true
 
@@ -96,7 +100,9 @@ func (s *SnakeSimulator) Verify(population *genetic.Population) (fitnesses []*ge
 	s.fieldUpdateQueue <- true
 	prevSpeed := s.speed
 	s.speed = 5
+	population.Networks[fitnesses[0].Index].SetStateWatcher(s.remoteControl)
 	s.runSnake(population.Networks[fitnesses[0].Index], false)
+	population.Networks[fitnesses[0].Index].SetStateWatcher(nil)
 	s.speed = prevSpeed
 	return
 }
@@ -361,6 +367,8 @@ func (s *SnakeSimulator) StartServer() {
 			fmt.Printf("Failed to serve: %v\n", err)
 		}
 	}()
+
+	go s.remoteControl.Run()
 }
 
 // Steaming of Field updates

+ 8 - 1
neuralnetwork/snakesimulator/snakesimulatorui/main.cpp

@@ -27,6 +27,7 @@
 #include <QQmlApplicationEngine>
 #include <QQmlContext>
 #include <QDebug>
+#include <QtProtobufTypes>
 
 #include "qgrpchttp2channel.h"
 #include "insecurecredentials.h"
@@ -43,7 +44,13 @@ public:
 int main(int argc, char *argv[])
 {
     QGuiApplication app(argc, argv);
-
+    QtProtobuf::registerProtoTypes();
+    qRegisterProtobufType<snakesimulator::Snake>();
+    qRegisterProtobufType<snakesimulator::Field>();
+    qRegisterProtobufType<snakesimulator::Stats>();
+    qRegisterProtobufType<snakesimulator::Point>();
+    qRegisterProtobufType<snakesimulator::Speed>();
+\
     qmlRegisterUncreatableType<QtProtobuf::QGrpcAsyncReply>("snakesimulator", 1, 0, "QGrpcAsyncReply", "");
     std::shared_ptr<snakesimulator::SnakeSimulatorClient> client(new snakesimulator::SnakeSimulatorClient);
     auto chan = std::shared_ptr<QtProtobuf::QGrpcHttp2Channel>(new QtProtobuf::QGrpcHttp2Channel(QUrl("http://localhost:65002"), QtProtobuf::InsecureCredentials()|NoneCredencials()));