<commit_before># encoding: utf-8
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Author: Kyle Lahnakoski (kyle@lahnakoski.com)
#
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals

import sys

import mo_dots
from mo_dots import wrap, join_field, split_field

DEBUG = True

def set(constants):
    """
    REACH INTO THE MODULES AND OBJECTS TO SET CONSTANTS.
    THINK OF THIS AS PRIMITIVE DEPENDENCY INJECTION FOR MODULES.
    USEFUL FOR SETTING DEBUG FLAGS.
    """
    if not constants:
        return
    constants = wrap(constants)

    for k, new_value in constants.leaves():
        errors = []
        try:
            old_value = mo_dots.set_attr(sys.modules, k, new_value)
            continue
        except Exception, e:
            errors.append(e)

        # ONE MODULE IS MISSING, THE CALLING MODULE
        try:
            caller_globals = sys._getframe(1).f_globals
            caller_file = caller_globals["__file__"]
            if not caller_file.endswith(".py"):
                raise Exception("do not know how to handle non-python caller")
            caller_module = caller_file[:-3].replace("/", ".")

            path = split_field(k)
            for i, p in enumerate(path):
                if i == 0:
                    continue
                prefix = join_field(path[:1])
                name = join_field(path[i:])
                if caller_module.endswith(prefix):
                    old_value = mo_dots.set_attr(caller_globals, name, new_value)
                    if DEBUG:
                        from mo_logs import Log

                        Log.note("Changed {{module}}[{{attribute}}] from {{old_value}} to {{new_value}}",
                            module= prefix,
                            attribute= name,
                            old_value= old_value,
                            new_value= new_value)
                    break
        except Exception, e:
            errors.append[e]

        if errors:
            from mo_logs import Log

            Log.error("Can not set constant {{path}}", path=k, cause=errors)
<commit_msg>whitespace<commit_after># encoding: utf-8
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Author: Kyle Lahnakoski (kyle@lahnakoski.com)
#
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals

import sys

import mo_dots
from mo_dots import wrap, join_field, split_field

DEBUG = True

def set(constants):
    """
    REACH INTO THE MODULES AND OBJECTS TO SET CONSTANTS.
    THINK OF THIS AS PRIMITIVE DEPENDENCY INJECTION FOR MODULES.
    USEFUL FOR SETTING DEBUG FLAGS.
    """
    if not constants:
        return
    constants = wrap(constants)

    for k, new_value in constants.leaves():
        errors = []
        try:
            old_value = mo_dots.set_attr(sys.modules, k, new_value)
            continue
        except Exception, e:
            errors.append(e)

        # ONE MODULE IS MISSING, THE CALLING MODULE
        try:
            caller_globals = sys._getframe(1).f_globals
            caller_file = caller_globals["__file__"]
            if not caller_file.endswith(".py"):
                raise Exception("do not know how to handle non-python caller")
            caller_module = caller_file[:-3].replace("/", ".")

            path = split_field(k)
            for i, p in enumerate(path):
                if i == 0:
                    continue
                prefix = join_field(path[:1])
                name = join_field(path[i:])
                if caller_module.endswith(prefix):
                    old_value = mo_dots.set_attr(caller_globals, name, new_value)
                    if DEBUG:
                        from mo_logs import Log

                        Log.note("Changed {{module}}[{{attribute}}] from {{old_value}} to {{new_value}}",
                            module= prefix,
                            attribute= name,
                            old_value= old_value,
                            new_value= new_value)
                    break
        except Exception, e:
            errors.append[e]

        if errors:
            from mo_logs import Log

            Log.error("Can not set constant {{path}}", path=k, cause=errors)
<|endoftext|>


<commit_before>"""
This algorithm helps you to swap cases.

User will give input and then program will perform swap cases.

In other words, convert all lowercase letters to uppercase letters and vice versa.
For example:
1. Please input sentence: Algorithm.Python@89
  aLGORITHM.pYTHON@89
2. Please input sentence: github.com/mayur200
  GITHUB.COM/MAYUR200

"""

def swap_case(sentence: str) -> str:
    """
    This function will convert all lowercase letters to uppercase letters
    and vice versa.

    >>> swap_case('Algorithm.Python@89')
    'aLGORITHM.pYTHON@89'
    """
    new_string = ""
    for char in sentence:
        if char.isupper():
            new_string += char.lower()
        elif char.islower():
            new_string += char.upper()
        else:
            new_string += char

    return new_string

if __name__ == "__main__":
    print(swap_case(input("Please input sentence: ")))
<commit_msg>Used in-built method (#5183)<commit_after><|endoftext|>


<commit_before>#!/usr/bin/env python
# encoding: utf-8

from __future__ import division

from __future__ import absolute_import
from .fitness import fitness
from random import randrange, choice, random, sample, shuffle
from collections import namedtuple

def arrange(links, successors, multiplicity, col_count, row_count, verbose, has_expired,
            population_size, max_generations, plateau, crossover_rate, mutation_rate, sample_size,
            **kwargs):

    def make_individual():
        """ Construct a chromosome. Select a random node for the first gene. The next ones are chosen
            sequentially. When a gene has another gene to the west, the corresponding node is preferably
            selected among the successors of the latter. NB: Applying the same technic for the north and
            nortwest directions produces better individual, but worse final results. """
        pool = list(range(box_count))
        chromosome = [pool.pop(randrange(box_count))]
        (x, y) = (0, 0)
        for i in range(1, box_count):
            x += 1
            if x == col_count:
                x = 0
                y += 1
                candidates = set()
            else:
                candidates = successors[chromosome[i-1]].intersection(pool)
            chromosome.append(pool.pop(pool.index(choice(tuple(candidates))) if candidates else randrange(len(pool))))
        return Individual(evaluate(chromosome), chromosome)

    def crossover(chromosome_1, chromosome_2):
        """ Produce two children for a given pair of individuals. A random rectangular zone is first selected.
            The corresponding genes in the first individual are copied in the first child. The remaining places
            are filled with the genes of the second individual taken one by one. Symmetrical operations for the
            second child. """
        (x1, y1) = (randrange(col_count), randrange(row_count))
        (x2, y2) = (randrange(x1+1, col_count+1), randrange(y1+1, row_count+1))
        def mate(chromosome_1, chromosome_2):
            used = set(chromosome_1[x+y*col_count] for x in range(x1, x2) for y in range(y1, y2))
            not_used_next = (allele for allele in chromosome_2 if allele not in used).next
            return [chromosome_1[x+y*col_count] if x1 <= x < x2 and y1 <= y < y2 else not_used_next() for y in range(row_count) for x in range(col_count)]
        return (mate(chromosome_1, chromosome_2), mate(chromosome_2, chromosome_1))

    def next_population():
        """ Evolve the population. The best individual is kept. The others are selected by tournament.
            Some selected pairs produce two children. Each selected individual may mutate at certain
            places. Mutation of a gene simply consists in swapping it with another one. """
        result = [best]
        while len(result) < population_size:
            chromosomes = crossover(tournament(), tournament()) if random() < crossover_rate else [tournament()]
            for chromosome in chromosomes:
                for i in range(box_count):
                    if random() < mutation_rate:
                        j = randrange(box_count)
                        (chromosome[i], chromosome[j]) = (chromosome[j], chromosome[i])
                result.append(Individual(evaluate(chromosome), chromosome))
        return result[:population_size]

    def tournament():
        """ Return a COPY of the best chromosome selected among a random sample. """
        return min(sample(population, sample_size)).chromosome[:]

    evaluate = fitness(links, multiplicity, col_count, row_count)
    Individual = namedtuple("Individual", ["score", "chromosome"])
    box_count = col_count * row_count
    patience = plateau
    previous_best_score = None
    population = sorted([make_individual() for _ in range(population_size)])
    best = population[0]
    for generation in range(max_generations):
        if best.score == previous_best_score:
            patience -= 1
        else:
            if verbose:
                print("% 3d: %s" % (generation, best.score))
            previous_best_score = best.score
            patience = plateau
        if best.score == (0, 0) or patience == 0 or has_expired():
            break
        population = sorted(next_population())
        best = population[0]
        generation += 1
    return {
        "distances": best.score[1],
        "crossings": best.score[0],
        "layout": best.chromosome
    }

if __name__ == "__main__":
    from .mcd import Mcd
    from .argument_parser import parsed_arguments
    from time import time
    from random import seed
    clauses = u"""
        SUSPENDISSE: diam
        SOLLICITUDIN, 0N SUSPENDISSE, 0N CONSECTETUER, 0N LOREM: lectus
        CONSECTETUER: elit, sed
        MAECENAS, 1N DIGNISSIM, 1N DIGNISSIM

        DF1, 11 LOREM, 1N SUSPENDISSE
        LOREM: ipsum, dolor, sit
        TORTOR, 0N RISUS, 11 DIGNISSIM, 1N CONSECTETUER: nec
        DIGNISSIM: ligula, massa, varius

        DF, 11 RISUS, 0N RISUS
        AMET, 11> LOREM, 01 CONSECTETUER: adipiscing
        RISUS: ultricies, _cras, elementum
        SEMPER, 0N RISUS, 1N DIGNISSIM
    """.replace("  ", "").split("\n")
    params = parsed_arguments()
    params["verbose"] = True
    mcd = Mcd(clauses, params)
    params.update(mcd.get_layout_data())
    starting_time = time()
    seed(42)
    result = arrange(**params)
    if result:
        print()
        print(mcd.get_clauses_from_layout(**result))
        print()
        print("Cumulated distances:", result["distances"])
        print("Duration:", time() - starting_time)
        print()
<commit_msg>found another .next to modernize<commit_after>#!/usr/bin/env python
# encoding: utf-8

from __future__ import division

from fitness import fitness
from random import randrange, choice, random, sample
from collections import namedtuple

def arrange(links, successors, multiplicity, col_count, row_count, verbose, has_expired,
            population_size, max_generations, plateau, crossover_rate, mutation_rate, sample_size,
            **kwargs):

    def make_individual():
        """ Construct a chromosome. Select a random node for the first gene. The next ones are chosen
            sequentially. When a gene has another gene to the west, the corresponding node is preferably
            selected among the successors of the latter. NB: Applying the same technic for the north and
            nortwest directions produces better individual, but worse final results. """
        pool = list(range(box_count))
        chromosome = [pool.pop(randrange(box_count))]
        (x, y) = (0, 0)
        for i in range(1, box_count):
            x += 1
            if x == col_count:
                x = 0
                y += 1
                candidates = set()
            else:
                candidates = successors[chromosome[i-1]].intersection(pool)
            chromosome.append(pool.pop(pool.index(choice(tuple(candidates))) if candidates else randrange(len(pool))))
        return Individual(evaluate(chromosome), chromosome)

    def crossover(chromosome_1, chromosome_2):
        """ Produce two children for a given pair of individuals. A random rectangular zone is first selected.
            The corresponding genes in the first individual are copied in the first child. The remaining places
            are filled with the genes of the second individual taken one by one. Symmetrical operations for the
            second child. """
        (x1, y1) = (randrange(col_count), randrange(row_count))
        (x2, y2) = (randrange(x1+1, col_count+1), randrange(y1+1, row_count+1))
        def mate(chromosome_1, chromosome_2):
            used = set(chromosome_1[x+y*col_count] for x in range(x1, x2) for y in range(y1, y2))
            not_used = (allele for allele in chromosome_2 if allele not in used)
            return [chromosome_1[x+y*col_count] if x1 <= x < x2 and y1 <= y < y2 else next(not_used) for y in range(row_count) for x in range(col_count)]
        return (mate(chromosome_1, chromosome_2), mate(chromosome_2, chromosome_1))

    def next_population():
        """ Evolve the population. The best individual is kept. The others are selected by tournament.
            Some selected pairs produce two children. Each selected individual may mutate at certain
            places. Mutation of a gene simply consists in swapping it with another one. """
        result = [best]
        while len(result) < population_size:
            chromosomes = crossover(tournament(), tournament()) if random() < crossover_rate else [tournament()]
            for chromosome in chromosomes:
                for i in range(box_count):
                    if random() < mutation_rate:
                        j = randrange(box_count)
                        (chromosome[i], chromosome[j]) = (chromosome[j], chromosome[i])
                result.append(Individual(evaluate(chromosome), chromosome))
        return result[:population_size]

    def tournament():
        """ Return a COPY of the best chromosome selected among a random sample. """
        return min(sample(population, sample_size)).chromosome[:]

    evaluate = fitness(links, multiplicity, col_count, row_count)
    Individual = namedtuple("Individual", ["score", "chromosome"])
    box_count = col_count * row_count
    patience = plateau
    previous_best_score = None
    population = sorted([make_individual() for _ in range(population_size)])
    best = population[0]
    for generation in range(max_generations):
        if best.score == previous_best_score:
            patience -= 1
        else:
            if verbose:
                print("% 3d: %s" % (generation, best.score))
            previous_best_score = best.score
            patience = plateau
        if best.score == (0, 0) or patience == 0 or has_expired():
            break
        population = sorted(next_population())
        best = population[0]
        generation += 1
    return {
        "distances": best.score[1],
        "crossings": best.score[0],
        "layout": best.chromosome
    }

if __name__ == "__main__":
    from .mcd import Mcd
    from .argument_parser import parsed_arguments
    from time import time
    from random import seed
    clauses = u"""
        SUSPENDISSE: diam
        SOLLICITUDIN, 0N SUSPENDISSE, 0N CONSECTETUER, 0N LOREM: lectus
        CONSECTETUER: elit, sed
        MAECENAS, 1N DIGNISSIM, 1N DIGNISSIM

        DF1, 11 LOREM, 1N SUSPENDISSE
        LOREM: ipsum, dolor, sit
        TORTOR, 0N RISUS, 11 DIGNISSIM, 1N CONSECTETUER: nec
        DIGNISSIM: ligula, massa, varius

        DF, 11 RISUS, 0N RISUS
        AMET, 11> LOREM, 01 CONSECTETUER: adipiscing
        RISUS: ultricies, _cras, elementum
        SEMPER, 0N RISUS, 1N DIGNISSIM
    """.replace("  ", "").split("\n")
    params = parsed_arguments()
    params["verbose"] = True
    mcd = Mcd(clauses, params)
    params.update(mcd.get_layout_data())
    starting_time = time()
    seed(42)
    result = arrange(**params)
    if result:
        print()
        print(mcd.get_clauses_from_layout(**result))
        print()
        print("Cumulated distances:", result["distances"])
        print("Duration:", time() - starting_time)
        print()
<|endoftext|>


<commit_before>from django.contrib import admin

# Register your models here.
from studygroups.models import Course
from studygroups.models import StudyGroup
from studygroups.models import Meeting
from studygroups.models import Application
from studygroups.models import Reminder
from studygroups.models import Profile
from studygroups.models import Team
from studygroups.models import TeamMembership
from studygroups.models import TeamInvitation

class ApplicationInline(admin.TabularInline):
    model = Application

class StudyGroupAdmin(admin.ModelAdmin):
    inlines = [ApplicationInline]

    list_display = ['course', 'city', 'facilitator', 'start_date', 'day', 'signup_open']

class TeamMembershipInline(admin.TabularInline):
    model = TeamMembership
    raw_id_fields = ("user",)

class TeamAdmin(admin.ModelAdmin):
    list_display = ('name', 'page_slug', 'members')
    inlines = [TeamMembershipInline]

    def members(self, obj):
        return obj.teammembership_set.active().count()

class ApplicationAdmin(admin.ModelAdmin):
    list_display = ('name', 'study_group', 'email', 'mobile', 'created_at')

def reminder_course_title(obj):
    return obj.study_group.course.title

class ReminderAdmin(admin.ModelAdmin):
    list_display = (reminder_course_title, 'email_subject', 'sent_at')

class StudyGroupInline(admin.TabularInline):
    model = StudyGroup
    fields = ('venue_name', 'city', 'start_date', 'day')
    readonly_fields = fields

    def has_add_permission(self, request, obj=None):
        return False

    def has_delete_permission(self, request, obj=None):
        return False

class CourseAdmin(admin.ModelAdmin):

    def get_queryset(self, request):
        qs = super(CourseAdmin, self).get_queryset(request)
        return qs.active()

    def created_by(course):
        def display_user(user):
            return '{} {}'.format(user.first_name, user.last_name)
        return display_user(course.created_by) if course.created_by else 'P2PU'

    def email(course):
        return course.created_by.email if course.created_by else '-'

    def learning_circles(course):
        return course.studygroup_set.active().count()

    def listed(course):
        return not course.unlisted
    listed.boolean = True

    list_display = ('id', 'title', 'provider', 'on_demand', 'topics', learning_circles, created_by, email, listed, 'license')
    exclude = ('deleted_at',)
    inlines = [StudyGroupInline]
    search_fields = ['title', 'provider', 'topics', 'created_by__email', 'license']

class ProfileAdmin(admin.ModelAdmin):
    def user(profile):
        return " ".join([profile.user.first_name, profile.user.last_name])
    list_display = [user, 'mailing_list_signup', 'communication_opt_in']
    search_fields = ['user__email']

admin.site.register(Course, CourseAdmin)
admin.site.register(StudyGroup, StudyGroupAdmin)
admin.site.register(Meeting)
admin.site.register(Application, ApplicationAdmin)
admin.site.register(Reminder, ReminderAdmin)
admin.site.register(Team, TeamAdmin)
admin.site.register(TeamInvitation)
admin.site.register(Profile, ProfileAdmin)
<commit_msg>Add search field to application admin page<commit_after>from django.contrib import admin

# Register your models here.
from studygroups.models import Course
from studygroups.models import StudyGroup
from studygroups.models import Meeting
from studygroups.models import Application
from studygroups.models import Reminder
from studygroups.models import Profile
from studygroups.models import Team
from studygroups.models import TeamMembership
from studygroups.models import TeamInvitation

class ApplicationInline(admin.TabularInline):
    model = Application

class StudyGroupAdmin(admin.ModelAdmin):
    inlines = [ApplicationInline]

    list_display = ['course', 'city', 'facilitator', 'start_date', 'day', 'signup_open']

class TeamMembershipInline(admin.TabularInline):
    model = TeamMembership
    raw_id_fields = ("user",)

class TeamAdmin(admin.ModelAdmin):
    list_display = ('name', 'page_slug', 'members')
    inlines = [TeamMembershipInline]

    def members(self, obj):
        return obj.teammembership_set.active().count()

class ApplicationAdmin(admin.ModelAdmin):
    list_display = ('name', 'study_group', 'email', 'mobile', 'created_at')
    search_fields = ['name', 'email', 'mobile']

def reminder_course_title(obj):
    return obj.study_group.course.title

class ReminderAdmin(admin.ModelAdmin):
    list_display = (reminder_course_title, 'email_subject', 'sent_at')

class StudyGroupInline(admin.TabularInline):
    model = StudyGroup
    fields = ('venue_name', 'city', 'start_date', 'day')
    readonly_fields = fields

    def has_add_permission(self, request, obj=None):
        return False

    def has_delete_permission(self, request, obj=None):
        return False

class CourseAdmin(admin.ModelAdmin):

    def get_queryset(self, request):
        qs = super(CourseAdmin, self).get_queryset(request)
        return qs.active()

    def created_by(course):
        def display_user(user):
            return '{} {}'.format(user.first_name, user.last_name)
        return display_user(course.created_by) if course.created_by else 'P2PU'

    def email(course):
        return course.created_by.email if course.created_by else '-'

    def learning_circles(course):
        return course.studygroup_set.active().count()

    def listed(course):
        return not course.unlisted
    listed.boolean = True

    list_display = ('id', 'title', 'provider', 'on_demand', 'topics', learning_circles, created_by, email, listed, 'license')
    exclude = ('deleted_at',)
    inlines = [StudyGroupInline]
    search_fields = ['title', 'provider', 'topics', 'created_by__email', 'license']

class ProfileAdmin(admin.ModelAdmin):
    def user(profile):
        return " ".join([profile.user.first_name, profile.user.last_name])
    list_display = [user, 'mailing_list_signup', 'communication_opt_in']
    search_fields = ['user__email']

admin.site.register(Course, CourseAdmin)
admin.site.register(StudyGroup, StudyGroupAdmin)
admin.site.register(Meeting)
admin.site.register(Application, ApplicationAdmin)
admin.site.register(Reminder, ReminderAdmin)
admin.site.register(Team, TeamAdmin)
admin.site.register(TeamInvitation)
admin.site.register(Profile, ProfileAdmin)
<|endoftext|>


<commit_before>import numpy as np
from chainer import Variable
from chainer import optimizers
from model.dqn_agent import Q
import chainer.functions as F
from model.agent import Agent

class DQNTrainer(Agent):

    def __init__(self,
                    agent,
                    memory_size=10**3,
                    replay_size=32,
                    gamma=0.99,
                    initial_exploration=10**4,
                    target_update_freq=10**4,
                    learning_rate=0.00025,
                    epsilon_decay=1e-6,
                    minimum_epsilon=0.1):
        self.agent = agent
        self.target = Q(self.agent.q.n_history, self.agent.q.n_action, on_gpu=self.agent.q.on_gpu)

        self.memory_size = memory_size
        self.replay_size = replay_size
        self.gamma = gamma
        self.initial_exploration = initial_exploration
        self.target_update_freq = target_update_freq
        self.learning_rate = learning_rate
        self.epsilon_decay = epsilon_decay
        self.minimum_epsilon = minimum_epsilon
        self._step = 0

        # prepare memory for replay
        n_hist = self.agent.q.n_history
        size = self.agent.q.SIZE
        self.memory = [
            np.zeros((memory_size, n_hist, size, size), dtype=np.float32),
            np.zeros(memory_size, dtype=np.uint8),
            np.zeros((memory_size, 1), dtype=np.float32),
            np.zeros((memory_size, n_hist, size, size), dtype=np.float32),
            np.zeros((memory_size, 1), dtype=np.bool)
        ]
        self.memory_text = [
            "state", "action", "reward", "next_state", "episode_end"
        ]

        # prepare optimizer
        self.optimizer = optimizers.RMSpropGraves(lr=learning_rate, alpha=0.95, momentum=0.95, eps=0.01)
        self.optimizer.setup(self.agent.q)
        self._loss = 9
        self._qv = 0

    def calc_loss(self, states, actions, rewards, next_states, episode_ends):
        qv = self.agent.q(states)
        q_t = self.target(next_states)  # Q(s', *)
        max_q_prime = np.array(list(map(np.max, q_t.data)), dtype=np.float32)  # max_a Q(s', a)

        target = cuda.to_cpu(qv.data.copy())
        for i in range(self.replay_size):
            if episode_ends[i][0] is True:
                _r = np.sign(rewards[i])
            else:
                _r = np.sign(rewards[i]) + self.gamma * max_q_prime[i]

            target[i, actions[i]] = _r

        td = Variable(self.target.arr_to_gpu(target)) - qv
        td_tmp = td.data + 1000.0 * (abs(td.data) <= 1)  # Avoid zero division
        td_clip = td * (abs(td.data) <= 1) + td/abs(td_tmp) * (abs(td.data) > 1)

        zeros = Variable(self.target.arr_to_gpu(np.zeros((self.replay_size, self.target.n_action), dtype=np.float32)))
        loss = F.mean_squared_error(td_clip, zeros)
        self._loss = loss.data
        self._qv = np.max(qv.data)
        return loss

    def start(self, observation):
        return self.agent.start(observation)

    def act(self, observation, reward):
        if self.initial_exploration <= self._step:
            self.agent.epsilon -= 1.0/10**6
            if self.agent.epsilon < self.minimum_epsilon:
                self.agent.epsilon = self.minimum_epsilon

        return self.train(observation, reward, episode_end=False)

    def end(self, observation, reward):
        self.train(observation, reward, episode_end=True)

    def train(self, observation, reward, episode_end):
        action = 0
        last_state = self.agent.get_state()
        last_action = self.agent.last_action
        if not episode_end:
            action = self.agent.act(observation, reward)
            result_state = self.agent.get_state()
            self.memorize(
                last_state,
                last_action,
                reward,
                result_state,
                False)
        else:
            self.memorize(
                last_state,
                last_action,
                reward,
                last_state,
                True)

        if self.initial_exploration <= self._step:
            self.experience_replay()

            if self._step % self.target_update_freq == 0:
                self.target.copyparams(self.agent.q)

        self._step += 1
        return action

    def memorize(self, state, action, reward, next_state, episode_end):
        _index = self._step % self.memory_size
        self.memory[0][_index] = state
        self.memory[1][_index] = action
        self.memory[2][_index] = reward
        if not episode_end:
            self.memory[3][_index] = next_state
        self.memory[4][_index] = episode_end

    def experience_replay(self):
        indices = []
        if self._step < self.memory_size:
            indices = np.random.randint(0, self._step, (self.replay_size))
        else:
            indices = np.random.randint(0, self.memory_size, (self.replay_size))

        states = []
        actions = []
        rewards = []
        next_states = []
        episode_ends = []
        for i in indices:
            states.append(self.memory[0][i])
            actions.append(self.memory[1][i])
            rewards.append(self.memory[2][i])
            next_states.append(self.memory[3][i])
            episode_ends.append(self.memory[4][i])

        to_np = lambda arr: np.array(arr)
        self.optimizer.target.cleargrads()
        loss = self.calc_loss(to_np(states), to_np(actions), to_np(rewards), to_np(next_states), to_np(episode_ends))
        loss.backward()
        self.optimizer.update()

    def report(self, episode):
        s = "{0}: loss={1}, q value={2}, epsilon={3}".format(self._step, self._loss, self._qv, self.agent.epsilon)
        self.agent.save(episode)
        return s
<commit_msg>down to numpy array<commit_after>import numpy as np
from chainer import Variable
from chainer import optimizers
from chainer import cuda
from model.dqn_agent import Q
import chainer.functions as F
from model.agent import Agent

class DQNTrainer(Agent):

    def __init__(self,
                    agent,
                    memory_size=10**3,
                    replay_size=32,
                    gamma=0.99,
                    initial_exploration=10**4,
                    target_update_freq=10**4,
                    learning_rate=0.00025,
                    epsilon_decay=1e-6,
                    minimum_epsilon=0.1):
        self.agent = agent
        self.target = Q(self.agent.q.n_history, self.agent.q.n_action, on_gpu=self.agent.q.on_gpu)

        self.memory_size = memory_size
        self.replay_size = replay_size
        self.gamma = gamma
        self.initial_exploration = initial_exploration
        self.target_update_freq = target_update_freq
        self.learning_rate = learning_rate
        self.epsilon_decay = epsilon_decay
        self.minimum_epsilon = minimum_epsilon
        self._step = 0

        # prepare memory for replay
        n_hist = self.agent.q.n_history
        size = self.agent.q.SIZE
        self.memory = [
            np.zeros((memory_size, n_hist, size, size), dtype=np.float32),
            np.zeros(memory_size, dtype=np.uint8),
            np.zeros((memory_size, 1), dtype=np.float32),
            np.zeros((memory_size, n_hist, size, size), dtype=np.float32),
            np.zeros((memory_size, 1), dtype=np.bool)
        ]
        self.memory_text = [
            "state", "action", "reward", "next_state", "episode_end"
        ]

        # prepare optimizer
        self.optimizer = optimizers.RMSpropGraves(lr=learning_rate, alpha=0.95, momentum=0.95, eps=0.01)
        self.optimizer.setup(self.agent.q)
        self._loss = 9
        self._qv = 0

    def calc_loss(self, states, actions, rewards, next_states, episode_ends):
        qv = self.agent.q(states)
        q_t = self.target(next_states)  # Q(s', *)
        max_q_prime = np.array(list(map(np.max, q_t.data)), dtype=np.float32)  # max_a Q(s', a)

        target = cuda.to_cpu(qv.data.copy())
        for i in range(self.replay_size):
            if episode_ends[i][0] is True:
                _r = np.sign(rewards[i])
            else:
                _r = np.sign(rewards[i]) + self.gamma * max_q_prime[i]

            target[i, actions[i]] = _r

        td = Variable(self.target.arr_to_gpu(target)) - qv
        td_tmp = td.data + 1000.0 * (abs(td.data) <= 1)  # Avoid zero division
        td_clip = td * (abs(td.data) <= 1) + td/abs(td_tmp) * (abs(td.data) > 1)

        zeros = Variable(self.target.arr_to_gpu(np.zeros((self.replay_size, self.target.n_action), dtype=np.float32)))
        loss = F.mean_squared_error(td_clip, zeros)
        self._loss = loss.data
        self._qv = np.max(qv.data)
        return loss

    def start(self, observation):
        return self.agent.start(observation)

    def act(self, observation, reward):
        if self.initial_exploration <= self._step:
            self.agent.epsilon -= 1.0/10**6
            if self.agent.epsilon < self.minimum_epsilon:
                self.agent.epsilon = self.minimum_epsilon

        return self.train(observation, reward, episode_end=False)

    def end(self, observation, reward):
        self.train(observation, reward, episode_end=True)

    def train(self, observation, reward, episode_end):
        action = 0
        last_state = self.agent.get_state()
        last_action = self.agent.last_action
        if not episode_end:
            action = self.agent.act(observation, reward)
            result_state = self.agent.get_state()
            self.memorize(
                last_state,
                last_action,
                reward,
                result_state,
                False)
        else:
            self.memorize(
                last_state,
                last_action,
                reward,
                last_state,
                True)

        if self.initial_exploration <= self._step:
            self.experience_replay()

            if self._step % self.target_update_freq == 0:
                self.target.copyparams(self.agent.q)

        self._step += 1
        return action

    def memorize(self, state, action, reward, next_state, episode_end):
        _index = self._step % self.memory_size
        self.memory[0][_index] = state
        self.memory[1][_index] = action
        self.memory[2][_index] = reward
        if not episode_end:
            self.memory[3][_index] = next_state
        self.memory[4][_index] = episode_end

    def experience_replay(self):
        indices = []
        if self._step < self.memory_size:
            indices = np.random.randint(0, self._step, (self.replay_size))
        else:
            indices = np.random.randint(0, self.memory_size, (self.replay_size))

        states = []
        actions = []
        rewards = []
        next_states = []
        episode_ends = []
        for i in indices:
            states.append(self.memory[0][i])
            actions.append(self.memory[1][i])
            rewards.append(self.memory[2][i])
            next_states.append(self.memory[3][i])
            episode_ends.append(self.memory[4][i])

        to_np = lambda arr: np.array(arr)
        self.optimizer.target.cleargrads()
        loss = self.calc_loss(to_np(states), to_np(actions), to_np(rewards), to_np(next_states), to_np(episode_ends))
        loss.backward()
        self.optimizer.update()

    def report(self, episode):
        s = "{0}: loss={1}, q value={2}, epsilon={3}".format(self._step, self._loss, self._qv, self.agent.epsilon)
        self.agent.save(episode)
        return s
<|endoftext|>


<commit_before>import numpy as np
from hyperopt import hp

# required params:
# - embedding_size
# - lr
# - batch_size
# - max_iter
# - neg_ratio
# - contiguous_sampling
# - valid_every: set it to 0 to enable early stopping

param_space_TransE = {
	"embedding_size": hp.quniform("embedding_size", 50, 200, 10),
	"margin": hp.quniform("margin", 0.5, 5, 0.5),
	"lr": hp.qloguniform("lr", np.log(1e-3), np.log(1e-1), 1e-3),
	"batch_size": 5000,
	"max_iter": 100000,
	"neg_ratio": 1,
	"contiguous_sampling": False,
	"valid_every": 5000,
}

param_space_best_TransE_L2_wn18 = {
	"embedding_size": 200,
	"margin": 0.5,
	"lr": 0.001,
	"batch_size": 2000,
	"max_iter": 2000,
	"neg_ratio": 10,
	"contiguous_sampling": False,
	"valid_every": 0,
}

param_space_best_TransE_L1_fb15k = {
	"embedding_size": 190,
	"margin": 3.5,
	"lr": 0.001,
	"batch_size": 5000,
	"max_iter": 15000,
	"neg_ratio": 1,
	"contiguous_sampling": False,
	"valid_every": 0,
}

param_space_DistMult = {
	"embedding_size": hp.quniform("embedding_size", 50, 200, 10),
	"l2_reg_lambda": hp.qloguniform("l2_reg_lambda", np.log(1e-4), np.log(5e-2), 1e-4),
	"lr": hp.qloguniform("lr", np.log(1e-3), np.log(1e-1), 1e-3),
	"batch_size": 5000,
	"max_iter": 100000,
	"neg_ratio": 1,
	"contiguous_sampling": False,
	"valid_every": 5000,
}

param_space_best_DistMult_tanh_wn18 = {
	"embedding_size": 150,
	"l2_reg_lambda": 0.0026,
	"lr": 0.011,
	"batch_size": 2000,
	"max_iter": 15000,
	"neg_ratio": 1,
	"contiguous_sampling": False,
	"valid_every": 0,
}

param_space_best_DistMult_tanh_fb15k = {
	"embedding_size": 200,
	"l2_reg_lambda": 0.0009,
	"lr": 0.001,
	"batch_size": 5000,
	"max_iter": 55000,
	"neg_ratio": 10,
	"contiguous_sampling": False,
	"valid_every": 0,
}

param_space_NTN = {
	"embedding_size": hp.quniform("embedding_size", 50, 200, 10),
	"k": 2,
	"l2_reg_lambda": hp.qloguniform("l2_reg_lambda", np.log(1e-4), np.log(1e-3), 1e-4),
	"lr": hp.qloguniform("lr", np.log(1e-3), np.log(1e-1), 1e-3),
	"batch_size": 5000,
	"max_iter": 100000,
	"neg_ratio": 1,
	"contiguous_sampling": False,
	"valid_every": 5000,
}

param_space_best_NTN_wn18 = {
	"embedding_size": 66,
	"k": 2,
	"l2_reg_lambda": 0.0002,
	"lr": 0.001,
	"batch_size": 2000,
	"max_iter": 100000,
	"neg_ratio": 1,
	"contiguous_sampling": False,
	"valid_every": 0,
}

param_space_best_NTN_fb15k = {
	"embedding_size": 120,
	"k": 2,
	"l2_reg_lambda": 0.0001,
	"lr": 0.001,
	"batch_size": 5000,
	"max_iter": 50000,
	"neg_ratio": 1,
	"contiguous_sampling": False,
	"valid_every": 0,
}

param_space_Complex = {
	"embedding_size": hp.quniform("embedding_size", 50, 200, 10),
	"l2_reg_lambda": hp.qloguniform("l2_reg_lambda", np.log(1e-4), np.log(5e-2), 1e-4),
	"lr": hp.qloguniform("lr", np.log(1e-3), np.log(1e-1), 1e-3),
	"batch_size": 2000,
	"max_iter": 100000,
	"neg_ratio": 1,
	"contiguous_sampling": False,
	"valid_every": 5000,
}

param_space_best_Complex_wn18 = {
	"embedding_size": 180,
	"l2_reg_lambda": 0.0073,
	"lr": 0.002,
	"batch_size": 2000,
	"max_iter": 25000,
	"neg_ratio": 1,
	"contiguous_sampling": False,
	"valid_every": 5000,
}

param_space_best_Complex_tanh_fb15k = {
	"embedding_size": 140,
	"l2_reg_lambda": 0.0172,
	"lr": 0.001,
	"batch_size": 5000,
	"max_iter": 80000,
	"neg_ratio": 10,
	"contiguous_sampling": False,
	"valid_every": 5000,
}

param_space_dict = {
	"TransE_L2": param_space_TransE,
	"TransE_L1": param_space_TransE,
	"best_TransE_L2_wn18": param_space_best_TransE_L2_wn18,
	"best_TransE_L1_fb15k": param_space_best_TransE_L1_fb15k,
	"DistMult": param_space_DistMult,
	"DistMult_tanh": param_space_DistMult,
	"best_DistMult_tanh_wn18": param_space_best_DistMult_tanh_wn18,
	"best_DistMult_tanh_fb15k": param_space_best_DistMult_tanh_fb15k,
	"NTN": param_space_NTN,
	"best_NTN_wn18": param_space_best_NTN_wn18,
	"best_NTN_fb15k": param_space_best_NTN_fb15k,
	"Complex": param_space_Complex,
	"Complex_tanh": param_space_Complex,
	"best_Complex_wn18": param_space_best_Complex_wn18,
	"best_Complex_tanh_fb15k": param_space_best_Complex_tanh_fb15k,
}

int_params = [
	"embedding_size", "batch_size", "max_iter", "neg_ratio", "valid_every", "k",
]

class ModelParamSpace:
	def __init__(self, learner_name):
		s = "Invalid model name! (Check model_param_space.py)"
		assert learner_name in param_space_dict, s
		self.learner_name = learner_name

	def _build_space(self):
		return param_space_dict[self.learner_name]

	def _convert_into_param(self, param_dict):
		if isinstance(param_dict, dict):
			for k,v in param_dict.items():
				if k in int_params:
					param_dict[k] = int(v)
				elif isinstance(v, list) or isinstance(v, tuple):
					for i in range(len(v)):
						self._convert_into_param(v[i])
				elif isinstance(v, dict):
					self._convert_into_param(v)
		return param_dict
<commit_msg>add param space: DistMult_fb3m<commit_after>import numpy as np
from hyperopt import hp

# required params:
# - embedding_size
# - lr
# - batch_size
# - max_iter
# - neg_ratio
# - contiguous_sampling
# - valid_every: set it to 0 to enable early stopping

param_space_TransE = {
	"embedding_size": hp.quniform("embedding_size", 50, 200, 10),
	"margin": hp.quniform("margin", 0.5, 5, 0.5),
	"lr": hp.qloguniform("lr", np.log(1e-3), np.log(1e-1), 1e-3),
	"batch_size": 5000,
	"max_iter": 100000,
	"neg_ratio": 1,
	"contiguous_sampling": False,
	"valid_every": 5000,
}

param_space_best_TransE_L2_wn18 = {
	"embedding_size": 200,
	"margin": 0.5,
	"lr": 0.001,
	"batch_size": 2000,
	"max_iter": 2000,
	"neg_ratio": 10,
	"contiguous_sampling": False,
	"valid_every": 0,
}

param_space_best_TransE_L1_fb15k = {
	"embedding_size": 190,
	"margin": 3.5,
	"lr": 0.001,
	"batch_size": 5000,
	"max_iter": 15000,
	"neg_ratio": 1,
	"contiguous_sampling": False,
	"valid_every": 0,
}

param_space_DistMult = {
	"embedding_size": hp.quniform("embedding_size", 50, 200, 10),
	"l2_reg_lambda": hp.qloguniform("l2_reg_lambda", np.log(1e-4), np.log(5e-2), 1e-4),
	"lr": hp.qloguniform("lr", np.log(1e-3), np.log(1e-1), 1e-3),
	"batch_size": 5000,
	"max_iter": 100000,
	"neg_ratio": 1,
	"contiguous_sampling": False,
	"valid_every": 5000,
}

param_space_DistMult_fb3m = {
	"embedding_size": 50,
	"l2_reg_lambda": hp.qloguniform("l2_reg_lambda", np.log(1e-4), np.log(5e-2), 1e-4),
	"lr": hp.qloguniform("lr", np.log(1e-3), np.log(1e-1), 1e-3),
	"batch_size": 2000,
	"max_iter": 300000,
	"neg_ratio": 1,
	"contiguous_sampling": False,
	"valid_every": 10000,
}

param_space_best_DistMult_tanh_wn18 = {
	"embedding_size": 150,
	"l2_reg_lambda": 0.0026,
	"lr": 0.011,
	"batch_size": 2000,
	"max_iter": 15000,
	"neg_ratio": 1,
	"contiguous_sampling": False,
	"valid_every": 0,
}

param_space_best_DistMult_tanh_fb15k = {
	"embedding_size": 200,
	"l2_reg_lambda": 0.0009,
	"lr": 0.001,
	"batch_size": 5000,
	"max_iter": 55000,
	"neg_ratio": 10,
	"contiguous_sampling": False,
	"valid_every": 0,
}

param_space_NTN = {
	"embedding_size": hp.quniform("embedding_size", 50, 200, 10),
	"k": 2,
	"l2_reg_lambda": hp.qloguniform("l2_reg_lambda", np.log(1e-4), np.log(1e-3), 1e-4),
	"lr": hp.qloguniform("lr", np.log(1e-3), np.log(1e-1), 1e-3),
	"batch_size": 5000,
	"max_iter": 100000,
	"neg_ratio": 1,
	"contiguous_sampling": False,
	"valid_every": 5000,
}

param_space_best_NTN_wn18 = {
	"embedding_size": 66,
	"k": 2,
	"l2_reg_lambda": 0.0002,
	"lr": 0.001,
	"batch_size": 2000,
	"max_iter": 100000,
	"neg_ratio": 1,
	"contiguous_sampling": False,
	"valid_every": 0,
}

param_space_best_NTN_fb15k = {
	"embedding_size": 120,
	"k": 2,
	"l2_reg_lambda": 0.0001,
	"lr": 0.001,
	"batch_size": 5000,
	"max_iter": 50000,
	"neg_ratio": 1,
	"contiguous_sampling": False,
	"valid_every": 0,
}

param_space_Complex = {
	"embedding_size": hp.quniform("embedding_size", 50, 200, 10),
	"l2_reg_lambda": hp.qloguniform("l2_reg_lambda", np.log(1e-4), np.log(5e-2), 1e-4),
	"lr": hp.qloguniform("lr", np.log(1e-3), np.log(1e-1), 1e-3),
	"batch_size": 2000,
	"max_iter": 100000,
	"neg_ratio": 1,
	"contiguous_sampling": False,
	"valid_every": 5000,
}

param_space_best_Complex_wn18 = {
	"embedding_size": 180,
	"l2_reg_lambda": 0.0073,
	"lr": 0.002,
	"batch_size": 2000,
	"max_iter": 25000,
	"neg_ratio": 1,
	"contiguous_sampling": False,
	"valid_every": 5000,
}

param_space_best_Complex_tanh_fb15k = {
	"embedding_size": 140,
	"l2_reg_lambda": 0.0172,
	"lr": 0.001,
	"batch_size": 5000,
	"max_iter": 80000,
	"neg_ratio": 10,
	"contiguous_sampling": False,
	"valid_every": 5000,
}

param_space_dict = {
	"TransE_L2": param_space_TransE,
	"TransE_L1": param_space_TransE,
	"best_TransE_L2_wn18": param_space_best_TransE_L2_wn18,
	"best_TransE_L1_fb15k": param_space_best_TransE_L1_fb15k,
	"DistMult": param_space_DistMult,
	"DistMult_tanh": param_space_DistMult,
	"best_DistMult_tanh_wn18": param_space_best_DistMult_tanh_wn18,
	"best_DistMult_tanh_fb15k": param_space_best_DistMult_tanh_fb15k,
	"NTN": param_space_NTN,
	"best_NTN_wn18": param_space_best_NTN_wn18,
	"best_NTN_fb15k": param_space_best_NTN_fb15k,
	"Complex": param_space_Complex,
	"Complex_tanh": param_space_Complex,
	"best_Complex_wn18": param_space_best_Complex_wn18,
	"best_Complex_tanh_fb15k": param_space_best_Complex_tanh_fb15k,
	"DistMult_tanh_fb3m": param_space_DistMult_fb3m,
}

int_params = [
	"embedding_size", "batch_size", "max_iter", "neg_ratio", "valid_every", "k",
]

class ModelParamSpace:
	def __init__(self, learner_name):
		s = "Invalid model name! (Check model_param_space.py)"
		assert learner_name in param_space_dict, s
		self.learner_name = learner_name

	def _build_space(self):
		return param_space_dict[self.learner_name]

	def _convert_into_param(self, param_dict):
		if isinstance(param_dict, dict):
			for k,v in param_dict.items():
				if k in int_params:
					param_dict[k] = int(v)
				elif isinstance(v, list) or isinstance(v, tuple):
					for i in range(len(v)):
						self._convert_into_param(v[i])
				elif isinstance(v, dict):
					self._convert_into_param(v)
		return param_dict
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2015 XCG Consulting
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp.osv import osv, fields
from openerp.tools.translate import _

class account_sdd_mandate(osv.Model):
    """
    Represents the mandate document used to generate SDD SEPA payments
    """
    _name = 'account.sdd.mandate'

    def create(self, cr, uid, vals, context=None):
        if(not vals['ultimate_debtor_id']):
            vals['ultimate_debtor'] = vals['debtor']

        if(not vals['ultimate_creditor_id']):
            vals['ultimate_creditor'] = vals['ultimate_creditor']

        return super(account_sdd_mandate, self).write(self, cr, uid, vals,
                                                      context)

    def get_sequence_type(self, cr, uid, id, number, context=None):
        """
        Returns the 'sequence type' of the SDD SEPA mandate.
        This value depends on the number of times this mandate was
        used in the context of a 'payment information' block.

        :param number: integer greater or equal to one,
                       increment this value for each batch
        :return: string, one of 'OOFF', 'RCUR', 'FRST', 'FNAL'
        """
        mandate_obj = self.pool['account.sdd.mandate']
        mandate_id = self.browse(cr, uid, id, context)

        if mandate_obj.sequence_type == 'recurring':
            count = mandate_obj.count + number
            if mandate_id.count == 0:
                seq_type = 'RCUR'
            elif mandate_id.count and count < mandate_id.count:
                seq_type = 'RCUR'
            else:
                seq_type = 'FNAL'
        else:
            seq_type = 'OOFF'

        return seq_type

    _columns = {
        'active': fields.boolean(
            string='Active',
        ),
        'identification': fields.char(
            string='Mandate Identification',
            size=128,
        ),
        'debtor_id': fields.many2one(
            'res.partner',
            string='Debtor',
        ),
        'debtor_account_id': fields.many2one(
            'account.account',
            string='Debtor Account',
        ),
        'debtor_agent_id': fields.many2one(
            'res.partner',
            string='Financial Institution',
        ),
        'creditor_id': fields.many2one(
            'res.partner',
            string='Creditor Name',
        ),
        'count': fields.integer(
            string='Count',
            help='Number of times this mandate has been used'
        ),
        'sequence_type': fields.selection(
            [('one_off', 'One Off'),
             ('recurring', 'Recurring')],
            string='Sequence Type',
        ),
        'date_of_signature': fields.date(
            string='Date of Signature',
        ),
        'ultimate_debtor_id': fields.many2one(
            'res.partner',
            string='Ultimate Debtor',
            help='Can be the same as the Debtor',
        ),
        'ultimate_creditor_id': fields.many2one(
            'res.partner',
            string='Ultimate Debtor',
            help='Can be the same as the Creditor',
        ),
        'amends_mandate': fields.boolean(
            string='Amends another mandate',
            help='Means that this mandate replace another if ticked'
        ),
        'original_mandate_id': fields.many2one(
            'account.sdd.mandate',
            string='Original Mandate',
            help='Must be specified if the mandate is the'
                 'amendment of another',
        )
    }

    _defaults = {
        'active': True,
    }
<commit_msg>Added logic to automatically fill in fields in 'account.sdd.mandate' 'create'<commit_after># -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2015 XCG Consulting
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp.osv import osv, fields
from openerp.tools.translate import _

class account_sdd_mandate(osv.Model):
    """
    Represents the mandate document used to generate SDD SEPA payments
    """
    _name = 'account.sdd.mandate'

    def create(self, cr, uid, vals, context=None):
        if(not vals['ultimate_debtor_id']):
            vals['ultimate_debtor'] = vals['debtor']

        if(not vals['ultimate_creditor_id']):
            vals['ultimate_creditor'] = vals['ultimate_creditor']

        res_partner_osv = self.pool['res.partner']
        debtor = res_partner_osv.browse(cr, uid, [vals['ultimate_debtor_id']],
                                        context=context)[0]

        if not debtor.bank_ids:
            raise osv.except_osv('Error', _(
                'No bank accounts associated with {}').format(debtor)
            )
        vals['debtor_account_id'] = debtor.bank_ids[0].id
        vals['debtor_agent_id'] = debtor.bank_ids[0].bank.id

        return super(account_sdd_mandate, self).create(cr, uid, vals,
                                                       context=context)

    def get_sequence_type(self, cr, uid, id, number, context=None):
        """
        Returns the 'sequence type' of the SDD SEPA mandate.
        This value depends on the number of times this mandate was
        used in the context of a 'payment information' block.

        :param number: integer greater or equal to one,
                       increment this value for each batch
        :return: string, one of 'OOFF', 'RCUR', 'FRST', 'FNAL'
        """
        mandate_obj = self.pool['account.sdd.mandate']
        mandate_id = self.browse(cr, uid, id, context)

        if mandate_obj.sequence_type == 'recurring':
            count = mandate_obj.count + number
            if mandate_id.count == 0:
                seq_type = 'RCUR'
            elif mandate_id.count and count < mandate_id.count:
                seq_type = 'RCUR'
            else:
                seq_type = 'FNAL'
        else:
            seq_type = 'OOFF'

        return seq_type

    _columns = {
        'active': fields.boolean(
            string='Active',
        ),
        'identification': fields.char(
            string='Mandate Identification',
            size=128,
        ),
        'debtor_id': fields.many2one(
            'res.partner',
            string='Debtor',
        ),
        'debtor_account_id': fields.many2one(
            'account.account',
            string='Debtor Account',
        ),
        'debtor_agent_id': fields.many2one(
            'res.partner',
            string='Financial Institution',
        ),
        'creditor_id': fields.many2one(
            'res.partner',
            string='Creditor Name',
        ),
        'count': fields.integer(
            string='Count',
            help='Number of times this mandate has been used'
        ),
        'sequence_type': fields.selection(
            [('one_off', 'One Off'),
             ('recurring', 'Recurring')],
            string='Sequence Type',
        ),
        'date_of_signature': fields.date(
            string='Date of Signature',
        ),
        'ultimate_debtor_id': fields.many2one(
            'res.partner',
            string='Ultimate Debtor',
            help='Can be the same as the Debtor',
        ),
        'ultimate_creditor_id': fields.many2one(
            'res.partner',
            string='Ultimate Debtor',
            help='Can be the same as the Creditor',
        ),
        'amends_mandate': fields.boolean(
            string='Amends another mandate',
            help='Means that this mandate replace another if ticked'
        ),
        'original_mandate_id': fields.many2one(
            'account.sdd.mandate',
            string='Original Mandate',
            help='Must be specified if the mandate is the'
                 'amendment of another',
        )
    }

    _defaults = {
        'active': True,
    }
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2015 XCG Consulting
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp.osv import osv, fields
from openerp.tools.translate import _

class account_sdd_mandate(osv.Model):
    """
    Represents the mandate document used to generate SDD SEPA payments
    """
    _name = 'account.sdd.mandate'

    def create(self, cr, uid, vals, context=None):
        if(not vals['ultimate_debtor_id']):
            vals['ultimate_debtor'] = vals['debtor']

        if(not vals['ultimate_creditor_id']):
            vals['ultimate_creditor'] = vals['ultimate_creditor']

        res_partner_osv = self.pool['res.partner']
        debtor = res_partner_osv.browse(cr, uid, [vals['ultimate_debtor_id']],
                                        context=context)[0]

        if not debtor.bank_ids:
            raise osv.except_osv('Error', _(
                'No bank accounts associated with {}').format(debtor)
            )
        vals['debtor_account_id'] = debtor.bank_ids[0].id
        vals['debtor_agent_id'] = debtor.bank_ids[0].bank.id

        return super(account_sdd_mandate, self).create(cr, uid, vals,
                                                       context=context)

    def get_sequence_type(self, cr, uid, id, number, context=None):
        """
        Returns the 'sequence type' of the SDD SEPA mandate.
        This value depends on the number of times this mandate was
        used in the context of a 'payment information' block.

        :param number: integer greater or equal to one,
                       increment this value for each batch
        :return: string, one of 'OOFF', 'RCUR', 'FRST', 'FNAL'
        """
        mandate_obj = self.pool['account.sdd.mandate']
        mandate_id = self.browse(cr, uid, id, context)

        if mandate_obj.sequence_type == 'recurring':
            count = mandate_obj.count + number
            if mandate_id.count == 0:
                seq_type = 'RCUR'
            elif mandate_id.count and count < mandate_id.count:
                seq_type = 'RCUR'
            else:
                seq_type = 'FNAL'
        else:
            seq_type = 'OOFF'

        return seq_type

    _columns = {
        'active': fields.boolean(
            string='Active',
        ),
        'identification': fields.char(
            string='Mandate Identification',
            size=128,
            required=True,
        ),
        'debtor_id': fields.many2one(
            'res.partner',
            string='Debtor',
            required=True,
        ),
        'debtor_account_id': fields.many2one(
            'res.partner.bank',
            string='Debtor Account',
            domain='[("partner_id", "=", ultimate_debtor_id)]',
        ),
        'debtor_agent_id': fields.many2one(
            'res.partner',
            string='Financial Institution',
        ),
        'creditor_id': fields.many2one(
            'res.partner',
            string='Creditor Name',
            required=True,
        ),
        'count': fields.integer(
            string='Count',
            help='Number of times this mandate has been used'
        ),
        'sequence_type': fields.selection(
            [('one_off', 'One Off'),
             ('recurring', 'Recurring')],
            string='Sequence Type',
        ),
        'date_of_signature': fields.date(
            string='Date of Signature',
            required=True,
        ),
        'ultimate_debtor_id': fields.many2one(
            'res.partner',
            string='Ultimate Debtor',
            help='Can be the same as the Debtor',
            domain='''[
                "|",
                    ("id", "=", debtor_id),
                    ("id", "child_of", "debtor_id")
            ]''',
        ),
        'ultimate_creditor_id': fields.many2one(
            'res.partner',
            string='Ultimate Debtor',
            help='Can be the same as the Creditor',
            domain='''[
                "|",
                    ("id", "=", creditor_id),
                    ("id", "child_of", "creditor_id")
            ]''',
        ),
        'amends_mandate': fields.boolean(
            string='Amends another mandate',
            help='Means that this mandate replace another if ticked'
        ),
        'original_mandate_id': fields.many2one(
            'account.sdd.mandate',
            string='Original Mandate',
            help='Must be specified if the mandate is the'
                 'amendment of another',
        )
    }

    _defaults = {
        'active': True,
    }

    def _check_original_mandate(self, cr, uid, ids, context=None):
        """
        Makes sure that 'original_mandate_id' is filled in when
        'ammends_mandate' is  checked
        """
        for record in self.browse(cr, uid, ids, context=context):
            if record.amends_mandate and not record.original_mandate_id:
                return False
        return True

    _constraints = [
        (_check_original_mandate,
         'Please specify mandate',
         ['amends_mandate', 'original_mandate_id'])
    ]
<commit_msg>Changed field 'debtor_agent_id' in 'account.sdd.mandate'<commit_after># -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2015 XCG Consulting
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp.osv import osv, fields
from openerp.tools.translate import _

class account_sdd_mandate(osv.Model):
    """
    Represents the mandate document used to generate SDD SEPA payments
    """
    _name = 'account.sdd.mandate'

    def create(self, cr, uid, vals, context=None):
        if(not vals['ultimate_debtor_id']):
            vals['ultimate_debtor'] = vals['debtor']

        if(not vals['ultimate_creditor_id']):
            vals['ultimate_creditor'] = vals['ultimate_creditor']

        res_partner_osv = self.pool['res.partner']
        debtor = res_partner_osv.browse(cr, uid, [vals['ultimate_debtor_id']],
                                        context=context)[0]

        if not debtor.bank_ids:
            raise osv.except_osv('Error', _(
                'No bank accounts associated with {}').format(debtor)
            )
        vals['debtor_account_id'] = debtor.bank_ids[0].id
        vals['debtor_agent_id'] = debtor.bank_ids[0].bank.id

        return super(account_sdd_mandate, self).create(cr, uid, vals,
                                                       context=context)

    def get_sequence_type(self, cr, uid, id, number, context=None):
        """
        Returns the 'sequence type' of the SDD SEPA mandate.
        This value depends on the number of times this mandate was
        used in the context of a 'payment information' block.

        :param number: integer greater or equal to one,
                       increment this value for each batch
        :return: string, one of 'OOFF', 'RCUR', 'FRST', 'FNAL'
        """
        mandate_obj = self.pool['account.sdd.mandate']
        mandate_id = self.browse(cr, uid, id, context)

        if mandate_obj.sequence_type == 'recurring':
            count = mandate_obj.count + number
            if mandate_id.count == 0:
                seq_type = 'RCUR'
            elif mandate_id.count and count < mandate_id.count:
                seq_type = 'RCUR'
            else:
                seq_type = 'FNAL'
        else:
            seq_type = 'OOFF'

        return seq_type

    _columns = {
        'active': fields.boolean(
            string='Active',
        ),
        'identification': fields.char(
            string='Mandate Identification',
            size=128,
            required=True,
        ),
        'debtor_id': fields.many2one(
            'res.partner',
            string='Debtor',
            required=True,
        ),
        'debtor_account_id': fields.many2one(
            'res.partner.bank',
            string='Debtor Account',
            domain='[("partner_id", "=", ultimate_debtor_id)]',
        ),
        'debtor_agent_id': fields.many2one(
            'res.bank',
            string='Financial Institution',
        ),
        'creditor_id': fields.many2one(
            'res.partner',
            string='Creditor Name',
            required=True,
        ),
        'count': fields.integer(
            string='Count',
            help='Number of times this mandate has been used'
        ),
        'sequence_type': fields.selection(
            [('one_off', 'One Off'),
             ('recurring', 'Recurring')],
            string='Sequence Type',
        ),
        'date_of_signature': fields.date(
            string='Date of Signature',
            required=True,
        ),
        'ultimate_debtor_id': fields.many2one(
            'res.partner',
            string='Ultimate Debtor',
            help='Can be the same as the Debtor',
            domain='''[
                "|",
                    ("id", "=", debtor_id),
                    ("id", "child_of", "debtor_id")
            ]''',
        ),
        'ultimate_creditor_id': fields.many2one(
            'res.partner',
            string='Ultimate Debtor',
            help='Can be the same as the Creditor',
            domain='''[
                "|",
                    ("id", "=", creditor_id),
                    ("id", "child_of", "creditor_id")
            ]''',
        ),
        'amends_mandate': fields.boolean(
            string='Amends another mandate',
            help='Means that this mandate replace another if ticked'
        ),
        'original_mandate_id': fields.many2one(
            'account.sdd.mandate',
            string='Original Mandate',
            help='Must be specified if the mandate is the'
                 'amendment of another',
        )
    }

    _defaults = {
        'active': True,
    }

    def _check_original_mandate(self, cr, uid, ids, context=None):
        """
        Makes sure that 'original_mandate_id' is filled in when
        'ammends_mandate' is  checked
        """
        for record in self.browse(cr, uid, ids, context=context):
            if record.amends_mandate and not record.original_mandate_id:
                return False
        return True

    _constraints = [
        (_check_original_mandate,
         'Please specify mandate',
         ['amends_mandate', 'original_mandate_id'])
    ]
<|endoftext|>


<commit_before># python3
# Copyright 2018 DeepMind Technologies Limited. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Utility function for building and launching launchpad programs."""

import atexit
import functools
import inspect
import os
import sys
import time
from typing import Any, Callable, Optional

from absl import flags
from absl import logging
from acme.utils import counting
import launchpad as lp

FLAGS = flags.FLAGS

def partial_kwargs(function: Callable[..., Any],
                   **kwargs: Any) -> Callable[..., Any]:
  """Return a partial function application by overriding default keywords.

  This function is equivalent to `functools.partial(function, **kwargs)` but
  will raise a `ValueError` when called if either the given keyword arguments
  are not defined by `function` or if they do not have defaults.

  This is useful as a way to define a factory function with default parameters
  and then to override them in a safe way.

  Args:
    function: the base function before partial application.
    **kwargs: keyword argument overrides.

  Returns:
    A function.
  """
  # Try to get the argspec of our function which we'll use to get which keywords
  # have defaults.
  argspec = inspect.getfullargspec(function)

  # Figure out which keywords have defaults.
  if argspec.defaults is None:
    defaults = []
  else:
    defaults = argspec.args[-len(argspec.defaults):]

  # Find any keys not given as defaults by the function.
  unknown_kwargs = set(kwargs.keys()).difference(defaults)

  # Raise an error
  if unknown_kwargs:
    error_string = 'Cannot override unknown or non-default kwargs: {}'
    raise ValueError(error_string.format(', '.join(unknown_kwargs)))

  return functools.partial(function, **kwargs)

class StepsLimiter:
  """Process that terminates an experiment when `max_steps` is reached."""

  def __init__(self,
               counter: counting.Counter,
               max_steps: int,
               steps_key: str = 'actor_steps'):
    self._counter = counter
    self._max_steps = max_steps
    self._steps_key = steps_key

  def run(self):
    """Run steps limiter to terminate an experiment when max_steps is reached.
    """

    logging.info('StepsLimiter: Starting with max_steps = %d (%s)',
                 self._max_steps, self._steps_key)
    while True:
      # Update the counts.
      counts = self._counter.get_counts()
      num_steps = counts.get(self._steps_key, 0)

      logging.info('StepsLimiter: Reached %d recorded steps', num_steps)

      if num_steps > self._max_steps:
        logging.info('StepsLimiter: Max steps of %d was reached, terminating',
                     self._max_steps)
        lp.stop()

      # Don't spam the counter.
      for _ in range(10):
        # Do not sleep for a long period of time to avoid LaunchPad program
        # termination hangs (time.sleep is not interruptible).
        time.sleep(1)

# Resources for each individual instance of the program.
def make_xm_docker_resources(program: lp.Program,
                             requirements: Optional[str] = None):
  """Returns Docker XManager resources for each program's node.

  For each node of the Launchpad's program appropriate hardware requirements are
  specified (CPU, memory...), while the list of PyPi packages specified in
  the requirements file will be installed inside the Docker images.

  Args:
    program: program for which to construct Docker XManager resources.
    requirements: file containing additional requirements to use.
      If not specified, default Acme dependencies are used instead.
  """
  if (FLAGS.lp_launch_type != 'vertex_ai' and
      FLAGS.lp_launch_type != 'local_docker'):
    # Avoid importing 'xmanager' for local runs.
    return None

  # Reference lp.DockerConfig to force lazy import of xmanager by Launchpad and
  # then import it. It is done this way to avoid heavy imports by default.
  lp.DockerConfig  # pylint: disable=pointless-statement
  from xmanager import xm  # pylint: disable=g-import-not-at-top

  # Get number of each type of node.
  num_nodes = {k: len(v) for k, v in program.groups.items()}

  xm_resources = {}

  acme_location = os.path.dirname(os.path.dirname(__file__))
  if not requirements:
    # Acme requirements are located in the Acme directory (when installed
    # with pip), or need to be extracted from setup.py when using Acme codebase
    # from GitHub without PyPi installation.
    requirements = os.path.join(acme_location, 'requirements.txt')
    if not os.path.isfile(requirements):
      # Try to generate requirements.txt from setup.py
      setup = os.path.join(os.path.dirname(acme_location), 'setup.py')
      if os.path.isfile(setup):
        # Generate requirements.txt file using setup.py.
        import importlib.util  # pylint: disable=g-import-not-at-top
        spec = importlib.util.spec_from_file_location('setup', setup)
        setup = importlib.util.module_from_spec(spec)
        try:
          spec.loader.exec_module(setup)  # pytype: disable=attribute-error
        except SystemExit:
          pass
        atexit.register(os.remove, requirements)
        setup.generate_requirements_file(requirements)

  # Extend PYTHONPATH with paths used by the launcher.
  python_path = []
  for path in sys.path:
    if path.startswith(acme_location) and acme_location != path:
      python_path.append(path[len(acme_location):])

  if 'replay' in num_nodes:
    replay_cpu = 6 + num_nodes.get('actor', 0) * 0.01
    replay_cpu = min(40, replay_cpu)

    xm_resources['replay'] = lp.DockerConfig(
        acme_location,
        requirements,
        hw_requirements=xm.JobRequirements(cpu=replay_cpu, ram=10 * xm.GiB),
        python_path=python_path)

  if 'evaluator' in num_nodes:
    xm_resources['evaluator'] = lp.DockerConfig(
        acme_location,
        requirements,
        hw_requirements=xm.JobRequirements(cpu=2, ram=4 * xm.GiB),
        python_path=python_path)

  if 'actor' in num_nodes:
    xm_resources['actor'] = lp.DockerConfig(
        acme_location,
        requirements,
        hw_requirements=xm.JobRequirements(cpu=2, ram=4 * xm.GiB),
        python_path=python_path)

  if 'learner' in num_nodes:
    learner_cpu = 6 + num_nodes.get('actor', 0) * 0.01
    learner_cpu = min(40, learner_cpu)
    xm_resources['learner'] = lp.DockerConfig(
        acme_location,
        requirements,
        hw_requirements=xm.JobRequirements(
            cpu=learner_cpu, ram=6 * xm.GiB, P100=1),
        python_path=python_path)

  if 'environment_loop' in num_nodes:
    xm_resources['environment_loop'] = lp.DockerConfig(
        acme_location,
        requirements,
        hw_requirements=xm.JobRequirements(
            cpu=6, ram=6 * xm.GiB, P100=1),
        python_path=python_path)

  if 'counter' in num_nodes:
    xm_resources['counter'] = lp.DockerConfig(
        acme_location,
        requirements,
        hw_requirements=xm.JobRequirements(cpu=3, ram=4 * xm.GiB),
        python_path=python_path)

  if 'cacher' in num_nodes:
    xm_resources['cacher'] = lp.DockerConfig(
        acme_location,
        requirements,
        hw_requirements=xm.JobRequirements(cpu=3, ram=6 * xm.GiB),
        python_path=python_path)

  return xm_resources
<commit_msg>Avoid importing launchpad if not actually used (by non-distributed agents).<commit_after># python3
# Copyright 2018 DeepMind Technologies Limited. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Utility function for building and launching launchpad programs."""

import atexit
import functools
import inspect
import os
import sys
import time
from typing import Any, Callable

from absl import flags
from absl import logging
from acme.utils import counting

FLAGS = flags.FLAGS

def partial_kwargs(function: Callable[..., Any],
                   **kwargs: Any) -> Callable[..., Any]:
  """Return a partial function application by overriding default keywords.

  This function is equivalent to `functools.partial(function, **kwargs)` but
  will raise a `ValueError` when called if either the given keyword arguments
  are not defined by `function` or if they do not have defaults.

  This is useful as a way to define a factory function with default parameters
  and then to override them in a safe way.

  Args:
    function: the base function before partial application.
    **kwargs: keyword argument overrides.

  Returns:
    A function.
  """
  # Try to get the argspec of our function which we'll use to get which keywords
  # have defaults.
  argspec = inspect.getfullargspec(function)

  # Figure out which keywords have defaults.
  if argspec.defaults is None:
    defaults = []
  else:
    defaults = argspec.args[-len(argspec.defaults):]

  # Find any keys not given as defaults by the function.
  unknown_kwargs = set(kwargs.keys()).difference(defaults)

  # Raise an error
  if unknown_kwargs:
    error_string = 'Cannot override unknown or non-default kwargs: {}'
    raise ValueError(error_string.format(', '.join(unknown_kwargs)))

  return functools.partial(function, **kwargs)

class StepsLimiter:
  """Process that terminates an experiment when `max_steps` is reached."""

  def __init__(self,
               counter: counting.Counter,
               max_steps: int,
               steps_key: str = 'actor_steps'):
    self._counter = counter
    self._max_steps = max_steps
    self._steps_key = steps_key

  def run(self):
    """Run steps limiter to terminate an experiment when max_steps is reached.
    """

    logging.info('StepsLimiter: Starting with max_steps = %d (%s)',
                 self._max_steps, self._steps_key)
    while True:
      # Update the counts.
      counts = self._counter.get_counts()
      num_steps = counts.get(self._steps_key, 0)

      logging.info('StepsLimiter: Reached %d recorded steps', num_steps)

      if num_steps > self._max_steps:
        logging.info('StepsLimiter: Max steps of %d was reached, terminating',
                     self._max_steps)
        # Avoid importing Launchpad until it is actually used.
        import launchpad as lp  # pylint: disable=g-import-not-at-top
        lp.stop()

      # Don't spam the counter.
      for _ in range(10):
        # Do not sleep for a long period of time to avoid LaunchPad program
        # termination hangs (time.sleep is not interruptible).
        time.sleep(1)

# Resources for each individual instance of the program.
def make_xm_docker_resources(program, requirements):
  """Returns Docker XManager resources for each program's node.

  For each node of the Launchpad's program appropriate hardware requirements are
  specified (CPU, memory...), while the list of PyPi packages specified in
  the requirements file will be installed inside the Docker images.

  Args:
    program: program for which to construct Docker XManager resources.
    requirements: file containing additional requirements to use.
      If not specified, default Acme dependencies are used instead.
  """
  if (FLAGS.lp_launch_type != 'vertex_ai' and
      FLAGS.lp_launch_type != 'local_docker'):
    # Avoid importing 'xmanager' for local runs.
    return None

  # Avoid importing Launchpad until it is actually used.
  import launchpad as lp  # pylint: disable=g-import-not-at-top
  # Reference lp.DockerConfig to force lazy import of xmanager by Launchpad and
  # then import it. It is done this way to avoid heavy imports by default.
  lp.DockerConfig  # pylint: disable=pointless-statement
  from xmanager import xm  # pylint: disable=g-import-not-at-top

  # Get number of each type of node.
  num_nodes = {k: len(v) for k, v in program.groups.items()}

  xm_resources = {}

  acme_location = os.path.dirname(os.path.dirname(__file__))
  if not requirements:
    # Acme requirements are located in the Acme directory (when installed
    # with pip), or need to be extracted from setup.py when using Acme codebase
    # from GitHub without PyPi installation.
    requirements = os.path.join(acme_location, 'requirements.txt')
    if not os.path.isfile(requirements):
      # Try to generate requirements.txt from setup.py
      setup = os.path.join(os.path.dirname(acme_location), 'setup.py')
      if os.path.isfile(setup):
        # Generate requirements.txt file using setup.py.
        import importlib.util  # pylint: disable=g-import-not-at-top
        spec = importlib.util.spec_from_file_location('setup', setup)
        setup = importlib.util.module_from_spec(spec)
        try:
          spec.loader.exec_module(setup)  # pytype: disable=attribute-error
        except SystemExit:
          pass
        atexit.register(os.remove, requirements)
        setup.generate_requirements_file(requirements)

  # Extend PYTHONPATH with paths used by the launcher.
  python_path = []
  for path in sys.path:
    if path.startswith(acme_location) and acme_location != path:
      python_path.append(path[len(acme_location):])

  if 'replay' in num_nodes:
    replay_cpu = 6 + num_nodes.get('actor', 0) * 0.01
    replay_cpu = min(40, replay_cpu)

    xm_resources['replay'] = lp.DockerConfig(
        acme_location,
        requirements,
        hw_requirements=xm.JobRequirements(cpu=replay_cpu, ram=10 * xm.GiB),
        python_path=python_path)

  if 'evaluator' in num_nodes:
    xm_resources['evaluator'] = lp.DockerConfig(
        acme_location,
        requirements,
        hw_requirements=xm.JobRequirements(cpu=2, ram=4 * xm.GiB),
        python_path=python_path)

  if 'actor' in num_nodes:
    xm_resources['actor'] = lp.DockerConfig(
        acme_location,
        requirements,
        hw_requirements=xm.JobRequirements(cpu=2, ram=4 * xm.GiB),
        python_path=python_path)

  if 'learner' in num_nodes:
    learner_cpu = 6 + num_nodes.get('actor', 0) * 0.01
    learner_cpu = min(40, learner_cpu)
    xm_resources['learner'] = lp.DockerConfig(
        acme_location,
        requirements,
        hw_requirements=xm.JobRequirements(
            cpu=learner_cpu, ram=6 * xm.GiB, P100=1),
        python_path=python_path)

  if 'environment_loop' in num_nodes:
    xm_resources['environment_loop'] = lp.DockerConfig(
        acme_location,
        requirements,
        hw_requirements=xm.JobRequirements(
            cpu=6, ram=6 * xm.GiB, P100=1),
        python_path=python_path)

  if 'counter' in num_nodes:
    xm_resources['counter'] = lp.DockerConfig(
        acme_location,
        requirements,
        hw_requirements=xm.JobRequirements(cpu=3, ram=4 * xm.GiB),
        python_path=python_path)

  if 'cacher' in num_nodes:
    xm_resources['cacher'] = lp.DockerConfig(
        acme_location,
        requirements,
        hw_requirements=xm.JobRequirements(cpu=3, ram=6 * xm.GiB),
        python_path=python_path)

  return xm_resources
<|endoftext|>