Ejemplo n.º 1
0
 def __init__(self):
     super(ExampleOS, self).__init__()
     self.X = OptimizableVariable(VariableState(3.0), name="X")
     self.Y = OptimizableVariable(VariableState(20.0), name="Y")
     self.Z = OptimizableVariable(PickupState(
         (FunctionObject("f = lambda x, y: x**2 + y**2", ["f"]), "f"),
         (self.X, self.Y)),
                                  name="Z")
Ejemplo n.º 2
0
def test_variables_pickups_externals():
    """
    Check whether pickups are also working for strings
    """

    sum_fo = FunctionObject("f = lambda p, q: p + q", ["f"])

    p = OptimizableVariable(VariableState("glass1"))
    q = OptimizableVariable(VariableState("glass2"))
    r = OptimizableVariable(PickupState((sum_fo, "f"), (p, q)))

    assert p() == "glass1"
    assert q() == "glass2"
    assert r() == "glass1glass2"

    p.set_value("glass5")
    q.set_value("glass6")

    assert r() == "glass5glass6"

    r._state.parameters["functionobject"] = (FunctionObject(
        "f = lambda x, y: x*3 + y*4", ["f"]), "f")

    assert r() == "glass5glass5glass5glass6glass6glass6glass6"
Ejemplo n.º 3
0
    def set_optimizable_variables(self, names_tuple,
                                  dict_of_keys_and_value_tuples):
        """
        Set values in different instance of base instance.
        (deep copy). What about pickups?

        @param names_tuple (tuple of strings): names of the new systems
                                                (changes also keys)
        @param dict_of_keys_and_tuples (dict of tuples): consists of values
                                                        for fixed or
                                                        variable optimizable
                                                        variables.

        @return instance_list (list of instances)

        """
        length_value_tuples = 0
        if self.base_instance is None:
            instance_list = None
        else:
            instance_list = []
            if names_tuple is None:
                names_tuple = tuple([
                    self.base_instance.name
                    for r in dict_of_keys_and_value_tuples.values()[0]
                ])
            self.info("Constructing multiple configs with names: %s" %
                      (str(names_tuple)))
            if len(names_tuple) > 0:
                length_value_tuples = len(names_tuple)
                # use names_tuple to obtain no. of copies
                # notice that first flat copying instances and only deep copying
                # of variables does not work. Therefore we did it the other way
                # around.

                self.debug("Deep copying base instance")
                for index in range(length_value_tuples):
                    self.debug(str(index))
                    instance_list.append(copy.deepcopy(self.base_instance))
                    # deep copy instance

                self.debug("Constructing variables")
                for (index, instance) in enumerate(instance_list):
                    self.debug(str(index))
                    # reset all non-changing variables to instances of base_instance
                    # components.
                    for (key, variable) in OptimizableVariableKeyIterator(
                            instance).variables_dictionary.items():
                        print(key)
                        if key in dict_of_keys_and_value_tuples:
                            val_tuple = dict_of_keys_and_value_tuples[key]
                            self.debug("%s to %s" % (key, val_tuple[index]))
                            #variable.setvalue(val_tuple[index])
                            if not isinstance(val_tuple, tuple):
                                self.warning(
                                    "Incorrect format for multi config values!"
                                )
                                self.debug(
                                    "Values must be of type (string, contents):"
                                )
                                self.debug(
                                    "For fixed values: (\"fixed\", fixed_val)")
                                self.debug(
                                    "For pickup values: (\"pickup\", function)"
                                )
                            else:
                                (mcv_type, mcv_contents) = val_tuple[index]
                                if mcv_type.lower() == "fixed":
                                    instance.resetVariable(
                                        key,
                                        OptimizableVariable(
                                            FixedState(mcv_contents),
                                            name=variable.name))
                                elif mcv_type.lower() == "pickup":
                                    instance.resetVariable(
                                        key,
                                        OptimizableVariable(
                                            PickupState(
                                                mcv_contents, variable),
                                            name=variable.name))
                                else:
                                    self.warning(
                                        "Unknown type for multi config values")
                        else:
                            self.debug("Reseting %s" % (key, ))
                            instance.resetVariable(
                                key, self.base_instance.getVariable(key))
                    # set names of the new instances accordingly
                    self.debug("Setting name of instance %s" %
                               (names_tuple[index]))
                    instance.set_name(names_tuple[index])

        return instance_list