コード例 #1
0
ファイル: sobol_test.py プロジェクト: CnrLwlss/sobol_seq
def sobol_test_generate():
    """
    sobol_test02 tests i4_sobol_generate.
    """
    print("\nSOBOL_TEST_GENERATE" "  I4_BIT_ returns the location of the high 1 bit." "\n     I     I4_BIT_HI1(I)\n")

    target = np.array(
        [
            [0.5, 0.5, 0.5, 0.5, 0.5],
            [0.75, 0.25, 0.75, 0.25, 0.75],
            [0.25, 0.75, 0.25, 0.75, 0.25],
            [0.375, 0.375, 0.625, 0.125, 0.875],
            [0.875, 0.875, 0.125, 0.625, 0.375],
            [0.625, 0.125, 0.375, 0.375, 0.125],
            [0.125, 0.625, 0.875, 0.875, 0.625],
            [0.1875, 0.3125, 0.3125, 0.6875, 0.5625],
            [0.6875, 0.8125, 0.8125, 0.1875, 0.0625],
            [0.9375, 0.0625, 0.5625, 0.9375, 0.3125],
        ]
    )

    results = i4_sobol_generate(5, 10)

    assert np.all(target == results), "Array values not as expected"

    return
コード例 #2
0
 def sample(self, num, d=None, rng=np.random):
     if d is None or d < 1 or d > 40: # TODO: also check if integer
         raise ValueError("d (%d) must be integer in range [1, 40]" % d)
     num, d = self._sample_shape(num, d)
     #from scipy.stats import uniform
     #mdd = MultiDimDistribution([uniform for _ in range(d)])
     #return np.asarray(mdd.sobol(num))
     return i4_sobol_generate(d, num, skip=0)
コード例 #3
0
ファイル: TaskWeek6.py プロジェクト: finartist/CG1
def getSobolCosinePointsH2(n):
    
    sobolpoints = sobol_seq.i4_sobol_generate(2, n)
    
    #map to hemisphere cosine weighted
    sobolpoints[:, 0] = np.arcsin(np.sqrt(sobolpoints[:,0]))
    sobolpoints[:, 1] = sobolpoints[:, 1] * 2 * np.pi
    
    return sobolpoints
コード例 #4
0
ファイル: TaskWeek6.py プロジェクト: finartist/CG1
def getSobolSequencePointsH2(n):
    
    sobolpoints = sobol_seq.i4_sobol_generate(2, n)
    
    sobolpoints[:, 1] = sobolpoints[:, 1] * 2 * np.pi
               
    #map to hemisphere
    sobolpoints[:, 0] = np.arccos(sobolpoints[:,0])
    
    return sobolpoints
コード例 #5
0
ファイル: sobol_design.py プロジェクト: SheffieldML/GPyOpt
    def get_samples(self, init_points_count):
        samples = np.empty((init_points_count, self.space.dimensionality))

        # Use random design to fill non-continuous variables
        random_design = RandomDesign(self.space)
        random_design.fill_noncontinous_variables(samples)

        if self.space.has_continuous():
            bounds = self.space.get_continuous_bounds()
            lower_bound = np.asarray(bounds)[:,0].reshape(1,len(bounds))
            upper_bound = np.asarray(bounds)[:,1].reshape(1,len(bounds))
            diff = upper_bound-lower_bound

            from sobol_seq import i4_sobol_generate
            X_design = np.dot(i4_sobol_generate(len(self.space.get_continuous_bounds()),init_points_count),np.diag(diff.flatten()))[None,:] + lower_bound
            samples[:, self.space.get_continuous_dims()] = X_design

        return samples
コード例 #6
0
def acq_max_geometric(ac, gp, bounds,cache_sobol):
    """
    A function to find the maximum of the acquisition function using
    the scipy python

    Input Parameters
    ----------
    ac: The acquisition function object that return its point-wise value.
    gp: A gaussian process fitted to the relevant data.
    y_max: The current maximum known value of the target function.
    bounds: The variables bounds to limit the search of the acq max.
    
    Returns
    -------
    x_max, The arg max of the acquisition function.
    """

    dim=bounds.shape[0]
    # Start with the lower bound as the argmax
    x_max = bounds[:, 0]
    max_acq = None

    #myopts ={'maxiter':5*dim,'maxfun':10*dim}
    #myopts ={'maxiter':5*dim}
   
    # create a grid
    #XXX = np.meshgrid(*[np.linspace(i,j,10)[:-1] for i,j in zip(bounds[:,0],bounds[:,1])])
    
    ninitpoint=200*dim
    #ninitpoint=5*dim
    
    if cache_sobol is not None:
        x_tries=cache_sobol

    else:
        print('sobol sequence is not cached')
        x_tries = sobol_seq.i4_sobol_generate(dim, ninitpoint)
        

    # randomly select points and evaluate points from a grid
    
    
    # Find the minimum of minus the acquisition function        
    #x_tries = np.random.uniform(bounds[:, 0], bounds[:, 1],size=(20*dim, dim))

    # evaluate
    #start_eval=time.time()
    y_tries=ac(x_tries,gp=gp)
    #end_eval=time.time()
    #print "elapse evaluate={:.5f}".format(end_eval-start_eval)
    
    #find x optimal for init
    idx_max=np.argmax(y_tries)
    x_max=x_tries[idx_max]
    
    #start_opt=time.time()

    #res = minimize(lambda x: -ac(x.reshape(1, -1), gp=gp),x_init_max.reshape(1, -1),bounds=bounds,method="L-BFGS-B",options=myopts)#L-BFGS-B
    
    
    #res = fmin_bfgs(lambda x: -ac(x.reshape(1, -1), gp=gp, y_max=y_max),x_init_max.reshape(1, -1),disp=False)#L-BFGS-B
    # value at the estimated point
    #val=ac(res.x,gp,y_max)        
   

    # Clip output to make sure it lies within the bounds. Due to floating
    # point technicalities this is not always the case.
    #return np.clip(x_max[0], bounds[:, 0], bounds[:, 1])
    return np.clip(x_max, bounds[:, 0], bounds[:, 1])
コード例 #7
0
ファイル: construct.py プロジェクト: g13/patchV1
def generate_pos_3d(lcurve, rcurve, target_area, ly, ry, n, seed):
    nl = ly.size - 1
    nr = ry.size - 1
    assert (len(lcurve) == nl)
    assert (len(rcurve) == nr)
    if min(ly) != min(ry):
        print(f'{ly}:{ry}')
        assert (min(ly) == min(ry))
    if max(ly) != max(ry):
        print(f'{ly}:{ry}')
        assert (max(ly) == max(ry))

    def collect_3d(n, x, y, z):
        selected = np.ones(n, dtype=bool)
        for i in range(n):
            for j in range(nl):
                if ly[j] < y[i] and y[i] < ly[j + 1]:
                    if lcurve[j].check(y[i]) > x[i]:
                        selected[i] = False
                        break
            if selected[i]:
                for j in range(nr):
                    if ry[j] < y[i] and y[i] < ry[j + 1]:
                        if rcurve[j].check(y[i]) < x[i]:
                            selected[i] = False
                            break
        pos = np.array([x[selected], y[selected], z[selected]])
        return pos, sum(selected)

    xmax = max([r.xmax() for r in rcurve])
    xmin = min([l.xmin() for l in lcurve])
    ymax = max(ly)
    ymin = min(ly)
    storming_area = (xmax - xmin) * (ymax - ymin)
    assert (storming_area > 0)
    #print(xmax,xmin,ymax,ymin, storming_area)

    ratio = storming_area / target_area
    #print(ratio)
    # to do: if ratio > some threshold rotate the coordinates and implement methods for the corresponding change for the Class of curves, too
    ntmp = np.ceil(n * ratio).astype(int)
    i = 0
    pos = np.empty((3, n), dtype=float)
    skip = 602
    #qrng = rand.QRNG(rndtype=rand.QRNG.SCRAMBLED_SOBOL32, ndim=2)
    count = 0
    while (i < n):
        #irands = np.empty((2,ntmp), dtype='uint32')
        rands = ss.i4_sobol_generate(3, ntmp, skip=skip)
        # wait for scipy 1.7.0

        x = xmin + (xmax - xmin) * rands[:, 0]
        y = ymin + (ymax - ymin) * rands[:, 1]
        z = 0.01 * rands[:, 2]

        acquired_pos, nselected = collect_3d(ntmp, x, y, z)
        if i + nselected > n:
            nselected = n - i
        pos[:, i:i + nselected] = acquired_pos[:, :nselected]
        i += nselected
        ntmp = np.ceil((n - i) * ratio).astype(int)
        skip = skip + ntmp
        count += 1
        if count > 10:
            print(count, ntmp)
    return pos
コード例 #8
0
plt.scatter(x2k, y2k, s=1, marker='.')
plt.yticks([])
plt.xticks([])

plt.subplot(2, 5, 5)
x5k = [random.uniform(0, 4) for x in range(5000)]
y5k = [random.uniform(0, 4) for x in range(5000)]
plt.title("N=5000")
plt.scatter(x5k, y5k, s=1, marker='.')
plt.yticks([])
plt.xticks([])

#Quasi-Random goes down here.  Fingers crossed.

plt.subplot(2, 5, 6)
sobol_100_x = sobol_seq.i4_sobol_generate(4, 100)
x100 = sobol_100_x[:, 0]
y100 = sobol_100_x[:, 1]
plt.ylabel("Quasi Random")
plt.scatter(x100, y100, s=1, marker='.')
plt.yticks([])
plt.xticks([])

plt.subplot(2, 5, 7)
sobol_500_x = sobol_seq.i4_sobol_generate(4, 500)
x500 = sobol_500_x[:, 0]
y500 = sobol_500_x[:, 1]
plt.scatter(x500, y500, s=1, marker='.')
plt.yticks([])
plt.xticks([])
コード例 #9
0
    def generate_initial_observations(n,
                                      logger,
                                      loaded_init_theta=None,
                                      loaded_init_G=None,
                                      loaded_init_G_sem=None):
        """
        Takes an integer `n` and generates `n` initial observations
        from the black box function using Sobol random parameter settings
        in the unit cube. Returns parameter setting and black box function outputs.
        If `loaded_init_theta/G/G_sem` are specified, initialization is loaded (possibly partially, in which
        case the initialization using the Sobol random sequence is continued where left off).
        """

        if n <= 0:
            raise ValueError(
                'qKnowledgeGradient and GP needs at least one observation to be defined properly.'
            )

        # sobol sequence proposal points
        # new_thetas: [n, n_params]
        new_thetas = torch.tensor(sobol_seq.i4_sobol_generate(n_params, n),
                                  dtype=torch.float)

        # check whether initial observations are loaded
        loaded = (loaded_init_theta is not None and loaded_init_G is not None
                  and loaded_init_G_sem is not None)
        if loaded:
            n_loaded = loaded_init_theta.shape[
                0]  # loaded no. of observations total
            n_loaded_init = min(
                n_loaded,
                n)  # loaded no. of quasi-random initialization observations
            n_init = max(
                n_loaded, n
            )  # final no. of observations returned, at least quasi-random initializations

            # check whether loaded proposal points are same as without loading observations
            try:
                assert (np.allclose(loaded_init_theta[:n_loaded_init],
                                    new_thetas[:n_loaded_init]))
            except AssertionError:
                print(
                    '\n\n\n===> Warning: parameters of loaded inital observations '
                    'do not coincide with initialization that would have been done. '
                    'Double check simulation, ninit, and parameter bounds, which could change '
                    'the initial random Sobol sequence. \nThe loaded parameter settings are used. \n\n\n'
                )

            if n_init > n:
                new_thetas = loaded_init_theta  # size of tensor increased to `n_init`, as more than Sobol init points loaded

        else:
            n_loaded = 0  # loaded no. of observations total
            n_loaded_init = 0  # loaded no. of quasi-random initialization observations
            n_init = n  # final no. of observations returned, at least quasi-random initializations

        # instantiate simulator observation tensors
        if per_age_group_objective:
            # new_G, new_G_sem: [n_init, n_days * n_age] (flattened outputs)
            new_G = torch.zeros((n_init, n_days * n_age), dtype=torch.float)
            new_G_sem = torch.zeros((n_init, n_days * n_age),
                                    dtype=torch.float)
        else:
            # new_G, new_G_sem: [n_init, n_days]
            new_G = torch.zeros((n_init, n_days), dtype=torch.float)
            new_G_sem = torch.zeros((n_init, n_days), dtype=torch.float)

        # generate `n` initial evaluations at quasi random settings; if applicable, skip and load expensive evaluation result
        for i in range(n_init):

            # if loaded, use initial observation for this parameter settings
            if loaded and i <= n_loaded - 1:
                new_thetas[i] = loaded_init_theta[i]
                G, G_sem = loaded_init_G[i], loaded_init_G_sem[i]
                walltime = 0.0

            # if not loaded, evaluate as usual
            else:
                t0 = time.time()
                G, G_sem = composite_simulation(new_thetas[i])
                walltime = time.time() - t0

            new_G[i] = G
            new_G_sem[i] = G_sem

            # log
            G_objectives = objective(new_G[:i + 1])
            best_idx = G_objectives.argmax()
            best = G_objectives[best_idx].item()
            current = objective(G).item()

            if per_age_group_objective:
                case_diff = G.reshape(n_days,
                                      n_age)[-1].sum() - G_obs_aggregate[-1]
            else:
                case_diff = G[-1] - G_obs_aggregate[-1]

            logger.log(i=i - n,
                       time=walltime,
                       best=best,
                       objective=current,
                       case_diff=case_diff,
                       theta=transforms.unnormalize(
                           new_thetas[i, :].detach().squeeze(), sim_bounds))

            # save state
            state = {
                'train_theta': new_thetas[:i + 1],
                'train_G': new_G[:i + 1],
                'train_G_sem': new_G_sem[:i + 1],
                'best_observed_obj': best,
                'best_observed_idx': best_idx,
            }
            save_state(state, logger.filename)

        # compute best objective from simulations
        f = objective(new_G)
        best_f_idx = f.argmax()
        best_f = f[best_f_idx].item()

        return new_thetas, new_G, new_G_sem, best_f, best_f_idx
コード例 #10
0
ファイル: methods.py プロジェクト: zkkxu/tf-quant-finance
def non_uniform_grid(num, ndim, skip=42, large=False):
    """Build a non-uniform grid with num points of ndim dimensions."""
    if not large:
        _check_not_too_large(num * ndim)
    return sobol_seq.i4_sobol_generate(ndim, num, skip=skip)
コード例 #11
0
ファイル: HyCho.py プロジェクト: ratnania/mhd
    particles[:, 0] = particles[:, 0] * Lz
    particles[:, 1] = sp.erfinv(2 * particles[:, 1] - 1) * wperp * np.sqrt(2)
    particles[:, 2] = sp.erfinv(2 * particles[:, 2] - 1) * wperp * np.sqrt(2)
    particles[:, 3] = sp.erfinv(2 * particles[:, 3] - 1) * wpar * np.sqrt(2)

elif loading == 'external loading':
    particles[:, :] = np.load(name_initial_particles)

    # inversion of cumulative distribution function
    particles[:, 0] = particles[:, 0] * Lz
    particles[:, 1] = sp.erfinv(2 * particles[:, 1] - 1) * wperp * np.sqrt(2)
    particles[:, 2] = sp.erfinv(2 * particles[:, 2] - 1) * wperp * np.sqrt(2)
    particles[:, 3] = sp.erfinv(2 * particles[:, 3] - 1) * wpar * np.sqrt(2)

elif loading == 'sobol_plain':
    particles[:, :4] = sobol.i4_sobol_generate(4, Np, 1000)

    # inversion of cumulative distribution function
    particles[:, 0] = particles[:, 0] * Lz
    particles[:, 1] = sp.erfinv(2 * particles[:, 1] - 1) * wperp * np.sqrt(2)
    particles[:, 2] = sp.erfinv(2 * particles[:, 2] - 1) * wperp * np.sqrt(2)
    particles[:, 3] = sp.erfinv(2 * particles[:, 3] - 1) * wpar * np.sqrt(2)

elif loading == 'sobol_antithetic16':
    pic.set_particles_symmetric(sobol.i4_sobol_generate(4, int(Np / 16), 1000),
                                particles)

    # inversion of cumulative distribution function
    particles[:, 0] = particles[:, 0] * Lz
    particles[:, 1] = sp.erfinv(2 * particles[:, 1] - 1) * wperp * np.sqrt(2)
    particles[:, 2] = sp.erfinv(2 * particles[:, 2] - 1) * wperp * np.sqrt(2)
コード例 #12
0
    x = [random.random() for k in range(N[i])]
    y = [random.random() for k in range(N[i])]
    axes = plt.subplot(2, 5, i + 1)
    plt.xlim(0.0, 1.0)
    plt.ylim(0.0, 1.0)
    axes.set_title('N=' + str(N[i]))
    axes.set_aspect('equal')
    plt.scatter(x, y, s=1)
    if i == 0:
        axes.set_ylabel('Pseudo-Random', rotation=90, size='large')
    else:
        axes.get_yaxis().set_ticklabels([])
    axes.get_xaxis().set_ticklabels([])

for i in range(len(N)):
    z = sobol_seq.i4_sobol_generate(2, N[i])
    x = z[:, 0]
    y = z[:, 1]
    axes = plt.subplot(2, 5, i + len(N) + 1)
    plt.xlim(0.0, 1.0)
    plt.ylim(0.0, 1.0)
    axes.set_aspect('equal')
    plt.scatter(x, y, s=1)
    if i == 0:
        axes.set_ylabel('Quasi-Random', rotation=90, size='large')
    else:
        axes.get_yaxis().set_ticklabels([])

fig.tight_layout()
plt.subplots_adjust(wspace=0.1, hspace=0.1)
plt.show()
コード例 #13
0
ファイル: uq_gsa.py プロジェクト: strespi/chemics-reactors
    def _generate_doe_design(self):
        """
        This function is responsible for generating the matrix of operating
        conditions used in the design of experiments. It supports all of the
        design types implemented by pyDOE with the exception of general full
        factorials (including mixed level factorials). Full factorials are only
        permitted to have two levels at the moment.

        Parameters
        ----------
        None

        Returns
        -------
        None, but updates the self object to have the needed auxiliary data
        """

        # Unpack the dictionary of DOE design parameters
        doe_type = self.doe_design['doe_args']['type']
        kwargs = self.doe_design['doe_args']['args']

        # Get the list of parameters to iterate over
        try:
            param_list = self.doe_design['doe_params']
        except:
            self._generate_doe_param_list()
            param_list = self.doe_design['doe_params']
        n = len(param_list)

        # Create the design matrix in coded units
        if doe_type == 'full2':  # Two level general factorial
            coded_design_matrix = pyDOE.ff2n(n)
        elif doe_type == 'frac':  # Fractional factorial
            gen = kwargs.pop('gen', None)
            if gen is None:
                raise ValueError(
                    'No generator sequence specified for a fractional factorial design.'
                )
            coded_design_matrix = pyDOE.fracfact(gen)
        elif doe_type == 'pb':  # Plackett-Burman
            coded_design_matrix = pyDOE.pbdesign(n)
        elif doe_type == 'bb':  # Box-Behnken
            coded_design_matrix = pyDOE.bbdesign(n, **kwargs)
        elif doe_type == 'cc':  # Central composite
            coded_design_matrix = pyDOE.ccdesign(n, **kwargs)
        elif doe_type == 'lh':  # Latin hypercube
            coded_design_matrix = pyDOE.lhs(n, **kwargs)
        elif doe_type == 'sob':  # Sobol' Lp-tau low discrepancy sequence
            samples = kwargs.pop('samples')
            coded_design_matrix = sobol_seq.i4_sobol_generate(n, samples)
        else:
            raise ValueError('Unrecognized DOE design option ' + doe_type)

        # Convert the coded design matrix into an uncoded design matrix (i.e.,
        # in terms of the raw operating conditions). This takes the minimum and
        # maximum values from the distributions and places the points
        # accordingly.
        a_array = np.zeros(n)  # Minimum
        b_array = np.zeros(n)  # Maximum
        for i in range(n):
            pkey = param_list[i]
            a_array[i] = self.doe_design['doe_param_dist'][pkey].a
            b_array[i] = self.doe_design['doe_param_dist'][pkey].b
        r_array = b_array - a_array  # Ranges for each dimension
        if doe_type in ['pb', 'bb', 'cc', 'frac', 'full2']:
            # These designs all have points clustered around a distinct center.
            # The coded matrix is in terms of unit perturbations from the
            # center, so we can just scale them by the ranges before adding the
            # center value. For these designs, we actually want to use half the
            # range so that the points that are supposed to be at the min/max
            # values end up there.
            c_array = (a_array + b_array) / 2  # Center is average of min, max
            doe_op_conds = coded_design_matrix * r_array / 2 + c_array
        elif doe_type in ['lh', 'sob']:
            # The Latin hypercube and Sobol' sequences space points between a
            # range. This means the coded design matrix has all elements on
            # (0, 1). Since we don't have a center point, we place the operating
            # conditions with respect to the minimum values.
            doe_op_conds = coded_design_matrix * r_array + a_array
        self.doe_design['doe_op_conds'] = doe_op_conds
コード例 #14
0
def traj_segment_generator(pi, env, horizon, stochastic, num_options, saves,
                           results, rewbuffer, dc):
    max_action = env.action_space.high
    t = 0
    glob_count = 0
    glob_count_thresh = -1
    ac = env.action_space.sample()  # not used, just so we have the datatype
    new = True  # marks if we're on first timestep of an episode
    ob = env.reset()
    ob_env_shape = np.shape(ob)
    ac_env_shape = np.shape(ac)

    ac = pi.reset_last_act().eval()

    ob = np.concatenate((ob, ac))

    cur_ep_ret = 0  # return in current episode
    cur_ep_len = 0  # len of current episode
    ep_rets = []  # returns of completed episodes in this segment
    ep_lens = []  # lengths of ...

    # Initialize history arrays
    horizon = 500
    obs = np.array([ob for _ in range(horizon)])
    rews = np.zeros(horizon, 'float32')
    realrews = np.zeros(horizon, 'float32')
    vpreds = np.zeros(horizon, 'float32')
    wrong_checks = np.zeros(1, 'int32')
    news = np.zeros(horizon, 'int32')
    opts = np.zeros(horizon, 'int32')
    incorrect = np.zeros(horizon, 'int32')
    opts_val = np.zeros(horizon, 'float32')
    acs = np.array([ac for _ in range(horizon)])
    prevacs = acs.copy()

    # These are params that need to be predefined!
    # load the previously exported file:
    nnet_file_name = 'standart_all_comp.nnet'
    # define the angular range and the safe angular range
    #for the retraining if a point is unstable it has to be in the safe range
    ang = 1 * 2.5 * 0.0174533
    ang_save = ang - 0.0001
    # define the angular velocity and the safe angular velocity range
    angvel = 1 * 5.0 * 0.0174533
    angvel_save = angvel - 0.0001

    while True:
        if (t > 0):
            yield {
                "ob": obs,
                "rew": rews,
                "realrew": realrews,
                "vpred": vpreds,
                "new": news,
                "ac": acs,
                "opts": opts,
                "opts_val": opts_val,
                "incorrect": incorrect,
                "wrong_checks": wrong_checks
            }
        t = 0
        # first do the overall check:
        unsucess = check_whole(nnet_file_name, ang, ang_save, angvel,
                               angvel_save)
        if (True):
            if (len(unsucess) == 0):
                print("YES WE DID IT -> CONVERGENCE WAS REACHED!")
                wrong_checks[0] = 0
            else:
                wrong_checks[0] = len(unsucess)
                for i in range(len(unsucess)):
                    obs[t] = [
                        unsucess[i][0], unsucess[i][1], unsucess[i][2],
                        unsucess[i][4]
                    ]
                    acs[t] = unsucess[i][3]
                    incorrect[t] = 1
                    opts_val[t] = -2.0
                    opts[t] = 1
                    t += 1

        sample = None

        num_local_samples = 0  #

        # generate additional points using Sobol sequences
        size_to_gen = (horizon - (num_local_samples + 1) * len(unsucess))
        if (True):
            # using sobol
            sample_new = np.zeros((size_to_gen, 4))
            sample_new[:, 1:4] = (
                (sobol_seq.i4_sobol_generate(3, size_to_gen)) * 2 - 1).reshape(
                    size_to_gen, 3)
            sample_new[:, 0] = 1.0
            sample_new[:, 1] = sample_new[:, 1] * ang
            sample_new[:, 2] = sample_new[:, 2] * angvel
        if (sample is None):
            sample = sample_new
        else:
            sample = np.concatenate((sample, sample_new))

        overall_things = 0
        curr_correct = 0
        comm_savings = 0
        no_comm_samples = 0

        # now assign the correct values to the points:
        for i in range((np.shape(sample))[0]):
            overall_things += 1
            dec = pi._get_op_orig([sample[i]])[0][0][0]
            if (dec > 0):  #this means we select opt 0:
                curr_opt = 0
                comm_savings += 1
            else:
                curr_opt = 1

            # check the individual points:
            corr, point = check_whole_comp_comm_eff(nnet_file_name,
                                                    sample[i, 1], ang_save,
                                                    sample[i, 2], angvel_save,
                                                    sample[i, 3], curr_opt)

            if (corr):
                if (point == "no_comm_also_works"):
                    # special case: saving communication also works -> we want this label
                    obs[t] = [
                        sample[i, 0], sample[i, 1], sample[i, 2], sample[i, 3]
                    ]
                    acs[t] = sample[i, 3]
                    incorrect[t] = 1
                    opts_val[t] = 0.4
                    opts[t] = 0
                    no_comm_samples += 1
                else:
                    curr_correct += 1
                    obs[t] = [
                        sample[i, 0], sample[i, 1], sample[i, 2], sample[i, 3]
                    ]
                    acs[t] = sample[i, 3]
                    incorrect[t] = 0
                    if (dec > 0):
                        # this means we skip communication:
                        opts_val[t] = 0.4
                        no_comm_samples += 1
                        opts[t] = 0
                    else:
                        # for case of communication assign value of -2
                        # -> this case is more important compared to saving communication,...
                        opts_val[t] = -2.0
                        opts[t] = 1

            else:
                # the point is incorrect -> also give -2 but additionally mark as incorrect
                obs[t] = [
                    sample[i, 0], sample[i, 1], sample[i, 2], sample[i, 3]
                ]
                acs[t] = point[3]
                incorrect[t] = 1
                opts_val[t] = -2.0
                opts[t] = 1
            t += 1

        print("Rate correct samples: " + str(curr_correct / overall_things))
        print("Comm savings: " + str(comm_savings / overall_things))
        print(no_comm_samples)
        time.sleep(5.0)
コード例 #15
0





"""Part 3.2"""

plt.subplot(2, 5, 1)
N = 100
x = np.random.rand(N)
y = np.random.rand(N)
plt.scatter(x, y)

plt.subplot(2, 5, 6)
vals = sobol_seq.i4_sobol_generate(2, 100)
vals[:,0]
vals[:,1]
plt.scatter(x=vals[:,0], y=vals[:,1])           

plt.subplot(2, 5, 2)
N = 500
x = np.random.rand(N)
y = np.random.rand(N)
plt.scatter(x, y)

plt.subplot(2, 5, 7)
vals = sobol_seq.i4_sobol_generate(2, 500)
vals[:,0]
vals[:,1]
plt.scatter(x=vals[:,0], y=vals[:,1])
コード例 #16
0
    def __init__(self, p, kind="random", grid_source="mat"):
        m = p.grid_size
        σ = np.array([p.σηR, p.σηa, p.σηL, p.σηu, p.σηB, p.σηG])
        ρ = np.array([p.ρηR, p.ρηa, p.ρηL, p.ρηu, p.ρηB, p.ρηG])

        if kind == "sobol":
            if grid_source == "mat":
                _path = os.path.join(DIR, "Sobol_grids.mat")
                s = loadmat(_path)["Sobol_grids"][:m, :]
            else:
                s = sobol_seq.i4_sobol_generate(8, m)
            sη = s[:, :6]
            η = (-2*σ + 4*(sη.max(0)-sη) / (sη.max(0)-sη.min(0))*σ)/np.sqrt(1-ρ**2)
            R = 1+0.05*(np.max(s[:, 6])-s[:, 6])/(np.max(s[:, 6])-np.min(s[:, 6]))
            δ = 0.95+0.05*(np.max(s[:, 7])-s[:, 7])/(np.max(s[:, 7])-np.min(s[:, 7]))
        else:
            # Values of exogenous state variables are distributed uniformly
            # in the interval +/- std/sqrt(1-rho_nu**2)
            if grid_source == "mat":
                _path = os.path.join(DIR, "random_grids.mat")
                s = loadmat(_path)["random_grids"][:m, :]
            else:
                s = np.random.rand(m, 8)
            sη = s[:, :6]
            η = (-2*σ + 4*σ*sη) / np.sqrt(1-ρ**2)

            # Values of endogenous state variables are distributed uniformly
            # in the intervals [1 1.05] and [0.95 1], respectively
            R = 1 + 0.05 * s[:, 6]
            δ = 0.95 + 0.05 * s[:, 7]

        ηR = η[:, 0]
        ηa = η[:, 1]
        ηL = η[:, 2]
        ηu = η[:, 3]
        ηB = η[:, 4]
        ηG = η[:, 5]

        self.ηR = ηR
        self.ηa = ηa
        self.ηL = ηL
        self.ηu = ηu
        self.ηB = ηB
        self.ηG = ηG
        self.R = R
        self.δ = δ

        # shape (8, m)
        self.X = np.vstack([np.log(R), np.log(δ), η.T])

        # shape (n_complete(8, p.Degree), m)
        self.X0_G = {
            1: complete_polynomial(self.X, 1),
            p.degree: complete_polynomial(self.X, p.degree)
        }

        # shape (2*n=12, n=6)
        self.ϵ_nodes, self.ω_nodes = qnwmonomial1(p.vcov)

        # all shape (len(ϵ_nodes), m)
        self.ηR1 = p.ρηR * ηR[None, :] + self.ϵ_nodes[:, None, 0]
        self.ηa1 = p.ρηa * ηa[None, :] + self.ϵ_nodes[:, None, 1]
        self.ηL1 = p.ρηL * ηL[None, :] + self.ϵ_nodes[:, None, 2]
        self.ηu1 = p.ρηu * ηu[None, :] + self.ϵ_nodes[:, None, 3]
        self.ηB1 = p.ρηB * ηB[None, :] + self.ϵ_nodes[:, None, 4]
        self.ηG1 = p.ρηG * ηG[None, :] + self.ϵ_nodes[:, None, 5]
コード例 #17
0
    def generate_dataset(self, metabolite_names, n_samples, overwrite=False, checkpoint=None, save_dir=None):
        if n_samples <= 0:
            raise Exception('n_samples must be greater than 0! (%d)' % (n_samples))

        if save_dir is not None and checkpoint is None:
            raise Exception('Save directory has been set, but checkpoint has not! Please set it. ')

        if checkpoint is not None and save_dir is None:
            raise Exception('If you are setting a checkpoint for saving, you must specisify where you want them to '
                            'be saved (directory=None)')
        if len(self.spectra):
            if overwrite:
                print('Overwriting current dataset, as overwrite set to true.')
                self.spectra = []
            else:
                raise Exception('This dataset already has spectra associated with it, either set overwrite=True or '
                                'make a new dataset with the same basis for a different config.')

        n_metabolites = len(metabolite_names)
        for spectra in self.basis.spectra:
            if len(spectra.metabolite_names) > 1:
                raise Exception(
                    'Spectra in basis set cannot have more than one metabolite in if you want to generate a dataset.')

            if np.min(spectra.nu()) > self.high_ppm:
                raise Exception('Spectra does not reach the required max frequency axis (%.2f) for export: %.2f' % (np.min(spectra.nu()), self.high_ppm))
            elif np.max(spectra.nu()) < self.low_ppm:
                raise Exception('Spectra does not reach the required max frequency axis (%.2f) for export: %.2f' % (np.max(spectra.nu()), self.low_ppm))

        if n_metabolites > len(self.basis.spectra):
            raise Exception('Number of metabolite names is greater than the number of spectra in the basis.')
        if len(metabolite_names) != len(set(metabolite_names)):
            raise Exception('At least one metabolite name appears more than once in the list: ' + str(metabolite_names))

        # filter the spectra to make sure it only contains metabolites in the metabolite names list, or a
        # combination of them any additional metabolites are removed
        spectra_to_keep = []
        for ii in range(0, len(self.basis.spectra)):
            if len(self.basis.spectra[ii].metabolite_names) <= len(metabolite_names):
                # checks if every metabolite name in the spectra is also in the metabolite_names list
                if all([smn in metabolite_names for smn in self.basis.spectra[ii].metabolite_names]):
                    spectra_to_keep.append(self.basis.spectra[ii])
        if not len(spectra_to_keep):
            raise Exception('No spectra selected to keep!')

        self.basis.spectra = spectra_to_keep
        self.basis.setup()

        # now we've prepped the basis set time to set up the dataset.
        self.linewidth = self.basis.spectra[0].linewidth

        if self.conc_gen_method == 'random all':
            # verstion where all metabolites can be excited
            concentrations = np.random.ranf((n_samples, n_metabolites))
            concentrations = (concentrations.T / np.sum(concentrations, 1)).T
        elif self.conc_gen_method == 'random partial':
            # version where not all metabolites are excited
            concentrations = np.zeros((n_samples, n_metabolites))
            # decide how many metabolites will be excited for each concentraion
            n_excited_samples = np.random.randint(1, n_metabolites + 1, size=n_samples)
            # concentrations for each of the aformentioned metabolites
            excited_samples = [sorted(np.random.choice(range(n_metabolites), n_s, replace=False)) for n_s in
                               n_excited_samples]

            for c, e in zip(concentrations, excited_samples):
                c[e] = np.random.ranf(len(e))
                # normalise the concentration to be a percentage for each (softmax output from CNN) (sum = 1)
            concentrations = (concentrations.T / np.sum(concentrations, 1)).T
        elif self.conc_gen_method == 'random uniform':
            # version where there is uniform sampling across the number of excited metabolites
            concentrations = np.zeros((n_samples, n_metabolites))
            n_samples_per_div = int(np.ceil(n_samples / float(n_metabolites)))
            samples = []
            for n_excited_metabolites in range(1, n_metabolites + 1):
                samples.extend(
                    [sorted(np.random.choice(range(n_metabolites), n_excited_metabolites, replace=False)) for _ in
                     range(n_samples_per_div)])
            for ii, e in zip(range(n_samples), samples):
                concentrations[ii][e] = np.random.ranf(len(e))
        elif self.conc_gen_method == 'sobol':
            concentrations = sobol_seq.i4_sobol_generate(n_metabolites, n_samples)
        else:
            raise Exception('Unknown concentration generation method: ' + self.conc_gen_method)

        for count in range(n_samples):
            # then export the combination and add it to the dataset
            self.add_spectra(self.basis.export_combination(concentrations[count],
                                                           metabolite_names,
                                                           acquisitions=self.export_acquisitions))
            # checkpointing for saving large dataset objects, if specified
            if checkpoint is not None and count > 0:
                if (count % checkpoint == 0) or (count == (n_samples - 1)):
                    self.check()
                    spectra, labels, export_labels = self.export_to_keras()
                    self.save_compressed(save_dir, spectra, labels)
                    self.spectra = []

        if checkpoint is None:
            self.check()
        else:
            return self.get_save_folder_name()
コード例 #18
0
            pt) * distToAxis(pt)**2

sobolCumulativeInertias = []
# instead of bins, we can try a low discrepancy sequence like the sobol sequence, which I think
# was made for applications like this one
sobolTrials = 20
for j in range(1, sobolTrials):
    print('sobol trial ' + str(j))
    # the sobel sequence returns a deterministic list of numbers, thus to test convergence we must
    # vary the number of sampling points
    subSampleSize = j**3 * samplesPerBin
    # recalculated as above
    sampleVolume = (2 * r)**3 / subSampleSize

    # generate the first "subSampleSize" numbers of the sobel sequence in R^3
    seq = sobol_seq.i4_sobol_generate(3, subSampleSize)
    # check out sobel_projections.py. In general, the projections onto an arbitrary plane is not garunteed
    # to be good at all... Thus we will just rotate our sobol sequence to line up with the axis (of moment of inertia)

    v1 = np.array([1, 0, 0])
    v2 = np.array([0, 1, 0])
    normal = np.array([
        axis[1][0] - axis[0][0], axis[1][1] - axis[0][1],
        axis[1][2] - axis[0][2]
    ])
    nonParallel = v2 if angle(v1, normal) < angle(v2, normal) else v1

    perp1 = np.cross(normal, nonParallel)
    perp2 = np.cross(normal, perp1)

    perp3 = normalize(normal)
コード例 #19
0
def qmc_driver() -> None:
    """A driver for quasi-Monte Carlo Uncertainty Quantification.

    This component attaches to a collection of model instances, and
    feeds in different parameter values generated using a Sobol
    sequence.
    """
    instance = Instance({
        Operator.O_I: ['parameters_out[]'],
        Operator.S: ['states_in[]']
    })

    while instance.reuse_instance():
        # F_INIT
        # get and check parameter distributions
        n_samples = instance.get_setting('n_samples', 'int')
        d_min = instance.get_setting('d_min', 'float')
        d_max = instance.get_setting('d_max', 'float')
        k_min = instance.get_setting('k_min', 'float')
        k_max = instance.get_setting('k_max', 'float')

        if d_max < d_min:
            instance.error_shutdown('Invalid settings: d_max < d_min')
            exit(1)
        if k_max < k_min:
            instance.error_shutdown('Invalid settings: k_max < k_min')
            exit(1)

        # generate UQ parameter values
        sobol_sqn = sobol_seq.i4_sobol_generate(2, n_samples)
        ds = d_min + sobol_sqn[:, 0] * (d_max - d_min)
        ks = k_min + sobol_sqn[:, 1] * (k_max - k_min)

        # configure output port
        if not instance.is_resizable('parameters_out'):
            instance.error_shutdown(
                'This component needs a resizable'
                ' parameters_out port, but it is connected to'
                ' something that cannot be resized. Maybe try'
                ' adding a load balancer.')
            exit(1)

        instance.set_port_length('parameters_out', n_samples)

        # run ensemble
        Us = None
        # O_I
        for sample in range(n_samples):
            uq_parameters = Settings({'d': ds[sample], 'k': ks[sample]})
            msg = Message(0.0, None, uq_parameters)
            instance.send('parameters_out', msg, sample)

        # S
        for sample in range(n_samples):
            msg = instance.receive_with_settings('states_in', sample)
            U = np.array(msg.data)
            # accumulate
            if Us is None:
                Us = U
            else:
                Us = np.vstack((Us, U))

        mean = np.mean(Us, axis=0)
        plt.figure()
        plt.imshow(np.log(Us + 1e-20))
        plt.show()
コード例 #20
0
# -*- coding: utf-8 -*-
"""
Created on Thu Jan 24 19:36:54 2019

@author: Christof
"""

import numpy as np
import sobol_seq
import matplotlib.pyplot as plt

random = np.random.rand(1024, 3)
sobol = sobol_seq.i4_sobol_generate(3, 1024)

print(random.shape)
print(sobol.shape)

rand_dist = plt.figure()
plt.title('Example 3D Distribution of Random Points')
plt.subplots_adjust(wspace=0, hspace=0)
for i in range(3):
    plt.subplot(3, 3, i+1)
    plt.plot(random[:, 0], random[:, i], 'b,')
    plt.tick_params(axis='both', which='both',
                    bottom=False, top=False, right=False, left=False,
                    labelbottom=False, labeltop=False, labelright= False,
                    labelleft=False)
    plt.subplot(3, 3, i+4)
    plt.plot(random[:, 1], random[:, i], 'g,')
    plt.tick_params(axis='both', which='both',
                    bottom=False, top=False, right=False, left=False,
コード例 #21
0
import importance
import controlVariates

# Parametros de f(x)
alpha = 0.534548064
beta = 0.9350011

gReal = 0.6804991098  # Valor real da integral. Nao usado no criterio de parada,
# apenas na verificacao posterior do erro relativo.

name = ["CRUDE", "HIT OR MISS", "IMPORTANCE SAMPLING", "CONTROL VARIATES"]
g = [0, 0, 0, 0]
n = [0, 0, 0, 0]

# Sequencia de Sobol uniforme em 2 dimensoes
sobolSeq = sobol_seq.i4_sobol_generate(2, 25000)
# Sequencia de Sobol com distribuicao Beta
a = 1
b = 1.5
distribution = chaospy.Beta(a, b)
betaSeq = distribution.sample(25000, 'S')

# Recebe o valor esperado de gamma e o numero de iteracoes por metodo
g[0], n[0] = crude.run(alpha, beta, sobolSeq)
g[1], n[1] = hitOrMiss.run(alpha, beta, sobolSeq)
g[2], n[2] = importance.run(alpha, beta, betaSeq)
g[3], n[3] = controlVariates.run(alpha, beta, sobolSeq)

for i in range(len(g)):
    print(name[i])
    print("Estimativa de gamma: " + str(g[i]))
コード例 #22
0
ファイル: tiktak.py プロジェクト: egorkozlov/py_shotgun
def tiktak(*,
           N,
           N_st,
           xfix=None,
           skip_global=False,
           skip_local=False,
           resume_global=False,
           resume_local=False):

    xl, xu, x0, keys, translator = calibration_params(xfix=xfix)
    #Initial cheks
    assert len(xl) == len(xu)

    assert N >= N_st

    x0 = np.array(x0)

    ############################################
    #1 INITIALIZATION
    ###########################################

    if not skip_global:
        #First Create a Sobol Sequence
        init = sobol_seq.i4_sobol_generate(
            len(xl), N)  # generate many draws from uniform
        #init=init[:,0]

        #Get point on the grid
        x_init = xl * (1 - init) + xu * init
        x_init = x_init.T
        x_init = x_init.squeeze()

        #Get fitness of initial points

        pts = [('compute', translator(x_init[:, j])) for j in range(N)]
        fx_init = compute_for_values(pts, resume=resume_global)

        fx_init = (np.array(fx_init)).squeeze()
        # !! not the optimizer returns squared value of mdl_resid

        #Sort in ascending order of fitness
        order = np.argsort(fx_init)
        fx_init = fx_init[order]
        x_init = x_init[:, order]

        filer('sobol_results.pkl', (fx_init, x_init), True)
        print('saved the results succesfully')
    else:
        (fx_init, x_init0) = filer('sobol_results.pkl', None, False)

        # this block appends variables if too little are specified in sobol_results
        x_init = np.zeros((x0.size, x_init0.shape[1]), dtype=x_init0.dtype)
        x_init[:, :] = x0[:, None]
        x_init[0:(x_init0.shape[0]), :] = x_init0

        print('loaded the results from the file')

    #Take only the first N_st realization
    fx_init = fx_init[0:N_st]
    x_init = x_init[:, 0:N_st]

    if skip_local:
        print('local minimizers are skipped')
        return x_init[:, 0]

    #Create a file with sobol sequence points
    filer('sobol.pkl', x_init, True)

    if not resume_local:
        #List containing parameters and save them in file
        param = list([(fx_init[0], x_init[:, 0])])
        filer('wisdom.pkl', param, True)
        i_start = 0
    else:
        param = filer('wisdom.pkl', None, False)
        i_start = len(param)

    vals = [('minimize', (i, N_st, xfix)) for i in range(i_start, N_st)]

    compute_for_values(vals, timeout=7200.0)

    param = filer('wisdom.pkl', None, write=False)

    ############################################
    #3 TOPPING RULE
    ###########################################
    #print(999,ite)
    #Final Refinement

    return param[0]
コード例 #23
0
ファイル: v2ncjde.py プロジェクト: krowck/Mestrado
    def diferentialEvolution(self, pop_size, dim, max_iterations, runs, func, f, nfunc, maximize=True):

        crowding_target = 0
        neighborhood_list = []
        funcs = ["haha", "five_uneven_peak_trap", "equal_maxima", "uneven_decreasing_maxima", "himmelblau", "six_hump_camel_back", "shubert", "vincent", "shubert", "vincent", "modified_rastrigin_all", "CF1", "CF2", "CF3", "CF3", "CF4", "CF3", "CF4", "CF3", "CF4", "CF4"]
        #print(">>>>>>>>>>", str(funcs[1]))
        m = 0
        PR = [] #PEAK RATIO
        SR = 0.0
        #generate execution identifier
        #uid = uuid.uuid4()
        hora = strftime("%Hh%Mm%S", localtime())
        mkdir(str(funcs[nfunc]) + '_' + str(dim) + 'D_' + str(hora))
        mkdir(str(funcs[nfunc]) + '_' + str(dim) + 'D_' + str(hora) +'/graphs')
        #to record the results
        results = open(str(funcs[nfunc]) + '_' + str(dim) + 'D_' + str(hora) + '/results.txt', 'a')
        records = open(str(funcs[nfunc]) + '_' + str(dim) + 'D_' + str(hora) + '/records.txt', 'a')
        results.write('ID: %s\tDate: %s\tRuns: %s\n' % (str(funcs[nfunc] ), strftime("%Y-%m-%d %H:%M:%S", gmtime()), str(runs)))
        results.write('=================================================================================================================\n')
        records.write('ID: %s\tDate: %s\tRuns: %s\n' % (str(funcs[nfunc] ), strftime("%Y-%m-%d %H:%M:%S", gmtime()), str(runs)))
        records.write('=================================================================================================================\n')
        avr_fbest_r = []
        avr_diversity_r = []
        fbest_r = []
        best_r = []
        elapTime_r = []
        
        #runs
        for r in range(runs):
            count_global = 0.0
            elapTime = []
            start = time()
            records.write('Run: %i\n' % r)
            records.write('Iter\tGbest\tAvrFit\tDiver\tETime\t\n')
            
            #start the algorithm
            best = [] #global best positions
            fbest = 0.00
                    
            #global best fitness
            if maximize == True:
                fbest = 0.00
            else:
                fbest = math.inf

            sobol = ss.i4_sobol_generate(dim, pop_size)
            #initial_generations
            self.generatePopulation(pop_size, dim, f, sobol)
            #fpop = f.evaluate
            fpop = self.evaluatePopulation(func, f)

            for ind in range(pop_size):                
                self.euclidean_distance_full(self.pop[ind], ind, dim)



            fbest,best = self.getBestSolution(maximize, fpop)
            #evolution_step
            # generates crossover rate values
            crm = 0.5
            Fl = 0.1
            Fu = 0.9
            tau1 = tau2 = 0.1
            crossover_rate = [gauss(crm, 0.1) for i in range(pop_size)]
            mutation_rate = [0.5] * pop_size
            cr_list = []
            for iteration in range(max_iterations):
                print(iteration)
                if pop_size <= 200:
                    m=math.floor(5+20*((max_iterations-iteration)/max_iterations))
                else:
                    m=math.floor(5+20*((max_iterations-iteration)/max_iterations))
                avrFit = 0.00 
                # #update_solutions
                strategy = 0
                #print(mutation_rate)
                #print(crossover_rate)
                #sleep(5)
                for ind in range(0,len(self.pop)):

                    rand1 = uniform(0, 1)
                    rand2 = uniform(0, 1)
                    rand3 = uniform(0, 1)
                    rand4 = uniform(0, 1)

                    if rand2 < tau1:
                        mutation_rate[ind] = Fl + (rand1 * Fu)
                    

                    if rand4 < tau2:
                        crossover_rate[ind] = rand3
                    
                    # generate weight factor values
                    weight_factor = gauss(0.5, 0.3)
                    #weight_factor = 0.5
                    #crossover_rate[ind] = 0.1
                    if uniform(0,1) < 1:
                        neighborhood_list = self.generate_neighborhood(ind, m, dim, f)
                        candSol = self.rand_1_bin(self.pop[ind], ind, dim, mutation_rate[ind], crossover_rate[ind], neighborhood_list, m)
                        #candSol = self.currentToRand_1_bin(self.pop[ind], ind, dim, mutation_rate[ind], crossover_rate[ind], neighborhood_list, m)
                    
                    self.boundsRes(candSol, f, dim)

                    fcandSol = f.evaluate(candSol)

                    dist, crowding_target = self.euclidean_distance(candSol, ind, dim, f, fpop)

                    if maximize == True:
                        if fcandSol >= fpop[crowding_target]:
                            self.pop[crowding_target] = candSol
                            dist_correta, aux = self.euclidean_distance(candSol, crowding_target, dim, f, fpop)
                            self.full_euclidean[crowding_target] = dist_correta
                            fpop[crowding_target] = fcandSol
 
                    avrFit += fpop[crowding_target]
                avrFit = avrFit/pop_size
                self.diversity.append(0)
                

                fbest,best = self.getBestSolution(maximize, fpop)
                
                self.fbest_list.append(fbest)
                elapTime.append((time() - start)*1000.0)
                records.write('%i\t%.4f\t%.4f\t%.4f\t%.4f\n' % (iteration, round(fbest,4), round(avrFit,4), round(self.diversity[iteration],4), elapTime[iteration]))
                
                # if iteration%crPeriod == 0 and iteration!=0:
                #     crossover_rate = [gauss(crm, 0.1) for i in range(pop_size)]
                #     if iteration%crmUpdatePeriod == 0:
                #         crm = sum(cr_list)/len(cr_list)
                #         cr_list = []

            records.write('Pos: %s\n\n' % str(best))
            fbest_r.append(fbest)
            best_r.append(best)
            elapTime_r.append(elapTime[max_iterations-1])
            self.generateGraphs(self.fbest_list, self.diversity, max_iterations, funcs[nfunc], r, dim, hora)
            avr_fbest_r.append(self.fbest_list)
            avr_diversity_r.append(self.diversity)

            pop_aux = []
            pop_aux = self.pop
            count, seeds = how_many_goptima(self.pop, f, 0.001, len(self.pop), pop_aux)

            #print(count, seeds)
            count_global += count

            self.pop = []
            self.m_nmdf = 0.00 
            self.diversity = []
            self.fbest_list = []
            p1 = p2 = 0.5
            self.nf2 = 1
            self.ns1 = 1
            self.ns2 = 1
            self.nf1 = 1

            print("ENTROu")
            PR.append(count_global/f.get_no_goptima())
            print(PR[r])
            if(PR[r] == 1):
                SR += 1
        
        fbestAux = [sum(x)/len(x) for x in zip(*avr_fbest_r)]
        diversityAux = [sum(x)/len(x) for x in zip(*avr_diversity_r)]
        self.generateGraphs(fbestAux, diversityAux, max_iterations, funcs[nfunc], 'Overall', dim, hora)
        records.write('=================================================================================================================')
        if maximize==False:
            results.write('Gbest Overall: %.4f\n' % (min(fbest_r)))
            results.write('Positions: %s\n\n' % str(best_r[fbest_r.index(min(fbest_r))]))
        else:
            results.write('Tamanho da populacao: %d\n' % pop_size)
            results.write('Iteracoes Maximas: %d\n' % max_iterations)
            results.write('Funcao Otimizada: %s\n' % func)
            results.write('Gbest Overall: %.4f\n' % (max(fbest_r)))
            results.write('Positions: %s\n' % str(best_r[fbest_r.index(max(fbest_r))]))
            for i in range(0, runs):
                results.write('Mean Peaks Found on Run %d: %f (%f)\n' % (i, PR[i], (PR[i]*f.get_no_goptima())))
            if runs > 1:
                results.write('Mean Peaks Found: %.4f\n' % (sum(PR)/runs))
                results.write('Peak Ratio Standard Deviation: %.4f\n' % (stdev(PR)))
                results.write('[')
                for i in range(0, runs):
                    results.write('%.5f, ' % PR[i])
                results.write(']\n')            
            results.write('Success rate: %.4f\n\n' % (SR/runs))


        results.write('Gbest Average: %.4f\n' % (sum(fbest_r)/len(fbest_r)))
        results.write('Gbest Median: %.4f #probably should use median to represent due probably non-normal distribution (see Shapiro-Wilk normality test)\n' % (median(fbest_r)))
        if runs > 1:
            results.write('Gbest Standard Deviation: %.4f\n\n' % (stdev(fbest_r)))
        results.write('Elappsed Time Average: %.4f\n' % (sum(elapTime_r)/len(elapTime_r)))
        if runs > 1:
            results.write('Elappsed Time Standard Deviation: %.4f\n' % (stdev(elapTime_r)))
        results.write('=================================================================================================================\n')
コード例 #24
0
 def generate(self, dim_num, n):
     randoms = i4_sobol_generate(dim_num, n, skip=self.skip)
     self.skip += dim_num * n
     return randoms
コード例 #25
0
# -*- coding: utf-8 -*-
"""
Created on Thu Feb  6 19:45:14 2020

@author: Christof
"""

import numpy as np
import sobol_seq
import matplotlib.pyplot as plt
import scipy

random = np.random.rand(512, 2)
sobol = sobol_seq.i4_sobol_generate(2, 512)

print(random.shape)
print(sobol.shape)

rand_dist = plt.figure()
plt.title('Example 2D Distribution of Random Points')
plt.plot(random[:, 0], random[:, 1], 'bo', mfc='none')
plt.savefig('2D_Dist_Rand512.pdf', format='pdf')
plt.show()

sov_dist = plt.figure()
plt.title('Example 2D Distribution of Sobol Points')
plt.plot(sobol[:, 0], sobol[:, 1], 'bo', mfc='none')
plt.savefig('2D_Dist_Sobol512.pdf', format='pdf')
plt.show()
コード例 #26
0
    v2 = np.array([0, 1, 0])
    nonParallel = v2 if angle(v1, planeNormal) < angle(v2, planeNormal) else v1

    perp1 = np.cross(planeNormal, nonParallel)
    perp2 = np.cross(planeNormal, perp1)

    perp3 = normalize(planeNormal)
    perp1 = normalize(perp1)
    perp2 = normalize(perp2)

    return la.inv(np.column_stack([perp1, perp2, perp3]))


def planeCoord(pt, projMat):
    transformed = np.dot(projMat, np.column_stack([pt]))
    return [transformed[0][0], transformed[1][0]]


sobelPts = sobol_seq.i4_sobol_generate(3, 1000)
for k in range(7):
    normal = planeNormals[k]
    mat = projectMatrix(normal)
    pts = []
    for pt in sobelPts:
        if (pt[0] - .5)**2 + (pt[1] - .5)**2 + (pt[2] - .5)**2 < .5**2:
            pts.append(planeCoord(pt, mat))
    [xs, ys] = np.array(pts).transpose()
    plt.subplot(3, 3, k + 1)
    plt.plot(xs, ys, 'ro')
plt.show()
コード例 #27
0

from benchmark_functions import branin,Currin
functions=[branin,Currin]
d=2
referencePoint = [1e5]*len(functions)

total_iterations=100
seed=0
np.random.seed(seed)
intial_number=1
bound=[0,1]
sample_number=1

Fun_bounds = [bound]*d
grid = sobol_seq.i4_sobol_generate(d,1000,np.random.randint(0,100))
design_index = np.random.randint(0, grid.shape[0])

###################GP Initialisation##########################

GPs=[]
Multiplemes=[]
for i in range(len(functions)):
    GPs.append(GaussianProcess(d))
for k in range(intial_number):
    exist=True
    while exist:
        design_index = np.random.randint(0, grid.shape[0])
        x_rand=list(grid[design_index : (design_index + 1), :][0])
        if (any((x_rand == x).all() for x in GPs[0].xValues))==False:
            exist=False
コード例 #28
0
import numpy as np
import matplotlib.pyplot as plt
import sobol_seq
import scipy.stats

fig, ax = plt.subplots(nrows=3, ncols=5)

#Uniform Distribution

axes = plt.subplot(3,5,1)
N = 50
norm = sobol_seq.i4_sobol_generate(1, N)
axes.set_title('N=50')
axes.set_ylabel('Uniform')
plt.hist(norm, bins=N)

axes = plt.subplot(3,5,2)
N = 100
norm = sobol_seq.i4_sobol_generate(1, N)
axes.set_title('N=100')
plt.hist(norm, bins=N)

axes = plt.subplot(3,5,3)
N = 250
norm = sobol_seq.i4_sobol_generate(1, N)
axes.set_title('N=250')
plt.hist(norm, bins=N)

axes = plt.subplot(3,5,4)
N = 500
norm = sobol_seq.i4_sobol_generate(1, N)
コード例 #29
0
            scores = lines[1].split(",")
            MPE_train = float(scores[0])
            mean_train = mean_train + MPE_train**2

            if model_type == "net":
                model = Utils.load_data(new_path)
            else:
                model = joblib.load(new_path + "/model_GP.sav")
            minmax = pd.read_csv(new_path + "/MinMax.txt", header=0)
            min_val = minmax.Min.values[0]
            max_val = minmax.Max.values[0]

            if point == "random":
                preds = model.predict(np.random.uniform(0, 1, (1000, 7)))
            elif point == "sobol":
                preds = model.predict(sobol_seq.i4_sobol_generate(7, 1000))
            else:
                raise ValueError()
            std_app = np.std(preds * (max_val - min_val) + min_val)
            print("True STD: ", std)
            print("Appr STD: ", std_app)
            '''
            
            prod = 1
            for j in range(len(model.layers)):
                if model.layers[j].get_weights():
                    weight_matrix = model.layers[j].get_weights()[0]
                    # for k in range(weight_matrix.shape[1]):
                    #    print(weight_matrix[:,k])
                    #    print(sum(abs(weight_matrix[:,k])))
                    # print(weight_matrix.shape)
コード例 #30
0
def Quasi_Monte_Carlo_Simulation(no_of_trials, T, r, S_0, vol, K, q, m,
                                 batch):  #m是分几个interval
    def quasi_get_Gaussian(u1, u2):
        output_u1 = math.sqrt(-2.0 * math.log(u1)) * math.cos(
            6.283185307999998 * u2)
        output_u2 = math.sqrt(-2.0 * math.log(u1)) * math.sin(
            6.283185307999998 * u2)
        return [output_u1, output_u2]

    itval = T / m

    quasi_mean = quasi_mean_square = 0
    option_price_list = []

    for _ in range(batch):

        sobel = sobol_seq.i4_sobol_generate(m, no_of_trials)
        #生成一个shape 为  no_of_trials * m 的np.array()

        random_var = np.zeros((no_of_trials, m))
        #生一个numpy array, shape 为no_of_trials * m

        sim_option_price_sum = 0.0

        for i in range(no_of_trials):
            #print( random_var.shape, sobel.shape,len(random_var[i]), len(sobel[:,i]))
            random_var[i] = sobel[i] + np.random.uniform(
                0, 1, m)  #把sobel 所有第i行数据加上一个 uniform variable
            random_var[i] = random_var[i] - np.floor(
                random_var[i])  #让 所有第i行数据 都小于0
            #np.floor([2.8,1.2]) = np.array([2,1])
            random_var[i, 0:2] = quasi_get_Gaussian(*random_var[i, 0:2])
            # * 号表示unload list 元素到variable, 也可以写成quasi_get_Gaussian(random_var[i,0],random_var[i,1] )

            average = price = S_0 * math.exp(
                (r - q - 0.5 * vol**2) * itval +
                vol * random_var[i, 0] * itval**0.5)

            for j in range(m - 1):

                if j % 2 == 1:
                    random_var[i, j + 1:j + 3] = quasi_get_Gaussian(
                        *random_var[i, j + 1:j + 3])

                price = price * math.exp((r - q - 0.5 * vol**2) * itval + vol *
                                         random_var[i, j + 1] * itval**0.5)
                average += price

            average = average / m
            simulated_option_price = math.exp(-r * T) * max(average - K, 0)
            sim_option_price_sum += simulated_option_price
            option_price_list.append(average)

        option_price = average / no_of_trials
        quasi_mean += option_price
        quasi_mean_square += option_price * option_price

    quasi_mean = quasi_mean / batch
    quasi_mean_square = quasi_mean_square / batch
    option_price_list = np.array(option_price_list)

    std_err = math.sqrt(1 / batch * np.sum(
        (option_price_list - quasi_mean) * 2) / (batch - 1))
    #把list中每个option price 减去quasi_mean 再平方

    upper_bound = quasi_mean + 1.96 * std_err
    lower_bound = quasi_mean - 1.96 * std_err

    print(
        "Quasi Monte Carlo option price = {0}, standard_error = {1},  Confidence Interval = [{2:.3f}, {3:.3f}] "
        .format(option_price, std_err, upper_bound, lower_bound))
コード例 #31
0
# 1st row 4th plot
axes[0, 3].scatter(np.random.rand(d), np.random.rand(d), color = 'black', s = 1)
axes[0, 3].set_aspect('equal', 'box')
axes[0, 3].set_xlabel("N = " + str(d))
axes[0, 3].xaxis.set_label_position('top')

# 1st row 5th plot
axes[0, 4].scatter(np.random.rand(e), np.random.rand(e), color = 'black', s = 1)
axes[0, 4].set_aspect('equal', 'box')
axes[0, 4].set_xlabel("N = " + str(e))
axes[0, 4].xaxis.set_label_position('top')


# Generating Quasi-Random numbers
soba = ss.i4_sobol_generate(2, a)
sobb = ss.i4_sobol_generate(2, b)
sobc = ss.i4_sobol_generate(2, c)
sobd = ss.i4_sobol_generate(2, d)
sobe = ss.i4_sobol_generate(2, e)

# 2nd row 1st plot
axes[1, 0].scatter(soba[:, 0], soba[:, 1], color = 'black', s = 1)
axes[1, 0].set_aspect('equal', 'box')
axes[1, 0].set_ylabel("Quasi-Random")

# 2nd row 2nd plot
axes[1, 1].scatter(sobb[:, 0], sobb[:, 1], color = 'black', s = 1)
axes[1, 1].set_aspect('equal', 'box')

# 2nd row 3rd plot
コード例 #32
0
    def __init__(self,nogrid=False,**kwargs): 
        p = dict()        
        p['T']         = 2
        p['sig_zf_0']  = 0.15
        p['sig_zf']    = 0.05
        p['n_zf']      = 3
        p['sig_zm_0']  = 0.15#0.2
        p['sig_zm']    = 0.05#0.075
        p['n_zm']      = 3
        p['sigma_psi_init'] = 0.12
        p['sigma_psi']   = 0.03
        p['n_psi']     = 7
        p['beta'] = 0.95
        p['A'] = 1.2 # consumption in couple: c = (1/A)*[c_f^(1+rho) + c_m^(1+rho)]^(1/(1+rho))
        p['crra_power'] = 1.5
        p['couple_rts'] = 0.0       
        p['sig_partner_a'] = 0.0#0.1
        p['sig_partner_z'] = 0.2
        p['m_bargaining_weight'] = 0.5
        
        
        for key, value in kwargs.items():
            assert (key in p), 'wrong name?'
            p[key] = value
        
        
        p['nexo'] = p['n_zf']*p['n_zm']*p['n_psi']
        self.pars = p
        
        p_int = dict()
        
        
        # relevant for integration (we do not do integration here)
        
        # this one is for (a_part,z_part,psi_couple)
        p_int['num_partners'] = 5
        p_int['nodes_couple'] = norm.ppf(sobol_seq.i4_sobol_generate(3,p_int['num_partners']))
        p_int['num_z_nodes'] = 7
        p_int['z_nodes'] = norm.ppf(sobol_seq.i4_sobol_generate(1,p_int['num_z_nodes']))
        p_int['large_3dim'] = norm.ppf(sobol_seq.i4_sobol_generate(3,60)) # generate many draws from normal
        
        #Fabio modification
        p_int['zf']=np.random.normal(0, 1, 100)
        p_int['zm']=np.random.normal(0, 1, 100)
        p_int['psi']=np.random.normal(0, 1, 100)
        self.integration = p_int
        
        if not nogrid:
        
            exogrid = dict()
            
            
            # let's approximate three Markov chains
            # this sets up exogenous grid
            exogrid['zf_t'],  exogrid['zf_t_mat'] = rouw_nonst(p['T'],p['sig_zf'],p['sig_zf_0'],p['n_zf'])
            exogrid['zm_t'],  exogrid['zm_t_mat'] = rouw_nonst(p['T'],p['sig_zm'],p['sig_zm_0'],p['n_zm'])
            exogrid['psi_t'], exogrid['psi_t_mat'] = rouw_nonst(p['T'],p['sigma_psi'],p['sigma_psi_init'],p['n_psi'])
            
            zfzm, zfzmmat = combine_matrices_two_lists(exogrid['zf_t'], exogrid['zm_t'], exogrid['zf_t_mat'], exogrid['zm_t_mat'])
            exogrid['all_t'], exogrid['all_t_mat'] = combine_matrices_two_lists(zfzm,exogrid['psi_t'],zfzmmat,exogrid['psi_t_mat'])
            exogrid['all_t_mat_sparse_T'] = [sparse.csc_matrix(D.T) if D is not None else None for D in exogrid['all_t_mat']]
            
            
            Exogrid_nt = namedtuple('Exogrid_nt',exogrid.keys())
            
            self.nexo = p['nexo']
            self.exogrid = Exogrid_nt(**exogrid)


        self.na = 60
        self.amin = 0
        self.amax = 1.5
        self.agrid = np.linspace(self.amin,self.amax,self.na)

        # grid for theta
        self.ntheta = 71
        self.thetamin = 0.0001
        self.thetamax = 0.9999
        self.thetagrid = np.linspace(self.thetamin,self.thetamax,self.ntheta)
コード例 #33
0
ファイル: TaskWeek6.py プロジェクト: finartist/CG1
def getStratifiedPointsH2(n, num_stratas, phi = False, theta = True, withLowDis = False):

    num_stratas = np.int32(num_stratas)
    samplesPerStrata = np.int32(n/num_stratas)
        
    omega = np.zeros([n, 2])
    
    if (withLowDis):
        num_stratasPhi = 8
        num_stratasTheta = np.int32(num_stratas/num_stratasPhi)
        
        index = 0
        sobolpoints = sobol_seq.i4_sobol_generate(2, samplesPerStrata)
        
        #slice sphere in theta and phi
        for i in range(num_stratasPhi):
            for j in range(num_stratasTheta):
                rand = sobolpoints.copy()
                rand[:, 0] = np.arccos(rand[:, 0]/num_stratasTheta + j/num_stratasTheta)
                rand[:, 1] = (rand[:,1]/num_stratasPhi + i/num_stratasPhi) * 2 * np.pi
                omega[index*samplesPerStrata:(index+1)*samplesPerStrata] = rand
                index += 1
                         
        return omega
    
    if (phi and theta):
        num_stratasPhi = 8
        num_stratasTheta = np.int32(num_stratas/num_stratasPhi)
        
        index = 0
        
        #slice sphere in theta and phi
        for i in range(num_stratasPhi):
            for j in range(num_stratasTheta):
                for k in range(samplesPerStrata):
                    rand = np.random.rand(2)
                    rand[0] = np.arccos(rand[0]/num_stratasTheta + j/num_stratasTheta)
                    rand[1] = rand[1]/num_stratasPhi + i/num_stratasPhi
                    omega[index, 0] = rand[0]
                    omega[index, 1] = rand[1] * 2 * np.pi
                    index += 1
                         
        return omega
    
    elif (phi):
        #sphere in num_stratas slices in phi direction
        for i in range(num_stratas):
            for j in range(samplesPerStrata):
                rand = np.random.rand(2)
                rand[1] = rand[1]/num_stratas + i/num_stratas
                omega[i*samplesPerStrata+j, 0] = np.arccos(rand[0])
                omega[i*samplesPerStrata+j, 1] = rand[1] * 2 * np.pi
                         
        return omega
    
    else:
        #sphere in num_stratas slices in theta direction
        for i in range(num_stratas):
            for j in range(samplesPerStrata):
                rand = np.random.rand(2)
                omega[i*samplesPerStrata+j, 0] = np.arccos(rand[0]/num_stratas + i/num_stratas)
                omega[i*samplesPerStrata+j, 1] = rand[1] * 2 * np.pi
                     
        return omega
コード例 #34
0
        #filename_list = ["Midline_40_0.csv","Midline_40_1.csv","Midline_40_2.csv","Midline_40_3.csv","Midline_40_4.csv","Midline_40_5.csv"]

        filename_list = ["Midline_40_0.csv"]
		
		
    elif myrads == 80:
        #removing _80_0 because of unusual yawrate changes.
        #removing _80_1 because the balanced portion of the experiment uses it.
        filename_list = ["Midline_80_2.csv","Midline_80_3.csv","Midline_80_4.csv","Midline_80_5.csv"]

        
    else:
        raise Exception('Unrecognised radius')

    Trials = 24
    sobol = sobol_seq.i4_sobol_generate(4, Trials) # 0,1 scale

    #print(sobol_3D)

    #rescale
    onset_sobol = sobol[:,2] * 4 + 5
    autofile_sobol = np.round(sobol[:,1] * 3,0) 

    ttlc_limit = 2
    ttlc_stay = 18

    ttlc_sobol = sobol[:,0] * (ttlc_stay-ttlc_limit) + ttlc_limit
    steer_sobol = sobol[:,2] #flag for understeering or oversteering
    
    #***** retrieve approximations of SAB ******
        
コード例 #35
0
def generate_sobol_seq(dim,nSobol):
    mysobol_seq = sobol_seq.i4_sobol_generate(dim, nSobol)
    return mysobol_seq
コード例 #36
0
ファイル: sobol_test.py プロジェクト: callummole/SP_18-19
import numpy as np
import sobol_seq
import matplotlib.pyplot as plt

#### SOBOL RANDOMISATION #######

#3 dimensional sobol sequence: SAB, Onset Time, Trajs
Trials = 30
sobol_3D = sobol_seq.i4_sobol_generate(3, 30)  # 0,1 scale

print(sobol_3D)

#onset time pool to range between 5-9.

#autofile as integers 0-3.

#sab from -5 - +5

#OnsetTimePool_sobel = np.arange(5, 9.25, step = .25)
#OnsetTimePool = OnsetTimePool[OnsetTimePool[] != 6]

onset_sobol = sobol_3D[:, 0] * 4 + 5
autofile_sobol = np.round(sobol_3D[:, 1] * 3, 0)
sab_sobol = sobol_3D[:, 2] * 10 - 5

plt.figure(2)
plt.plot(sab_sobol, autofile_sobol, 'k.', markersize=5, alpha=.2)
plt.xlabel("Steering Angle Biases (deg/s)")
plt.ylabel("Onset Times (s)")
plt.title("Sobol samples")
plt.show()