/
client.go
132 lines (103 loc) 路 3.23 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package ga
import (
"context"
"net"
"time"
//"github.com/salvacorts/TFG-Parasitic-Metaheuristics/mlp/common/utils"
"github.com/golang/protobuf/ptypes/empty"
//"github.com/salvacorts/TFG-Parasitic-Metaheuristics/mlp-ea-decentralized/common/mlp"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
)
// Client is a client to communicate with the GRPC server
type Client struct {
ServerAddr string
Log *logrus.Logger
ID string
CustomDialer func(s string, dt time.Duration) (net.Conn, error)
Delegate ServiceDelegate
finalize bool
}
// StatsFetcher is a thread to ask server about stats {best_fitness, best_neurons, total_evaluations_overall}
func (c *Client) StatsFetcher(client *DistributedEAClient) {
for !c.finalize {
time.Sleep(30 * time.Second)
stats, err := (*client).GetStats(context.Background(), &empty.Empty{})
if err != nil {
c.Log.Errorf("Could not get stats from server")
}
c.Log.WithFields(logrus.Fields{
"level": "info",
"Scope": "remote",
"Evaluations": stats.Evaluations,
"BestFitness": stats.BestFitness,
"AvgFitness": stats.AvgFitness,
}).Infof("Got Stats from server")
}
}
// Start starts the client on a while loop borrowing, evaluating and returning individuals
func (c *Client) Start() error {
var conn *grpc.ClientConn
var err error
c.Log.Infof("Dialing %s", c.ServerAddr)
// For wasm use WebSockets
if c.CustomDialer != nil {
conn, err = grpc.Dial(
c.ServerAddr, grpc.WithDialer(c.CustomDialer), grpc.WithInsecure())
c.Log.Debugf("Using custom dialer\n")
} else {
conn, err = grpc.Dial(c.ServerAddr, grpc.WithInsecure())
}
if err != nil {
c.Log.Fatalf("Cannot Dial. Error: %s", err.Error())
return err
}
defer conn.Close()
client := NewDistributedEAClient(conn)
c.finalize = false
go c.StatsFetcher(&client)
desc, err := client.GetProblemDescription(context.Background(), &empty.Empty{})
if err != nil {
c.Log.Fatalf("Cannot get problem description from server. Error: %s", err.Error())
}
c.Delegate.DeserializeProblemDescription(desc.Payload)
localEvaluations := 0
start := time.Now()
for {
msg, err := client.BorrowIndividual(context.Background(), &empty.Empty{})
if err != nil {
c.Log.Fatalf("Cannot borrow individual from server. Error: %s", err.Error())
break
}
genome := c.Delegate.DeserializeGenome(msg.Genome)
score, err := genome.Evaluate()
if err != nil {
c.Log.Fatalf("Error evaluating Genome. Error: %s", err.Error())
break
}
c.Log.WithFields(logrus.Fields{
"level": "info",
"Scope": "local",
"Evaluations": localEvaluations,
"Fitness": score,
}).Infof("Got score: %f", score)
out := &Individual{
IndividualID: msg.IndividualID,
Evaluated: true,
Fitness: score,
// We do not need to serialize again since we have not modified it the genome
Genome: msg.Genome,
}
_, err = client.ReturnIndividual(context.Background(), out)
if err != nil {
c.Log.Fatalf("Cannot return back individual to server. Error: %s", err.Error())
break
}
localEvaluations++
if localEvaluations%100 == 0 {
c.Log.Infof("Throughput: %d evaluations / second", localEvaluations/int(time.Since(start).Seconds()))
}
}
c.finalize = true
return nil
}