Skip to content

Commit

Permalink
Start implementating genetic algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
ilikerustoo committed Mar 14, 2019
1 parent b1daf25 commit 5c6369f
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 11 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ py = "*"
django = "*"
networkx = "*"
social-auth-app-django = "*"
pandas = "*"

[pipenv]
allow_prereleases = true
70 changes: 65 additions & 5 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions gatorgrouper/utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
ALGORITHM_ROUND_ROBIN = "rrobin"
ALGORITHM_RANDOM = "random"
ALGORITHM_GRAPH = "graph"
ALGORITHM_GENETIC = "genetic"
DEFAULT_METHOD = ALGORITHM_RANDOM
DEFAULT_GRPSIZE = 3
DEFAULT_NUMGRP = 3
Expand All @@ -24,5 +25,5 @@
# Define configuration variables

DEFAULT_GROUP_SIZE = 2
WORKBOOK = "GatorGrouper (Responses)"
WORKBOOK_CSV = WORKBOOK.replace(" ", "_") + ".csv"
WORKBOOK = "GatorGrouper_(Responses).csv"
# WORKBOOK_CSV = WORKBOOK.replace(" ", "_") + ".csv"
11 changes: 7 additions & 4 deletions gatorgrouper/utils/group_genetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import random
import math
import numpy as np
from colors import bold
from gatorgrouper.utils import constants
from gatorgrouper.utils import workbook
# from colors import bold


class Student:
Expand Down Expand Up @@ -78,16 +80,17 @@ def __str__(self):

def create():
"""Create the groups of student"""
students_to_group = workbook.STUDENTS[:]

students_to_group = ["a","b","c","d"]
random.shuffle(students_to_group)

grouping = list()

for _ in range(workbook.GROUPING_SIZE):
for _ in range(DEFAULT_GRPSIZE):
grouping.append(list())

for index, student in enumerate(students_to_group):
grouping[index % workbook.GROUPING_SIZE].append(student)
grouping[index % DEFAULT_GRPSIZE].append(student)

if grouping < 1:
print("CREATED TOO SMALL GROUPING")
Expand Down
33 changes: 33 additions & 0 deletions gatorgrouper/utils/mutations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import sys
import random
from typing import List
from gatorgrouper.utils import group_genetic


def swap(grouping):
# print("MUTATION")
group_count = len(grouping)
# print("TOTAL GROUPS: {}".format(group_count))
# print("BEFORE: {}".format(grouping))
first, second = random.sample(range(len(grouping)), 2)
first_index = random.randrange(len(grouping[first]))
second_index = random.randrange(len(grouping[second]))
# print("swapping student {} in group {} with student {} in group {}".format(first_index, first, second_index, second))
temp = grouping[second][second_index]
grouping[second][second_index] = grouping[first][first_index]
grouping[second][second_index] = temp
# grouping[first][first_index], grouping[second][second_index] = grouping[second][second_index], grouping[first][first_index]
# print("AFTER: {}".format(grouping))

return grouping


def multi_swap(grouping):
num_swaps = random.randrange(1, 6)
for _ in range(num_swaps):
grouping = swap(grouping)
return grouping


def get():
return [swap, multi_swap]
1 change: 1 addition & 0 deletions gatorgrouper/utils/parse_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def parse_arguments(args):
constants.ALGORITHM_GRAPH,
constants.ALGORITHM_ROUND_ROBIN,
constants.ALGORITHM_RANDOM,
constants.ALGORITHM_GENETIC,
],
default=constants.DEFAULT_METHOD,
required=False,
Expand Down
71 changes: 71 additions & 0 deletions gatorgrouper/utils/workbook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"""Integrate GatorGrouper with Google Sheets."""

import csv
import math
import logging
import gspread
import pandas as pd
from oauth2client.service_account import ServiceAccountCredentials

from gatorgrouper.utils import group_genetic
from gatorgrouper.utils import constants


EMAIL_COL = None
PREFERENCES_COL = None
SKILLS_COLS = set()

STUDENTS = None
GROUPING_SIZE = None

def get(group_size):
"""Retrieve data from Google Sheets and write to a CSV file."""

global EMAIL_COL
global PREFERENCES_COL
global SKILLS_COLS

# formatted_records = list()
# for entry in records:
# formatted_entry = list()
# for index, (question, response) in enumerate(entry.items()):
# if question == 'Email Address':
# EMAIL_COL = index - 1 # subtracting one because timestamp column not collected
# formatted_entry.append(response)
# elif "prefer" in question:
# PREFERENCES_COL = index - 1
# formatted_entry.append(response)
# elif "skill" in question:
# SKILLS_COLS.add(index - 1)
# formatted_entry.append(response)
# formatted_records.append(formatted_entry)

global STUDENTS
global GROUPING_SIZE

# EMAIL_COL = 0
# PREFERENCES_COL = 1
# SKILLS_COLS = [2, 3, 4, 5, 6]

DATA = pd.read_csv(constants.WORKBOOK_CSV, header=None)

EMAILS = DATA.iloc[:, EMAIL_COL]

STUDENTS = list()
for current_row, email in enumerate(EMAILS):
skills = list()
for skill_col in SKILLS_COLS:
skills.append(DATA.iat[current_row, skill_col])
preferences_str = DATA.iat[current_row, PREFERENCES_COL]

if isinstance(preferences_str, float) and math.isnan(preferences_str):
preferences = []
else:
preferences = preferences_str.replace(" ", "").split(",")

STUDENTS.append(Student(email, skills, preferences))

# for student in STUDENTS:
# print(str(student) + "\n")

GROUPING_SIZE = math.floor(len(STUDENTS) / group_size)
4 changes: 4 additions & 0 deletions gatorgrouper_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from gatorgrouper.utils import group_graph
from gatorgrouper.utils import display
from gatorgrouper.utils import constants
from gatorgrouper.utils import group_genetic
from gatorgrouper.utils import mutations


if __name__ == "__main__": # pragma: no cover
Expand Down Expand Up @@ -62,6 +64,8 @@
objective_weights=GG_ARGUMENTS.objective_weights,
objective_measures=GG_ARGUMENTS.objective_measures,
)
elif GG_ARGUMENTS.method == constants.ALGORITHM_GENETIC:
GROUPED_STUDENT_IDENTIFIERS = group_genetic.evolve(60, 0.33, 0.16, 0.18, 0.66, mutations.get())
else:
GROUPED_STUDENT_IDENTIFIERS = group_creation.group_random_num_group(
SHUFFLED_STUDENT_IDENTIFIERS, GG_ARGUMENTS.num_group
Expand Down

0 comments on commit 5c6369f

Please sign in to comment.