summaryrefslogtreecommitdiffstats
path: root/conductor/conductor/solver/request/objective.py
blob: 055905693a0761ce25e54450cf445ca0e540c8b1 (plain)
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
#!/usr/bin/env python
#
# -------------------------------------------------------------------------
#   Copyright (c) 2015-2017 AT&T Intellectual Property
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
# -------------------------------------------------------------------------
#


from conductor.solver.request import demand
# from conductor.solver.resource import region
# from conductor.solver.resource import service


class Objective(object):

    def __init__(self):
        self.goal = None
        self.operation = None
        self.operand_list = []

    def compute(self, _decision_path, _request):
        value = 0.0

        for op in self.operand_list:
            if self.operation == "sum":
                value += op.compute(_decision_path, _request)

        _decision_path.cumulated_value = value
        _decision_path.total_value = \
            _decision_path.cumulated_value + \
            _decision_path.heuristic_to_go_value


class Operand(object):

    def __init__(self):
        self.operation = None
        self.weight = 0
        self.function = None

    def compute(self, _decision_path, _request):
        value = 0.0
        cei = _request.cei
        if self.function.func_type == "distance_between":
            if isinstance(self.function.loc_a, demand.Location):
                if self.function.loc_z.name in \
                        _decision_path.decisions.keys():
                    resource = \
                        _decision_path.decisions[self.function.loc_z.name]
                    loc = None
                    # if isinstance(resource, region.Region):
                    #     loc = resource.location
                    # elif isinstance(resource, service.Service):
                    #     loc = resource.region.location
                    loc = cei.get_candidate_location(resource)
                    value = \
                        self.function.compute(self.function.loc_a.value, loc)
            elif isinstance(self.function.loc_z, demand.Location):
                if self.function.loc_a.name in \
                        _decision_path.decisions.keys():
                    resource = \
                        _decision_path.decisions[self.function.loc_a.name]
                    loc = None
                    # if isinstance(resource, region.Region):
                    #    loc = resource.location
                    # elif isinstance(resource, service.Service):
                    #    loc = resource.region.location
                    loc = cei.get_candidate_location(resource)
                    value = \
                        self.function.compute(self.function.loc_z.value, loc)
            else:
                if self.function.loc_a.name in \
                        _decision_path.decisions.keys() and \
                   self.function.loc_z.name in \
                        _decision_path.decisions.keys():
                    resource_a = \
                        _decision_path.decisions[self.function.loc_a.name]
                    loc_a = None
                    # if isinstance(resource_a, region.Region):
                    #     loc_a = resource_a.location
                    # elif isinstance(resource_a, service.Service):
                    #     loc_a = resource_a.region.location
                    loc_a = cei.get_candidate_location(resource_a)
                    resource_z = \
                        _decision_path.decisions[self.function.loc_z.name]
                    loc_z = None
                    # if isinstance(resource_z, region.Region):
                    #     loc_z = resource_z.location
                    # elif isinstance(resource_z, service.Service):
                    #     loc_z = resource_z.region.location
                    loc_z = cei.get_candidate_location(resource_z)

                    value = self.function.compute(loc_a, loc_z)

        elif self.function.func_type == "cost":
            for demand_name, candidate_info in _decision_path.decisions.items():
                value += float(candidate_info['cost'])

        if self.operation == "product":
            value *= self.weight

        return value