Пример #1
0
    def placeObjective(self, objective, x, y):
        if objective != "enemy":
            if not self.contains(objective):

                o = Objective(objective, x, y)
                self.objs.append(o)

            else:
                self.getObjective(objective).setPosition(x, y)

        else:
            o = Objective(objective, x, y)
            self.objs.append(o)
Пример #2
0
    def sample_points(self):
        """
        Main function, calls find_new_vector method for the required number of times
        """
        objective = Objective(self.n_dim, self.n_results)
        objective.add_point(np.array(self.example))

        addI = 0
        while addI < self.n_results - 1:
            if addI % 10 == 0:
                print addI
            new_vector, count_fail, best_val = self.find_new_vector(objective)
            if new_vector is not None:
                addI += 1
                objective.add_point(new_vector)

                # adds to total values for final reporting
                # sets min and max objective values for subsequent normalization
                objective.total_obj_distance += best_val
                objective.add_count_fail(count_fail)
                objective.set_max_objective(best_val)
                objective.set_min_objective(parameters.OBJECTIVE_METHOD)
            else:
                print "New vector not found...Consider parameter change if problem exists"

        print
        print "Total Count Fail ", objective.total_count_fail
        print "Total Objective Distance ", objective.total_obj_distance
        return objective.sample_points
Пример #3
0
 def load(self, lvl, stage):
     if (lvl == 1):
         if stage == 1:
             self.map_file = 'mapfilelvl1.1.txt'
         elif stage == 2:
             self.map_file = 'mapfilelvl1.2.txt'
         self.music_file = "music/gameplay.mod"
         self.cutscene = None
         self.objective = Objective(lvl, stage)
         self.character_pos_x = 100
         self.character_pos_y = 100
     elif (lvl == 2):
         if stage == 1:
             self.map_file = 'mapfilelvl2.1.txt'
         elif stage == 2:
             self.map_file = 'mapfilelvl2.2.txt'
         self.music_file = "music/gameplay2.mod"
         self.cutscene = None
         self.objective = Objective(lvl, stage)
         self.character_pos_x = 100
         self.character_pos_y = 100
     elif (lvl == 3):
         if stage == 1:
             self.map_file = 'mapfilelvl3.1.txt'
         elif stage == 2:
             self.map_file = 'mapfilelvl3.2.txt'
         self.music_file = "music/gameplay2.mod"
         self.cutscene = None
         self.objective = Objective(lvl, stage)
         self.character_pos_x = 100
         self.character_pos_y = 100
     elif (lvl == 4):
         if stage == 1:
             self.map_file = 'mapfile-lvl4-stg1.txt'
         elif stage == 2:
             self.map_file = 'mapfile-lvl4-stg2.txt'
         self.music_file = "music/gameplay3.mod"
         self.cutscene = None
         self.objective = Objective(lvl, stage)
         self.character_pos_x = 100
         self.character_pos_y = 100
     return self.map_file
Пример #4
0
def main():
    iris = load_iris()
    X = iris.data
    y = iris.target

    obj = Objective(X, y)

    study = optuna.create_study(direction='maximize')
    study.optimize(obj, n_trials=20)
    print(study.best_trial)
    print('DONE')
Пример #5
0
    def __init__(self, images="cifar10", labels=None, config=None):
        """Initializes DeepAugment object

        Does following steps:
            1. load and preprocess data
            2. create child model
            3. create controller
            4. create notebook (for recording trainings)
            5. do initial training
            6. create objective function
            7. evaluate objective function without augmentation

        Args:
            images (numpy.array/str): array with shape (n_images, dim1, dim2 , channel_size), or a string with name of keras-dataset (cifar10, fashion_mnsit)
            labels (numpy.array): labels of images, array with shape (n_images) where each element is an integer from 0 to number of classes
            config (dict): dictionary of configurations, for updating the default config which is:
                {
                    "model": "basiccnn",
                    "method": "bayesian_optimization",
                    "train_set_size": 2000,
                    "val_set_size": 100,
                    "opt_samples": 3,
                    "opt_last_n_epochs": 3,
                    "opt_initial_points": 10,
                    "child_epochs": 50,
                    "child_first_train_epochs": 0,
                    "child_batch_size": 64,
                    "pre_aug_weights_path": "pre_aug_weights.h5",
                    "logging": logging,
                    "notebook_path": f"{EXPERIMENT_FOLDER_PATH}/notebook.csv"
                }
        """
        self.config = DEFAULT_CONFIG
        if config!=None: self.config.update(config)
        self.iterated = 0  # keep tracks how many times optimizer iterated

        self._load_and_preprocess_data(images, labels)

        # define main objects
        self.child_model = ChildCNN(self.input_shape, self.num_classes, self.config)
        self.controller = Controller(self.config)
        self.notebook = Notebook(self.config)
        if self.config["child_first_train_epochs"] > 0:
            self._do_initial_training()
        self.child_model.save_pre_aug_weights()
        self.objective_func = Objective(
            self.data, self.child_model, self.notebook, self.config
        )

        #self._evaluate_objective_func_without_augmentation()
        self.trial_hyperparams = None
        self.trial_no = 0
Пример #6
0
def updateObjectives(game_map, opponent_ships):
    objs = []
    for p in planets:
        en_list = sorted(opponent_ships,
                         key=lambda x: p.calculate_distance_between(x))
        for i in range(len(en_list)):
            if p.calculate_distance_between(en_list[i]) > 40 - p.radius:
                break

        if not p.is_owned() and p.remaining_resources > 0:
            objs.append(Objective(p, 'dock_unowned'))
            objs[-1].addEnShip(en_list[:i])
        elif p.owner == me:
            objs.append(Objective(p, 'defend'))
            objs[-1].addEnShip(en_list[:i])
            if not p.is_full() and p.remaining_resources > 0:
                objs.append(Objective(p, 'dock_owned'))
                objs[-1].addEnShip(en_list[:i])
        elif p.owner != me and p.is_owned():
            objs.append(Objective(p, 'attack'))
            objs[-1].addEnShip(en_list[:i])

    return objs
Пример #7
0
    def fit_quasi_newton(self,
                         lamb_l2,
                         lamb_smth,
                         lamb_log,
                         w_init,
                         factr=1e7,
                         maxls=50,
                         m=10,
                         lb=-np.Inf,
                         ub=np.Inf,
                         maxiter=15000,
                         verbose=True):
        """
        """
        obj = Objective(self.sumstats, self.graph)
        obj.lamb_l2 = lamb_l2
        obj.lamb_smth = lamb_smth
        obj.lamb_log = lamb_log
        res = fmin_l_bfgs_b(func=loss_wrapper,
                            x0=np.log(w_init),
                            args=[obj],
                            factr=factr,
                            m=m,
                            maxls=maxls,
                            maxiter=maxiter,
                            approx_grad=False,
                            bounds=[(lb, ub)
                                    for _ in range(obj.graph.Delta.shape[1])])
        if maxiter >= 100:
            assert res[2]["warnflag"] == 0, "did not converge"
        self.graph.w = np.exp(res[0])

        # print update
        self.train_loss, _ = loss_wrapper(res[0], obj)
        self.train_nll = neg_log_lik_wrapper(res[0], obj)
        self.pen = self.train_loss - self.train_nll
        if verbose:
            sys.stdout.write(
                ("lambda_l2={:.7f}, "
                 "lambda={:.7f}, "
                 "alpha={:.7f}, "
                 "converged in {} iterations, "
                 "train_loss={:.7f}\n").format(lamb_l2, lamb_smth, lamb_log,
                                               res[2]["nit"], self.train_loss))
Пример #8
0
    def fit_w0_s2(self, verbose=True):
        """Fits a constant migration surface (no reg) and residual variance by maximum likelihood
        """
        obj = Objective(self.sumstats, self.graph)
        res = minimize(neg_log_lik_w0_s2, [0.0, 0.0],
                       method="Nelder-Mead",
                       args=(obj))
        assert res.success is True, "did not converge"
        w0_hat = np.exp(res.x[0])
        s2_hat = np.exp(res.x[1])
        self.graph.w0 = w0_hat * np.ones(self.graph.w.shape[0])
        self.sumstats.s2 = s2_hat
        self.sumstats.q = self.graph.n_samples_per_node / s2_hat
        self.sumstats.q_diag = sp.diags(self.sumstats.q).tocsc()
        self.sumstats.q_inv_diag = sp.diags(1. / self.sumstats.q).tocsc()

        # print update
        self.train_loss, _ = loss_wrapper(np.log(self.graph.w0), obj)
        if verbose:
            sys.stdout.write(
                ("constant-w/variance fit, "
                 "converged in {} iterations, "
                 "train_loss={:.7f}\n").format(res.nfev, self.train_loss))
Пример #9
0
from objective import Objective
from utils import get_data, to_original_order
from connectivity import DistanceConnectivity

if __name__ == '__main__':
    for k_instance in range(100):
        sellers, buyers, edges = get_data('data%d.txt'%k_instance, DistanceConnectivity(10))
        _, _, ori_edges = to_original_order(sellers, buyers, edges)
        alg = Objective(sellers, buyers, edges)

        for obj in ['urr', 'min_var', 'balance']:
            filename = 'output/' + obj + str(k_instance) + '.txt'
            prices = alg.get_prices(obj)
            solution = zip(ori_edges, zip(prices, alg.fs))
            file(filename, 'w').write(str(solution))
Пример #10
0
    def admm(self,
             n_iter,
             lamb_l2,
             lamb_smth,
             lamb_log,
             eta,
             w_init,
             rho=100.0,
             lb=1e-10,
             ub=1e10,
             eps=5 * 1.0e-3,
             n_print=1000):
        """Fits the feems model for a single regularzation parameters using admm
        """
        # m init
        self.graph.comp_lap(w_init)

        # opt params
        self.eta = eta
        self.rho = rho
        self.lb = lb
        self.ub = ub
        self.eps = eps

        # compute inital objective
        self.obj = Objective(self.sumstats, self.graph)
        self.obj.lamb_l2 = lamb_l2
        self.obj.lamb_smth = lamb_smth
        self.obj.lamb_log = lamb_log

        # compute inverse covariances and gradient of objective
        self.obj.inv()
        self.obj.grad()

        # admm variables
        z = np.zeros(self.obj.graph.Delta.shape[0])
        u = np.zeros(self.obj.graph.Delta.shape[0])

        ########## stopping criterion ##########
        # if self.opt_crit < self.eps terminate algorithm
        self.opt_crit = np.Inf
        self.converged = False

        # iterate
        self.losses = []
        self.losses.append(self.obj.loss())
        for i in range(n_iter):

            # variable updates
            # w update
            grad = self.obj.grad_obj * self.graph.w + self.rho * self.obj.graph.Delta.T @ \
                    (self.obj.graph.Delta @ (self.graph.w + self.obj.lamb_log * np.log(self.graph.w)) - z - u/rho) * \
                                                (self.obj.lamb_log + self.graph.w)
            self.graph.w = self._projection_onto_ranges(self.graph.w *
                                                        np.exp(-eta * grad))
            # if self.converged:
            # break

            # z update
            z = soft_thresh(self.obj.graph.Delta @ (self.graph.w + self.obj.lamb_log * np.log(self.graph.w)) \
                                                                                    - u/self.rho, self.obj.lamb_smth/self.rho)

            # u update
            u = u + self.rho * (z - self.obj.graph.Delta @ (
                self.graph.w + self.obj.lamb_log * np.log(self.graph.w)))

            # update graph
            self.graph.comp_lap(self.graph.w)
            # update objective
            self.obj.inv()
            self.obj.grad()

            loss = self.obj.neg_log_lik() + self.obj.lamb_l2/2 * np.linalg.norm(self.graph.w, 2)**2 \
                    + self.obj.lamb_smth * np.linalg.norm(self.obj.graph.Delta @ (self.graph.w + self.obj.lamb_log * np.log(self.graph.w)), 1)
            self.losses.append(loss)

            if i % n_print == 0:
                sys.stdout.write("iteration {}: loss = {}\n".format(
                    i, self.losses[-1]))

            # update the graph in the objective
            self.obj.graph = self.graph
Пример #11
0
def comp_fit_cov(feems,
                 lamb_l2,
                 lamb_smth,
                 lamb_log,
                 projection=True,
                 include_var=False,
                 ind_level=False):
    # create obj
    obj = Objective(feems.sumstats, feems.graph)
    obj.lamb_l2 = lamb_l2
    obj.lamb_smth = lamb_smth
    obj.lamb_log = lamb_log

    # update laplacian
    obj.graph.comp_lap(obj.graph.w)

    # update nll and grad
    obj.inv()
    obj.grad()

    if ind_level is True:
        # number of individuals
        n, p = obj.sumstats.data.shape

        # row index of assn matrix
        row = np.arange(n)

        # col index of assn matrix
        col = obj.graph.obs_ids

        # fill up J
        J = sp.csc_matrix(
            (np.ones(n), (row, col)),
            shape=(n, obj.graph.d))[:, obj.graph.perm_ids][:, :obj.graph.o]

        # diagonal component
        q_full_samples = 1.0 / obj.sumstats.s2 * np.ones(obj.graph.o)

        # fitted covariance
        fit_cov = J @ (obj.Linv_block['oo'] - 1 / obj.graph.d) @ J.T + np.diag(
            J @ (1. / q_full_samples))

        # empirical covariance
        emp_cov = obj.sumstats.data @ obj.sumstats.data.T / p
    else:
        # fitted covariance
        fit_cov = obj.Linv_block[
            'oo'] - 1 / obj.graph.d + obj.sumstats.q_inv_diag.toarray()

        # empirical covariance
        emp_cov = obj.sumstats.S

    # project to the space of contrast
    if projection is True:
        C = comp_contrasts(n) if ind_level is True else comp_contrasts(
            obj.graph.o)
        fit_cov = C @ fit_cov @ C.T
        emp_cov = C @ emp_cov @ C.T

    if include_var is True:
        return (np.triu(fit_cov, k=0), np.triu(emp_cov, k=0))
    else:
        return (np.triu(fit_cov, k=1), np.triu(emp_cov, k=1))
Пример #12
0
from utils import *
from objective import Objective


def generate_case():
    '''
    This is a function generating the data for a special case. with 3 sellers and 2 buyers
    :return: the list of sellers, the list of buyers, the edges between them.
    '''
    sellers = [Candidate(0.16, 10), Candidate(0.18, 10), Candidate(0.2, 10)]
    buyers = [Candidate(0.19, 13), Candidate(0.21, 13)]
    edges = [(0, 0), (1, 0), (1, 1), (2, 1)]
    return sellers, buyers, edges


sw, vols = solve_opt_assignment(*generate_case())
print "social welfare and volumes on each edge", sw, vols
print "The prices on each edge", Objective(*generate_case()).min_variance()
                        help="Prefix to all filenames")

    parser.add_argument(
        '--with_competitor',
        action='store_true',
        help=
        'Also run the standard DNN+Opt competitor to see how well ISMO is doing in comparison'
    )

    args = parser.parse_args()
    prefix = args.prefix

    dimension = 2
    number_of_variables = 1

    objective = Objective()

    ismo.convergence.convergence_study(
        generator_name=args.generator,
        training_parameter_filename=args.simple_configuration_file,
        optimizer_name=args.optimizer,
        retries=args.retries,
        save_result=args.save_result,
        prefix=args.prefix,
        with_competitor=args.with_competitor,
        dimension=dimension,
        number_of_variables=number_of_variables,
        number_of_samples_per_iteration=args.number_of_samples_per_iteration,
        simulator_creator=lambda starting_sample: simulate,
        objective=objective,
        variable_names=['p'],
Пример #14
0
from objective import Objective
from tkinter import Tk
from pywinauto.application import Application
import time
import json
import random
import string
import datetime

# Automated creation of new OSRS accounts using Google Chrome
# the accounts will be stored in a JSON file

account = {}
dob = []
r = Tk()
chrome = Objective("GoogleChrome")
tempMailLink = "https://temp-mail.org/"
dir = "osrs/"

failCount = 0
attemptCount = 0


def get_random_alphanumeric_string(length):
    letters_and_digits = string.ascii_letters + string.digits
    result_str = ''.join(
        (random.choice(letters_and_digits) for i in range(length)))
    return result_str


def fill_random_dateofbirth(objective):
Пример #15
0
    def fit(self,
            n_iter,
            lamb_l2,
            lamb_smth,
            lamb_log,
            eta,
            w_init,
            line_search=False,
            lb=1e-10,
            ub=1e10,
            c=.2,
            rho=.5,
            eps=5 * 1.0e-3,
            n_print=1000):
        """Fits the feems model for a single regularzation parameters
        """
        # m init
        self.graph.comp_lap(w_init)

        # opt params
        self.eta = eta
        self.line_search = line_search
        self.lb = lb
        self.ub = ub
        self.c = c
        self.rho = rho
        self.eps = eps

        # compute inital objective
        self.obj = Objective(self.sumstats, self.graph)
        self.obj.lamb_l2 = lamb_l2
        self.obj.lamb_smth = lamb_smth
        self.obj.lamb_log = lamb_log

        # compute inverse covariances and gradient of objective
        self.obj.inv()
        self.obj.grad()

        ########## stopping criterion ##########
        # if self.opt_crit < self.eps terminate algorithm
        self.opt_crit = np.Inf
        self.converged = False

        # iterate
        self.losses = []
        self.losses.append(self.obj.loss())
        for i in range(n_iter):

            # variable updates
            self._w_update()
            if self.converged:
                break

            self.losses.append(self.loss)

            if i % n_print == 0:
                sys.stdout.write("iteration {}: loss = {}\n".format(
                    i, self.losses[-1]))

            # update the graph in the objective
            self.obj.graph = self.graph
import shutil
import util
from objective import Objective


def generateARFF(fileName, samples):
    fullDestFilePath = f'./project2/data/{fileName}.arff'
    shutil.copy2('./project2/starter.arff', fullDestFilePath)
    with open(fullDestFilePath, 'a') as arff:
        for sample in samples:
            arff.write(f'{sample[0]},{sample[1]},{sample[2]}\n')


if __name__ == "__main__":
    params = util.getParams('./project2/params/params.yaml')
    xDomain = [1, 100]
    yDomain = [1, 100]
    objective = Objective(xDomain, yDomain)
    trainingSamples = objective.getSamples(params['numSamples'])
    generateARFF('samples', objective.getSamples(params['numSamples']))
Пример #17
0
from objective import Objective
from tkinter import Tk
import time
import re

# Automated sequence of actions for interaction with Discord bot
# Requires you to have the Discord client in the foreground
# TODO: Instead of time.sleep() it should await for some period of time

r = Tk()
Discord = Objective("Discord")
OCR = Objective("OCR")
countSpawned = 0
countClaimed = 0

# Execution loop
if __name__ == "__main__":
    while True:
        # Type something in Discord chat
        try:
            Discord.clickAtImg("dc_input.PNG")
            Discord.write(".")
            Discord.hotkey("enter")
            time.sleep(0.7)
        except:
            print("Couldn't find Discord input field!")
            continue

        # Check whether card spawned
        try:
            startCoord = Discord.locateImg("dc_spawn.PNG")