예제 #1
0
    def load_data(self):
        self.load_lci_data()
        positions = {
            "tech": (0, self.tech_params.shape[0]),
            "bio": (self.tech_params.shape[0],
                    self.tech_params.shape[0] + self.bio_params.shape[0])
        }

        # `euf` is extract_uncertainty_fields; needed because we are
        # "gluing" together arrays with different column numbers and labels
        params = (euf(self.tech_params), euf(self.bio_params))

        if self.lcia:
            self.load_lcia_data()
            positions["cf"] = (positions["bio"][1],
                               positions["bio"][1] + self.cf_params.shape[0])
            params = params + (euf(self.cf_params), )

        if self.weighting:
            self.load_weighting_data()
            positions["weighting"] = (positions["bio"][1],
                                      positions["bio"][1] +
                                      self.cf_params.shape[0])
            params = params + (euf(self.weighting_params), )

        self.positions = positions
        self.params = np.hstack(params)
        self.rng = MCRandomNumberGenerator(self.params, seed=self.seed)
예제 #2
0
    def load_data(self):
        self.load_lci_data()
        positions = {
            "tech": (0, self.tech_params.shape[0]),
            "bio": (
                self.tech_params.shape[0],
                self.tech_params.shape[0] + self.bio_params.shape[0],
            ),
        }

        params = (self.tech_params, self.bio_params)

        if self.lcia:
            self.load_lcia_data()
            positions["cf"] = (
                positions["bio"][1],
                positions["bio"][1] + self.cf_params.shape[0],
            )
            params = params + (self.cf_params, )

        if getattr(self, "weighting", None):
            self.load_weighting_data()
            positions["weighting"] = (
                positions["bio"][1],
                positions["bio"][1] + self.cf_params.shape[0],
            )
            params = params + (self.weighting_params, )

        self.positions = positions
        self.params = np.hstack(params)
        self.rng = MCRandomNumberGenerator(self.params, seed=self.seed)
예제 #3
0
class MonteCarloLCA(LCA):
    """Monte Carlo uncertainty analysis with separate `random number generators <http://en.wikipedia.org/wiki/Random_number_generation>`_ (RNGs) for each set of parameters."""
    def __init__(self, demand, data_objs, seed=None, *args, **kwargs):
        self.seed = seed or get_seed()
        super().__init__(demand, data_objs, seed=self.seed, *args, **kwargs)
        self.logger.info("Seeded RNGs", extra={'seed': self.seed})

    def load_data(self):
        self.load_lci_data()
        self.tech_rng = MCRandomNumberGenerator(self.tech_params,
                                                seed=self.seed)
        self.bio_rng = MCRandomNumberGenerator(self.bio_params, seed=self.seed)
        if self.lcia:
            self.load_lcia_data()
            self.cf_rng = MCRandomNumberGenerator(self.cf_params,
                                                  seed=self.seed)
        # if self.weighting:
        #     self.load_weighting_data()
        #     self.weighting_rng = MCRandomNumberGenerator(self.weighting_params, seed=self.seed)
        if self.overrides:
            self.overrides.reset_sequential_indices()

    def __iter__(self):
        return self

    def __call__(self):
        return next(self)

    def __next__(self):
        if not hasattr(self, "tech_rng"):
            self.load_data()
        self.rebuild_technosphere_matrix(self.tech_rng.next())
        self.rebuild_biosphere_matrix(self.bio_rng.next())
        if self.lcia:
            self.rebuild_characterization_matrix(self.cf_rng.next())
        # if self.weighting:
        #     self.weighting_value = self.weighting_rng.next()

        if self.overrides:
            self.overrides.update_matrices()

        if not hasattr(self, "demand_array"):
            self.build_demand_array()

        self.lci_calculation()
        if self.lcia:
            self.lcia_calculation()
            # if self.weighting:
            #     self.weighting_calculation()
            return self.score
        else:
            return self.supply_array
예제 #4
0
 def load_data(self):
     self.load_lci_data()
     self.tech_rng = MCRandomNumberGenerator(self.tech_params, seed=self.seed)
     self.bio_rng = MCRandomNumberGenerator(self.bio_params, seed=self.seed)
     if self.lcia:
         self.load_lcia_data()
         self.cf_rng = MCRandomNumberGenerator(self.cf_params, seed=self.seed)
     if self.weighting:
         self.load_weighting_data()
         self.weighting_rng = MCRandomNumberGenerator(self.weighting_params, seed=self.seed)
     if self.presamples:
         self.presamples.reset_sequential_indices()
예제 #5
0
class MonteCarloLCA(IterativeMonteCarlo):
    """Monte Carlo uncertainty analysis with separate `random number generators <http://en.wikipedia.org/wiki/Random_number_generation>`_ (RNGs) for each set of parameters."""
    def load_data(self):
        self.load_lci_data()
        self.tech_rng = MCRandomNumberGenerator(self.tech_params,
                                                seed=self.seed)
        self.bio_rng = MCRandomNumberGenerator(self.bio_params, seed=self.seed)
        if self.has("characterization"):
            self.load_lcia_data()
            self.cf_rng = MCRandomNumberGenerator(self.cf_params,
                                                  seed=self.seed)
        if self.has("weighting"):
            self.load_weighting_data()
            self.weighting_rng = MCRandomNumberGenerator(self.weighting_params,
                                                         seed=self.seed)
        if getattr(self, "presamples", None):
            self.presamples.reset_sequential_indices()

    def __next__(self):
        if not hasattr(self, "tech_rng"):
            self.load_data()
        self.rebuild_technosphere_matrix(self.tech_rng.next())
        self.rebuild_biosphere_matrix(self.bio_rng.next())
        if self.has("characterization"):
            self.rebuild_characterization_matrix(self.cf_rng.next())
        if self.has("weighting"):
            self.weighting_value = self.weighting_rng.next()

        if getattr(self, "presamples", None):
            self.presamples.update_matrices()

        if not hasattr(self, "demand_array"):
            self.build_demand_array()

        self.lci_calculation()
        if self.has("characterization"):
            self.lcia_calculation()
            if self.has("weighting"):
                self.weighting_calculation()
            return self.score
        else:
            return self.supply_array
예제 #6
0
 def load_data(self):
     self.load_lci_data()
     self.tech_rng = MCRandomNumberGenerator(self.tech_params,
                                             seed=self.seed)
     self.bio_rng = MCRandomNumberGenerator(self.bio_params, seed=self.seed)
     if self.has("characterization"):
         self.load_lcia_data()
         self.cf_rng = MCRandomNumberGenerator(self.cf_params,
                                               seed=self.seed)
     if self.has("weighting"):
         self.load_weighting_data()
         self.weighting_rng = MCRandomNumberGenerator(self.weighting_params,
                                                      seed=self.seed)
     if getattr(self, "presamples", None):
         self.presamples.reset_sequential_indices()
예제 #7
0
    def load_data(self):
        if not getattr(self, "method"):
            raise ValueError("Must specify an LCIA method")

        self.load_lci_data()
        self.load_lcia_data()
        self.tech_rng = MCRandomNumberGenerator(self.tech_params, seed=self.seed)
        self.bio_rng = MCRandomNumberGenerator(self.bio_params, seed=self.seed)
        self.cf_rng = MCRandomNumberGenerator(self.cf_params, seed=self.seed)
예제 #8
0
class ComparativeMonteCarlo(IterativeMonteCarlo):
    """First draft approach at comparative LCA"""
    def __init__(self, demands, *args, **kwargs):
        self.demands = demands
        # Get all possibilities for database retrieval
        demand_all = demands[0].copy()
        for other in demands[1:]:
            demand_all.update(other)
        super(ComparativeMonteCarlo, self).__init__(demand_all, *args, **kwargs)

    def load_data(self):
        if not getattr(self, "method"):
            raise ValueError("Must specify an LCIA method")

        self.load_lci_data()
        self.load_lcia_data()
        self.tech_rng = MCRandomNumberGenerator(self.tech_params, seed=self.seed)
        self.bio_rng = MCRandomNumberGenerator(self.bio_params, seed=self.seed)
        self.cf_rng = MCRandomNumberGenerator(self.cf_params, seed=self.seed)

    def __next__(self):
        if not hasattr(self, "tech_rng"):
            self.load_data()
        self.rebuild_technosphere_matrix(self.tech_rng.next())
        self.rebuild_biosphere_matrix(self.bio_rng.next())
        self.rebuild_characterization_matrix(self.cf_rng.next())

        if self.presamples:
            self.presamples.update_matrices()

        results = []
        for demand in self.demands:
            self.build_demand_array(demand)
            self.lci_calculation()
            self.lcia_calculation()
            results.append(self.score)
        return results
예제 #9
0
class ParameterVectorLCA(IterativeMonteCarlo):
    """A Monte Carlo class where all uncertain parameters are stored in a single large array.

    Useful for sensitivity analysis and easy manipulation."""
    def load_data(self):
        self.load_lci_data()
        positions = {
            "tech": (0, self.tech_params.shape[0]),
            "bio": (self.tech_params.shape[0],
                    self.tech_params.shape[0] + self.bio_params.shape[0])
        }

        # `euf` is extract_uncertainty_fields; needed because we are
        # "gluing" together arrays with different column numbers and labels
        params = (euf(self.tech_params), euf(self.bio_params))

        if self.lcia:
            self.load_lcia_data()
            positions["cf"] = (positions["bio"][1],
                               positions["bio"][1] + self.cf_params.shape[0])
            params = params + (euf(self.cf_params), )

        if self.weighting:
            self.load_weighting_data()
            positions["weighting"] = (positions["bio"][1],
                                      positions["bio"][1] +
                                      self.cf_params.shape[0])
            params = params + (euf(self.weighting_params), )

        self.positions = positions
        self.params = np.hstack(params)
        self.rng = MCRandomNumberGenerator(self.params, seed=self.seed)

    def rebuild_all(self, vector=None):
        """Rebuild the LCI/LCIA matrices from a new Monte Carlo sample or provided vector."""
        if not hasattr(self, "positions"):
            self.load_data()

        if vector is not None and not isinstance(vector, np.ndarray):
            raise ValueError("`vector` must be a 1-d numpy array")

        if vector is not None:
            assert vector.shape == self.params.shape, \
                "Incorrect `vector` shape. Is {}, but should be {}".format(
                    vector.shape, self.params.shape
                )
        # Copy to break references and avoid later manipulation by RNG
        self.sample = (self.rng.next() if vector is None else vector).copy()
        self.rebuild_technosphere_matrix(self.tech_sample)
        self.rebuild_biosphere_matrix(self.bio_sample)
        if self.lcia:
            self.rebuild_characterization_matrix(self.cf_sample)
        if self.weighting:
            self.weighting_value = self.weighting_sample

        if self.presamples:
            self.presamples.update_matrices()

    def __next__(self):
        """Generate a new Monte Carlo iteration."""
        self.rebuild_all()

        if not hasattr(self, "demand_array"):
            self.build_demand_array()

        self.lci_calculation()
        if self.lcia:
            self.lcia_calculation()
            if self.weighting:
                self.weighting_calculation()
            return self.score
        else:
            return self.supply_array

    @property
    def tech_sample(self):
        return self.sample[self.positions["tech"][0]:self.positions["tech"][1]]

    @property
    def bio_sample(self):
        return self.sample[self.positions["bio"][0]:self.positions["bio"][1]]

    @property
    def cf_sample(self):
        return self.sample[self.positions["cf"][0]:self.positions["cf"][1]]

    @property
    def weighting_sample(self):
        return self.sample[self.positions["weighting"][0]:self.
                           positions["weighting"][1]]