Beispiel #1
0
def choose_pairwise():

    # get current user
    username = request.form['username']

    # get the comparison
    winner_item = -np.fromstring(request.form['winner'][:-2], sep=',')
    loser_item = -np.fromstring(request.form['loser'][:-2], sep=',')

    # add comparison to user's data and save
    dataset_user = utils_users.get_gp_dataset(
        username, 'pairwise', num_objectives=specs_traffic.NUM_OBJECTIVES)
    dataset_user.add_single_comparison(winner_item, loser_item)
    utils_users.update_gp_dataset(username, dataset_user, 'pairwise')

    # find out whether user clicked left or right button (0=left, 1=right)
    item_clicked = request.form['item-clicked']

    button_type = request.form['buttonType']

    # display new traffic
    return redirect(
        url_for('.continue_pairwise',
                username=username,
                side_clicked=item_clicked,
                button_type=button_type))
def choose_pairwise():

    # get current user
    username = request.form['username']

    # get the comparison
    winner_job = np.fromstring(request.form['winner'], sep=' ')
    loser_job = np.fromstring(request.form['loser'], sep=' ')

    # normalise jobs again
    winner_job = data_jobs.normalise_job_offer(winner_job)
    loser_job = data_jobs.normalise_job_offer(loser_job)

    # add comparison to user's data and save
    dataset_user = utils_users.get_gp_dataset(
        username, 'pairwise', num_objectives=specs_jobs.NUM_OBJECTIVES)
    dataset_user.add_single_comparison(winner_job, loser_job)
    utils_users.update_gp_dataset(username, dataset_user, 'pairwise')

    # find out whether user clicked left or right button (0=left, 1=right)
    job_clicked = request.form['job-clicked']

    # display new jobs
    return redirect(
        url_for('.continue_pairwise',
                username=username,
                side_clicked=job_clicked))
Beispiel #3
0
def submit_ranking():

    # get the username and their dataset
    username = request.form['username']
    user_dataset = utils_users.get_gp_dataset(
        username, 'ranking', num_objectives=specs_jobs.NUM_OBJECTIVES)

    # get the ranking the user submitted
    ranking = np.fromstring(request.form['rankingResult'],
                            sep=',',
                            dtype=np.int)
    # save it
    utils_users.save_ranking(username, ranking)

    # get the actual jobs (ranking returned the indiced in the dataset)
    jobs_ranked = user_dataset.datapoints[ranking]

    # add the ranking to the dataset
    user_dataset.add_ranked_preferences(jobs_ranked)

    # save
    utils_users.update_gp_dataset(username, user_dataset, 'ranking')

    # get the start time for this user
    start_time = utils_users.get_experiment_start_time(username, 'ranking')

    if time.time() - start_time < specs_jobs.TIME_EXPERIMENT_SEC:
        return redirect(url_for('.start', username=username))
    else:
        utils_users.update_experiment_status(username=username,
                                             query_type='ranking')
        return redirect('start_experiment/{}'.format(username))
def submit_clustering_jobs():

    # get the username and their dataset
    username = request.form['username']
    user_dataset = utils_users.get_gp_dataset(username, 'clustering', num_objectives=specs_jobs.NUM_OBJECTIVES)

    # get the clusters the user submitted
    top_job = [int(request.form['top-cluster'])]
    good_jobs = np.fromstring(request.form['good-cluster'], sep=',', dtype=np.int)
    bad_jobs = np.fromstring(request.form['bad-cluster'], sep=',', dtype=np.int)
    clustering = [top_job, good_jobs, bad_jobs]

    # save it
    utils_users.save_clustering(username, clustering)

    # get the actual jobs (ranking returned the indiced in the dataset)
    top_job = user_dataset.datapoints[top_job]
    good_jobs = user_dataset.datapoints[good_jobs]
    bad_jobs = user_dataset.datapoints[bad_jobs]
    clustering = [top_job, good_jobs, bad_jobs]

    # add the ranking to the dataset
    user_dataset.add_clustered_preferences(clustering)

    # save
    utils_users.update_gp_dataset(username, user_dataset, 'clustering')

    # get the start time for this user
    start_time = utils_users.get_experiment_start_time(username, 'clustering')

    if time.time()-start_time < specs_jobs.TIME_EXPERIMENT_SEC:
        return redirect(url_for('.start', username=username))
    else:
        utils_users.update_experiment_status(username=username, query_type='clustering')
        return redirect('start_experiment/{}'.format(username))
def submit_ranking():

    # get the username and their dataset
    username = request.form['username']
    user_dataset = utils_users.get_gp_dataset(username, 'ranking', num_objectives=specs_traffic.NUM_OBJECTIVES)

    # get the ranking the user submitted
    ranking = np.fromstring(request.form['rankingResult'], sep=',', dtype=np.int)
    # save it
    utils_users.save_ranking(username, ranking)

    # get the actual traffic (ranking returned the indiced in the dataset)
    traffic_ranked = user_dataset.datapoints[ranking]

    # add the ranking to the dataset
    user_dataset.add_ranked_preferences(traffic_ranked)

    print("comparisons from curr ranking", user_dataset.comparisons)

    # save
    utils_users.update_gp_dataset(username, user_dataset, 'ranking')

    # get the start time for this user
    utils_users.get_experiment_start_time(username, 'ranking')

    button_type = request.form['buttonType']

    if button_type == 'next':
        return redirect(url_for('.start', username=username))
    elif button_type == 'end':
        # save end time
        utils_users.save_experiment_end_time(username, 'ranking')
        # register that this experiment was done
        utils_users.update_experiment_status(username=username, query_type='ranking')
        return redirect('start_experiment/{}'.format(username))
    else:
        raise NotImplementedError('Button type not defined')
def start(username):

    # get the start time for this user
    utils_users.get_experiment_start_time(username, 'ranking')

    # get the dataset for this user
    user_dataset = utils_users.get_gp_dataset(username, 'ranking', num_objectives=specs_traffic.NUM_OBJECTIVES)

    # initialise acquirer
    acquirer = DiscreteAcquirer(input_domain=data_traffic.get_traffic_results(), query_type='clustering',
                                seed=specs_traffic.SEED)
    
    # if no data has been collected yet, we only display two starting traffic
    if user_dataset.comparisons.shape[0] == 0:

        # delete any datapoint in the user's dataset (in case experiment was aborted)
        user_dataset.datapoints = np.empty((0, specs_traffic.NUM_OBJECTIVES))

        # get the starting points from the acquirer
        item1, item2 = acquirer.get_start_points()

        # add traffic to dataset of user
        item1_ID = user_dataset._add_single_datapoint(item1)
        item2_ID = user_dataset._add_single_datapoint(item2)

        # save dataset
        utils_users.update_gp_dataset(username, user_dataset, 'ranking')

        # put traffic we want to display in the respective lists
        traffic_unranked = [item1, item2]
        IDs_unranked = [item1_ID, item2_ID]
        traffic_ranked = []
        IDs_ranked = []

    # otherwise, we show the previous ranking and pick a new point according to that
    else:

        # add collected datapoints to acquirer
        acquirer.history = user_dataset.datapoints

        # add virtual comparisons in first few queries
        for i in range(np.min([user_dataset.datapoints.shape[0], 6])):
            user_dataset.add_single_comparison(user_dataset.datapoints[i], data_traffic.get_traffic_min())
            user_dataset.add_single_comparison(data_traffic.get_traffic_max(), user_dataset.datapoints[i])

        # add linear prior in first few queries
        if acquirer.history.shape[0] < 6:
            prior_mean_type = 'linear'
        else:
            prior_mean_type = 'zero'

        print("comparisons after adding stuff", user_dataset.comparisons)

        # intialise the GP
        gp = GPPairwise(num_objectives=specs_traffic.NUM_OBJECTIVES, seed=specs_traffic.SEED, prior_mean_type=prior_mean_type)

        # add collected datapoints to GP
        gp.update(user_dataset)

        # let acquirer pick new point
        job_new = acquirer.get_next_point(gp, user_dataset)

        # add that point to the dataset and save
        job_new_ID = user_dataset._add_single_datapoint(job_new)
        utils_users.update_gp_dataset(username, user_dataset, 'ranking')

        # put into list of traffic that need to be ranked
        traffic_unranked = [job_new]
        IDs_unranked = [job_new_ID]

        # get ranking so far
        IDs_ranked = utils_users.get_ranking(username)
        # get the job information from that ranking and convert to dictionaries
        traffic_ranked = user_dataset.datapoints[IDs_ranked]

    # get names of objectives
    obj_names = data_traffic.get_objective_names()
    obj_abbrev = data_traffic.get_objective_abbrev()

    return render_template("query_ranking_traffic.html", username=username, traffic_unranked=-1*np.array(traffic_unranked),
                           traffic_ranked=-1*np.array(traffic_ranked), IDs_ranked=IDs_ranked, IDs_unranked=IDs_unranked,
                           obj_names=obj_names, obj_abbrev=obj_abbrev)
Beispiel #7
0
def start(username):

    # get the start time for this user
    utils_users.get_experiment_start_time(username, 'ranking')

    # get the dataset for this user
    user_dataset = utils_users.get_gp_dataset(
        username, 'ranking', num_objectives=specs_jobs.NUM_OBJECTIVES)

    # if no data has been collected yet, we only display two starting jobs
    if user_dataset.comparisons.shape[0] == 0:

        # delete any datapoint in the user's dataset (in case experiment was aborted)
        user_dataset.datapoints = np.empty((0, specs_jobs.NUM_OBJECTIVES))

        # get the starting points from the acquirer
        job1, job2 = utils_jobs.get_next_start_jobs(username)

        # add jobs to dataset of user
        job1_idx = user_dataset._add_single_datapoint(job1)
        job2_idx = user_dataset._add_single_datapoint(job2)

        # save dataset
        utils_users.update_gp_dataset(username, user_dataset, 'ranking')

        # convert into displayable format
        job1 = utils_jobs.job_array_to_job_dict(job1)
        job2 = utils_jobs.job_array_to_job_dict(job2)

        # add ID to the above dictionaries (equals the index in the dataset
        job1['ID'] = job1_idx
        job2['ID'] = job2_idx

        # put jobs we want to display in the respective lists
        jobs_unranked = [job1, job2]
        jobs_ranked = []

    # otherwise, we show the previous ranking and pick a new point according to that
    else:

        # intialise the GP
        gp = GPPairwise(num_objectives=specs_jobs.NUM_OBJECTIVES,
                        seed=specs_jobs.SEED)

        # initialise acquirer
        acquirer = DiscreteAcquirer(input_domain=utils_jobs.get_jobs(),
                                    query_type='clustering',
                                    seed=specs_jobs.SEED)

        # add collected datapoints to acquirer
        acquirer.history = user_dataset.datapoints
        # add collected datapoints to GP
        gp.update(user_dataset)

        # let acquirer pick new point
        job_new = acquirer.get_next_point(gp, user_dataset)

        # add that point to the dataset and save
        job_new_idx = user_dataset._add_single_datapoint(job_new)
        utils_users.update_gp_dataset(username, user_dataset, 'ranking')

        # convert job to dictionary
        job_new = utils_jobs.job_array_to_job_dict(job_new)

        # add the ID
        job_new['ID'] = job_new_idx

        # put into list of jobs that need to be ranked
        jobs_unranked = [job_new]

        # get ranking so far
        ranking = utils_users.get_ranking(username)
        # get the job information from that ranking and convert to dictionaries
        jobs_ranked = user_dataset.datapoints[ranking]
        jobs_ranked = [
            utils_jobs.job_array_to_job_dict(job) for job in jobs_ranked
        ]
        # add the IDs
        for i in range(len(ranking)):
            jobs_ranked[i]['ID'] = ranking[i]

    return render_template("query_ranking_jobs.html",
                           username=username,
                           jobs_unranked=jobs_unranked,
                           jobs_ranked=jobs_ranked)