-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.py
69 lines (54 loc) · 2.13 KB
/
Client.py
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
import sys
import getopt
import grpc
from Tsp import Tsp
from protoGenerated import hcfi_pb2_grpc
from protoGenerated import hcfi_pb2
def main(argv):
tsp = Tsp()
server = '127.0.0.1:50051'
print("Connection to serveur " + server)
channel = grpc.insecure_channel(server)
try:
opts, args = getopt.getopt(argv, "ht", ["hillclimber", "tabousearch"])
except getopt.GetoptError:
print("Error :")
print("\tuse -h or --hillclimber to use hillclimber algorithm")
print("\tuse -t or --tabousearch to use tabousearch algorithm")
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--hillclimber"):
print("\nHillClimber process is started......")
processHC(channel=channel, tsp=tsp)
elif opt in ("-t", "--tabousearch"):
print("\nTabou Search process is started......")
def processHC(channel, tsp):
"""
:param channel: channel connected to the API
:param kp: Tsp object
:keyword HillClimber
"""
# Creation of the HillClimber Service Stub
stubHillClimber = hcfi_pb2_grpc.HillClimberServiceStub(channel)
# Initialisation of the transaction
response = stubHillClimber.InitTransaction(hcfi_pb2.InitTransactionRequest(
customer='Client-TSP-Test',
algorithm="hillclimber_first_improvement",
solutionSize=0,
fitness=tsp.fitness(),
solution=tsp.toString()
))
tsp.solution = response.solution.split("-")
# Compute the fitness of the solution generated by the API
for i in range(0, 10000):
response = stubHillClimber.SendFitness(hcfi_pb2.FitnessRequest(
id=response.id,
fitness=tsp.fitness(),
solution=response.solution))
print(tsp.fitness())
tsp.solution = response.solution.split("-")
# Stop the transaction and receive the best solution founded by the API
stop = stubHillClimber.StopTransaction(hcfi_pb2.StopRequest(id=response.id, message='done'))
print("HillClimber process is ended, the best solution find for this transaction is " + str(stop.fitness))
if __name__ == "__main__":
main(sys.argv[1:])