-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.py
More file actions
91 lines (73 loc) · 2.85 KB
/
Client.py
File metadata and controls
91 lines (73 loc) · 2.85 KB
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
import sys
import getopt
import grpc
from KnapSack import KnapSack
from protoGenerated import hcfi_pb2_grpc
from protoGenerated import hcfi_pb2
from protoGenerated import tabousearch_pb2_grpc
from protoGenerated import tabousearch_pb2
from protoGenerated import messages_pb2
def main(argv):
kp = KnapSack("ks_1000.dat")
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, kp=kp)
elif opt in ("-t", "--tabousearch"):
print("\nTabou Search process is started......")
processTS(channel=channel, kp=kp)
def processHC(channel, kp):
"""
:param channel: channel connected to the API
:param kp: KnapSack object
:keyword HillClimber
"""
# Creation of the HillClimber Service Stub
stubHillClimber = hcfi_pb2_grpc.HillClimberServiceStub(channel)
# Initialisation of the transaction
response = stubHillClimber.InitTransaction(messages_pb2.InitTransactionRequest(
customer='Client-Knapsack-Test',
solutionSize=kp.nbObject,
type="knapsack",
fitness=kp.fitness(),
solution=kp.toString()
))
# Compute the fitness of the solution generated by the API
for i in range(0, 10000):
response = stubHillClimber.SendFitness(messages_pb2.FitnessRequest(
id=response.id,
fitness=kp.fitness(response.solution),
solution=response.solution))
# Stop the transaction and receive the best solution founded by the API
stop = stubHillClimber.StopTransaction(messages_pb2.StopRequest(id=response.id, message='done'))
print("HillClimber process is ended, the best solution find for this transaction is " + str(stop.fitness))
def processTS(channel, kp):
"""
:param channel: channel connected to the API
:param kp: KnapSack object
:keyword TabouSearch
"""
# Creation of the HillClimber Service Stub
stubTabouSearch = tabousearch_pb2_grpc.TabouSearchServiceStub(channel=channel)
# Initialisation of the transaction
response = stubTabouSearch.InitTransaction(messages_pb2.InitTransactionRequest(
customer='Client-Knapsack-Test',
solutionSize=kp.nbObject,
type="knapsack",
fitness=kp.fitness(),
solution=kp.toString()
))
print(response.id)
print(response.solution)
if __name__ == "__main__":
main(sys.argv[1:])