<commit_before>"""DSSM Preprocessor."""

from matchzoo import engine
from matchzoo import preprocessor
from matchzoo import datapack

import typing
import logging
from tqdm import tqdm

logger = logging.getLogger(__name__)

class DSSMPreprocessor(engine.BasePreprocessor):
    """
    DSSM preprocessor helper.

    TODO: NEED REFACTORING.

    Example:
        >>> train_inputs = [
        ...     ("id0", "id1", "beijing", "Beijing is capital of China", 1),
        ...     ("id0", "id2", "beijing", "China is in east Asia", 0),
        ...     ("id0", "id3", "beijing", "Summer in Beijing is hot.", 1)
        ... ]
        >>> dssm_preprocessor = DSSMPreprocessor()
        >>> rv_train = dssm_preprocessor.fit_transform(
        ...     train_inputs,
        ...     stage='train')
        >>> dssm_preprocessor.context['input_shapes'][0][0]
        37
        >>> type(rv_train)
        <class 'matchzoo.datapack.DataPack'>
        >>> context = dssm_preprocessor.context
        >>> dssm_preprocessor_test = DSSMPreprocessor()
        >>> dssm_preprocessor_test.context = context
        >>> test_inputs = [("id0",
        ...                 "id4",
        ...                 "beijing",
        ...                 "I visted beijing yesterday.")]
        >>> rv_test = dssm_preprocessor_test.fit_transform(
        ...     test_inputs,
        ...     stage='test')
        >>> type(rv_test)
        <class 'matchzoo.datapack.DataPack'>

    """

    def __init__(self):
        """Initialization."""
        self._context = {}
        self._datapack = None
        self._cache = []

    @property
    def context(self):
        """Get fitted parameters."""
        return self._context

    @context.setter
    def context(self, context: dict):
        """
        Set pre-fitted context.

        :param context: pre-fitted context.
        """
        self._context = context

    def _prepare_stateless_units(self) -> list:
        """Prepare needed process units."""
        return [
            preprocessor.TokenizeUnit(),
            preprocessor.LowercaseUnit(),
            preprocessor.PuncRemovalUnit(),
            preprocessor.StopRemovalUnit(),
            preprocessor.NgramLetterUnit()
        ]

    def fit(self, inputs: typing.List[tuple]):
        """
        Fit pre-processing context for transformation.

        :param inputs: Inputs to be preprocessed.
        :return: class:`DSSMPreprocessor` instance.
        """
        vocab = []
        units = self._prepare_stateless_units()

        logger.info("Start building vocabulary & fitting parameters.")

        # Convert user input into a datapack object.
        self._datapack = self.segmentation(inputs, stage='train')

        # Loop through user input to generate tri-letters.
        # 1. Used for build vocabulary of tri-letters (get dimension).
        # 2. Cached tri-letters can be further used to perform input
        #    transformation.
        df = self._datapack.dataframe

        for idx, text in tqdm(zip(df['id'], df['text'])):
            # For each piece of text, apply process unit sequentially.
            for unit in units:
                text = unit.transform(text)
            vocab.extend(text)
            # cache tri-letters for transformation.
            self._cache.append((idx, text))

        # Initialize a vocabulary process unit to build tri-letter vocab.
        vocab_unit = preprocessor.VocabularyUnit()
        vocab_unit.fit(vocab)

        # Store the fitted parameters in context.
        self._context['term_index'] = vocab_unit.state['term_index']
        dim_triletter = len(vocab_unit.state['term_index']) + 1
        self._context['input_shapes'] = [(dim_triletter,), (dim_triletter,)]
        return self

    def transform(
        self,
        inputs: typing.List[tuple],
        stage: str
    ) -> datapack.DataPack:
        """
        Apply transformation on data, create `tri-letter` representation.

        :param inputsL Inputs to be preprocessed.
        :param stage: Pre-processing stage, `train` or `test`.

        :return: Transformed data as :class:`DataPack` object.
        """
        outputs = []

        if stage not in ['train', 'test']:
            raise ValueError(f'{stage} is not a valid stage name.')
        if not self._context.get('term_index'):
            raise ValueError(
                "Please fit term_index before apply transofm function.")

        # prepare word hashing unit.
        hashing = preprocessor.WordHashingUnit(
            self._context['term_index'])

        logger.info(f"Start processing input data for {stage} stage.")

        if stage == 'train':
            # use cached data to fit word hashing layer directly.
            for idx, tri_letter in tqdm(self._cache):
                outputs.append((idx, hashing.transform(tri_letter)))

            return self._make_output(output=outputs,
                                     mapping=self._datapack.mapping,
                                     context=self._context,
                                     stage=stage)
        else:
            # do preprocessing from scrach.
            units = self._prepare_stateless_units()
            units.append(hashing)
            self._datapack = self.segmentation(inputs, stage='test')

            df = self._datapack.dataframe

            for idx, text in tqdm(zip(df['id'], df['text'])):
                for unit in units:
                    text = unit.transform(text)
                outputs.append((idx, text))

            return self._make_output(output=outputs,
                                     mapping=self._datapack.mapping,
                                     context=self._context,
                                     stage=stage)
<commit_msg>Update dssm_preprocessor.py<commit_after>"""DSSM Preprocessor."""

from matchzoo import engine
from matchzoo import preprocessor
from matchzoo import datapack

import typing
import logging
from tqdm import tqdm

logger = logging.getLogger(__name__)

class DSSMPreprocessor(engine.BasePreprocessor):
    """
    DSSM preprocessor helper.

    TODO: NEED REFACTORING.

    Example:
        >>> train_inputs = [
        ...     ("id0", "id1", "beijing", "Beijing is capital of China", 1),
        ...     ("id0", "id2", "beijing", "China is in east Asia", 0),
        ...     ("id0", "id3", "beijing", "Summer in Beijing is hot.", 1)
        ... ]
        >>> dssm_preprocessor = DSSMPreprocessor()
        >>> rv_train = dssm_preprocessor.fit_transform(
        ...     train_inputs,
        ...     stage='train')
        >>> dssm_preprocessor.context['input_shapes'][0][0]
        37
        >>> type(rv_train)
        <class 'matchzoo.datapack.DataPack'>
        >>> context = dssm_preprocessor.context
        >>> dssm_preprocessor_test = DSSMPreprocessor()
        >>> dssm_preprocessor_test.context = context
        >>> test_inputs = [("id0",
        ...                 "id4",
        ...                 "beijing",
        ...                 "I visted beijing yesterday.")]
        >>> rv_test = dssm_preprocessor_test.fit_transform(
        ...     test_inputs,
        ...     stage='test')
        >>> type(rv_test)
        <class 'matchzoo.datapack.DataPack'>

    """

    def __init__(self):
        """Initialization."""
        self._context = {}
        self._datapack = None
        self._cache = []

    @property
    def context(self):
        """Get fitted parameters."""
        return self._context

    @context.setter
    def context(self, context: dict):
        """
        Set pre-fitted context.

        :param context: pre-fitted context.
        """
        self._context = context

    def _prepare_stateless_units(self) -> list:
        """Prepare needed process units."""
        return [
            preprocessor.TokenizeUnit(),
            preprocessor.LowercaseUnit(),
            preprocessor.PuncRemovalUnit(),
            preprocessor.StopRemovalUnit(),
            preprocessor.NgramLetterUnit()
        ]

    def fit(self, inputs: typing.List[tuple]):
        """
        Fit pre-processing context for transformation.

        :param inputs: Inputs to be preprocessed.
        :return: class:`DSSMPreprocessor` instance.
        """
        vocab = []
        units = self._prepare_stateless_units()

        logger.info("Start building vocabulary & fitting parameters.")

        # Convert user input into a datapack object.
        self._datapack = self.segmentation(inputs, stage='train')

        # Loop through user input to generate tri-letters.
        # 1. Used for build vocabulary of tri-letters (get dimension).
        # 2. Cached tri-letters can be further used to perform input
        #    transformation.
        df = self._datapack.dataframe

        for idx, text in tqdm(zip(df['id'], df['text'])):
            # For each piece of text, apply process unit sequentially.
            for unit in units:
                text = unit.transform(text)
            vocab.extend(text)
            # cache tri-letters for transformation.
            self._cache.append((idx, text))

        # Initialize a vocabulary process unit to build tri-letter vocab.
        vocab_unit = preprocessor.VocabularyUnit()
        vocab_unit.fit(vocab)

        # Store the fitted parameters in context.
        self._context['term_index'] = vocab_unit.state['term_index']
        dim_triletter = len(vocab_unit.state['term_index']) + 1
        self._context['input_shapes'] = [(dim_triletter,), (dim_triletter,)]
        return self

    def transform(
        self,
        inputs: typing.List[tuple],
        stage: str
    ) -> datapack.DataPack:
        """
        Apply transformation on data, create `tri-letter` representation.

        :param inputs: Inputs to be preprocessed.
        :param stage: Pre-processing stage, `train` or `test`.

        :return: Transformed data as :class:`DataPack` object.
        """
        outputs = []

        if stage not in ['train', 'test']:
            raise ValueError(f'{stage} is not a valid stage name.')
        if not self._context.get('term_index'):
            raise ValueError(
                "Please fit term_index before apply transofm function.")

        # prepare word hashing unit.
        hashing = preprocessor.WordHashingUnit(
            self._context['term_index'])

        logger.info(f"Start processing input data for {stage} stage.")

        if stage == 'train':
            # use cached data to fit word hashing layer directly.
            for idx, tri_letter in tqdm(self._cache):
                outputs.append((idx, hashing.transform(tri_letter)))

            return self._make_output(output=outputs,
                                     mapping=self._datapack.mapping,
                                     context=self._context,
                                     stage=stage)
        else:
            # do preprocessing from scrach.
            units = self._prepare_stateless_units()
            units.append(hashing)
            self._datapack = self.segmentation(inputs, stage='test')

            df = self._datapack.dataframe

            for idx, text in tqdm(zip(df['id'], df['text'])):
                for unit in units:
                    text = unit.transform(text)
                outputs.append((idx, text))

            return self._make_output(output=outputs,
                                     mapping=self._datapack.mapping,
                                     context=self._context,
                                     stage=stage)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from threading import Lock
from sockjs.tornado import SockJSConnection
from mindmaptornado.decorators import check_for_component_pk, check_for_data
from tornado.escape import json_decode, json_encode
from .exceptions import HTTPException
from .decorators import check_for_map_pk

class MindmapWebSocketHandler(SockJSConnection):
    _maps_participants = dict()
    _lock = Lock()

    @classmethod
    def _die(cls, *args, **kwargs):
        raise HTTPException(*args, **kwargs)

    def _broadcast_to_map(self, method, data={}):
        data['method'] = method
        self.broadcast(
            clients = [ maps_participant for maps_participant in self._maps_participants[data['map_pk']] if maps_participant != self ],
            message = json_encode(data)
        )

    @check_for_map_pk()
    def _register_myself_as_map_participant(self, data):
        self._lock.acquire()
        self._maps_participants.setdefault(data['map_pk'], set()).add(self)
        self._lock.release()

    @check_for_map_pk()
    @check_for_component_pk()
    @check_for_data('pos')
    def _update_component_pos(self, data):
        print data
        self._broadcast_to_map('update_component_pos', data)

    @check_for_map_pk()
    @check_for_data('offset_left', 'offset_top', 'except_component_pk')
    def _add_components_offset_except_one(self, data):
        print data
        self._broadcast_to_map('add_components_offset_except_one', data)

    def on_message(self, data):
        try:
            data = json_decode(data)
        except ValueError:
            raise self._die(log_message='Unable to decode json')

        if type(data).__name__ != 'dict' or 'method' not in data:
            raise self._die(log_message='data is not a dict or no key "method" in data dict found. Data: %s' % data)

        # define available methods
        methods = {
           'register_myself_as_map_participant': self._register_myself_as_map_participant,
           'update_component_pos': self._update_component_pos,
           'add_components_offset_except_one': self._add_components_offset_except_one,
        }

        # call method
        try:
            method = methods.get(data['method'], None)
        except TypeError:
            self._die('Unknown method "%s" called' % data['method'])

        method(data)

    def on_close(self):
        for map_pk in self._maps_participants.keys():
            if self in self._maps_participants.get(map_pk, []):
                self._lock.acquire()

                # remove client from map
                try:
                    self._maps_participants[map_pk].remove(self)
                except KeyError:
                    pass

                # remove map if no participants remains on map
                try:
                    if not len(self._maps_participants[map_pk]):
                        del self._maps_participants[map_pk]
                except KeyError:
                    pass

                self._lock.release()
<commit_msg>[CLEANUP]<commit_after># -*- coding: utf-8 -*-
from threading import Lock
from sockjs.tornado import SockJSConnection
from tornado.escape import json_decode, json_encode
from .exceptions import HTTPException
from .decorators import check_for_map_pk, check_for_component_pk, check_for_data

class MindmapWebSocketHandler(SockJSConnection):
    _maps_participants = dict()
    _lock = Lock()

    @classmethod
    def _die(cls, *args, **kwargs):
        raise HTTPException(*args, **kwargs)

    def _broadcast_to_map(self, method, data={}):
        data['method'] = method
        self.broadcast(
            clients = [ maps_participant for maps_participant in self._maps_participants[data['map_pk']] if maps_participant != self ],
            message = json_encode(data)
        )

    @check_for_map_pk()
    def _register_myself_as_map_participant(self, data):
        self._lock.acquire()
        self._maps_participants.setdefault(data['map_pk'], set()).add(self)
        self._lock.release()

    @check_for_map_pk()
    @check_for_component_pk()
    @check_for_data('pos')
    def _update_component_pos(self, data):
        print data
        self._broadcast_to_map('update_component_pos', data)

    @check_for_map_pk()
    @check_for_data('offset_left', 'offset_top', 'except_component_pk')
    def _add_components_offset_except_one(self, data):
        print data
        self._broadcast_to_map('add_components_offset_except_one', data)

    def on_message(self, data):
        try:
            data = json_decode(data)
        except ValueError:
            self._die(log_message='Unable to decode json')

        if type(data).__name__ != 'dict' or 'method' not in data:
            self._die(log_message='data is not a dict or no key "method" in data dict found. Data: %s' % data)

        # define available methods
        methods = {
           'register_myself_as_map_participant': self._register_myself_as_map_participant,
           'update_component_pos': self._update_component_pos,
           'add_components_offset_except_one': self._add_components_offset_except_one,
        }

        # call method
        try:
            method = methods.get(data['method'], None)
        except TypeError:
            self._die('Unknown method "%s" called' % data['method'])

        method(data)

    def on_close(self):
        for map_pk in self._maps_participants.keys():
            if self in self._maps_participants.get(map_pk, []):
                self._lock.acquire()

                # remove client from map
                try:
                    self._maps_participants[map_pk].remove(self)
                except KeyError:
                    pass

                # remove map if no participants remains on map
                try:
                    if not len(self._maps_participants[map_pk]):
                        del self._maps_participants[map_pk]
                except KeyError:
                    pass

                self._lock.release()
<|endoftext|>


<commit_before># This file is part of QuTiP: Quantum Toolbox in Python.
#
#    Copyright (c) 2011 and later, Paul D. Nation and Robert J. Johansson.
#    All rights reserved.
#
#    Redistribution and use in source and binary forms, with or without
#    modification, are permitted provided that the following conditions are
#    met:
#
#    1. Redistributions of source code must retain the above copyright notice,
#       this list of conditions and the following disclaimer.
#
#    2. Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#
#    3. Neither the name of the QuTiP: Quantum Toolbox in Python nor the names
#       of its contributors may be used to endorse or promote products derived
#       from this software without specific prior written permission.
#
#    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
#    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
#    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
#    PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
#    HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
#    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
#    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
#    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
#    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
###############################################################################
import numpy as np
import scipy.sparse as sp
from qutip.qobj import *
from qutip.qip.gates import *
from qutip.qip.circuit import QubitCircuit
from qutip.qip.models.circuitprocessor import CircuitProcessor

class SpinChain(CircuitProcessor):
    """
    Representation of the physical implementation of a quantum program/algorithm
    on a spin chain qubit system.
    """

    def __init__(self, N, correct_global_phase=True):

        super(SpinChain, self).__init__(N, correct_global_phase)

        self.sx_ops = [tensor([sigmax() if m == n else identity(2)
                               for n in range(N)])
                       for m in range(N)]
        self.sz_ops = [tensor([sigmaz() if m == n else identity(2)
                               for n in range(N)])
                       for m in range(N)]

        self.sxsy_ops = []
        for n in range(N-1):
            x = [identity(2)] * N
            x[n] = x[n+1] = sigmax()
            y = [identity(2)] * N
            y[n] = y[n+1] = sigmay()
            self.sxsy_ops.append(tensor(x) + tensor(y))

        self.sx_coeff = [0.25 * 2 * pi] * N
        self.sz_coeff = [1.0 * 2 * pi] * N
        self.sxsy_coeff = [0.1 * 2 * pi] * (N - 1)

    def get_ops_and_u(self):
        return (self.sx_ops + self.sz_ops + self.sxsy_ops,
                hstack((self.sx_u, self.sz_u, self.sxsy_u)))

    def get_ops_labels(self):
        return ([r"$\sigma_x^%d$" % n for n in range(self.N)] +
                [r"$\sigma_z^%d$" % n for n in range(self.N)] +
                [r"$\sigma_x^%d\sigma_x^{%d} + \sigma_y^%d\sigma_y^{%d}$"
                 % (n, n, n, n) for n in range(self.N-1)])

    def load_circuit(self, qc):

        gates = self.optimize_circuit(qc).gates

        self.global_phase = 0
        self.sx_u = np.zeros((len(gates), len(self.sx_ops)))
        self.sz_u = np.zeros((len(gates), len(self.sz_ops)))
        self.sxsy_u = np.zeros((len(gates), len(self.sxsy_ops)))
        self.T_list = []

        n = 0
        for gate in gates:

            if gate.name == "ISWAP":
                g = self.sxsy_coeff[min(gate.targets)]
                self.sxsy_u[n, min(gate.targets)] = -g
                T = pi / (4 * g)
                self.T_list.append(T)
                n += 1

            elif gate.name == "SQRTISWAP":
                g = self.sxsy_coeff[min(gate.targets)]
                self.sxsy_u[n, min(gate.targets)] = -g
                T = pi / (8 * g)
                self.T_list.append(T)
                n += 1

            elif gate.name == "RZ":
                g = self.sz_coeff[gate.targets[0]]
                self.sz_u[n, gate.targets[0]] = np.sign(gate.arg_value) * g
                T = abs(gate.arg_value) / (2 * g)
                self.T_list.append(T)
                n += 1

            elif gate.name == "RX":
                g = self.sx_coeff[gate.targets[0]]
                self.sx_u[n, gate.targets[0]] = np.sign(gate.arg_value) * g
                T = abs(gate.arg_value) / (2 * g)
                self.T_list.append(T)
                n += 1

            elif gate.name == "GLOBALPHASE":
                self.global_phase += gate.arg_value

            else:
                raise ValueError("Unsupported gate %s" % gate.name)

def adjacent_gates(qc, setup="linear"):
        """
        Method to resolve 2 qubit gates with non-adjacent control/s or target/s
        in terms of gates with adjacent interactions for linear/circular spin
        chain system.

        Parameters
        ----------
        qc: Qobj
            The circular spin chain circuit to be resolved

        setup: Boolean
            Linear of Circular spin chain setup

        Returns
        ----------
        qc_temp: Qobj
            Returns Qobj of resolved gates for the qubit circuit in the desired
            basis.
        """
        qc_temp = QubitCircuit(qc.N, qc.reverse_states)
        swap_gates = ["SWAP", "ISWAP", "SQRTISWAP", "SQRTSWAP", "BERKELEY",
                      "SWAPalpha"]

        for gate in qc.gates:
            if gate.name == "CNOT" or gate.name == "CSIGN":
                start = min([gate.targets[0], gate.controls[0]])
                end = max([gate.targets[0], gate.controls[0]])

                if (setup == "linear" or
                    (setup == "circular" and (end - start) <= N//2)):
                    i = start
                    while i < end:
                        if start+end-i-i == 1 and (end-start+1)%2 == 0:
                            #Apply required gate if control and target are
                            #adjacent to each other, provided |control-target|
                            #is even.
                            if end == gate.controls[0]:
                                qc_temp.gates.append(Gate(gate.name,
                                                          targets=[i],
                                                          controls=[i+1]))
                            else:
                                qc_temp.gates.append(Gate(gate.name,
                                                          targets=[i+1],
                                                          controls=[i]))

                        elif start+end-i-i == 2 and (end-start+1)%2 == 1:
                            #Apply a swap between i and its adjacent gate, then
                            #the required gate if and then another swap if
                            #control and target have one qubit between them,
                            #provided |control-target| is odd.
                            qc_temp.gates.append(Gate("SWAP", targets=[i, i+1]))
                            if end == gate.controls[0]:
                                qc_temp.gates.append(Gate(gate.name,
                                                          targets=[i+1],
                                                          controls=[i+2]))
                            else:
                                qc_temp.gates.append(Gate(gate.name,
                                                          targets=[i+2],
                                                          controls=[i+1]))
                            qc_temp.gates.append(Gate("SWAP", targets=[i, i+1]))
                            i += 1

                        else:
                            #Swap the target/s and/or control with their
                            #adjacent qubit to bring them closer.
                            qc_temp.gates.append(Gate("SWAP", targets=[i, i+1]))
                            qc_temp.gates.append(Gate("SWAP",
                                                      targets=[start+end-i-1,
                                                               start+end-i]))
                        i += 1

                else:
                    temp = QubitCircuit(N-end+start)
                    i = 0
                    while i < (N-end+start):

                        if N+start-end-i-i == 1 and (N-end+start)%2 == 0:
                            if end == gate.controls[0]:
                                temp.gates.append(Gate(gate.name, targets=[i],
                                                       controls=[i+1]))
                            else:
                                temp.gates.append(Gate(gate.name, targets=[i+1],
                                                       controls=[i]))

                        elif N+start-end-i-i == 2 and (N-end+start)%2 == 1:
                            temp.gates.append(Gate("SWAP", targets=[i, i+1]))
                            if end == gate.controls[0]:
                                temp.gates.append(Gate(gate.name, targets=[i+1],
                                                       controls=[i+2]))
                            else:
                                temp.gates.append(Gate(gate.name, targets=[i+2],
                                                       controls=[i+1]))
                            temp.gates.append(Gate("SWAP", targets=[i, i+1]))
                            i += 1

                        else:
                            temp.gates.append(Gate("SWAP", targets=[i, i+1]))
                            temp.gates.append(Gate("SWAP",
                                                   targets=[N+start-end-i-1,
                                                            N+start-end-i]))
                        i += 1

                    j = 0
                    for gate in temp.gates:
                        if(j<N-end+2):
                            if gate.name in ["CNOT", "CSIGN"]:
                                qc_temp.append(Gate(gate.name,
                                                    end+gate.targets[0],
                                                    end+gate.targets[1]))
                            else:
                                qc_temp.append(Gate(gate.name,
                                                    [end+gate.targets[0],
                                                     end+gate.targets[1]]))
                        elif(j==N-end+2):
                            if gate.name in ["CNOT", "CSIGN"]:
                                qc_temp.append(Gate(gate.name,
                                                    end+gate.targets[0],
                                                    (end+gate.targets[1])%N))
                            else:
                            qc_temp.append(Gate(gate.name,
                                                [end+gate.targets[0],
                                                 (end+gate.targets[1])%N]))
                        else:
                            if gate.name in ["CNOT", "CSIGN"]:
                                qc_temp.append(Gate(gate.name,
                                                    (end+gate.targets[0])%N,
                                                    (end+gate.targets[1])%N))
                            else:
                            qc_temp.append(Gate(gate.name,
                                           [(end+gate.targets[0]%N),
                                            (end+gate.targets[1])%N]))
                        j = j + 1

            elif gate.name in swap_gates:
                start = min([gate.targets[0], gate.targets[1]])
                end = max([gate.targets[0], gate.targets[1]])

                if (setup == "linear" or
                    (setup == "circular" and (end - start) <= N//2)):
                    i = start
                    while i < end:
                        if start+end-i-i == 1 and (end-start+1)%2 == 0:
                            qc_temp.gates.append(Gate(gate.name,
                                                      targets=[i, i+1]))
                        elif (start+end-i-i) == 2 and (end-start+1)%2 == 1:
                            qc_temp.gates.append(Gate("SWAP", targets=[i, i+1]))
                            qc_temp.gates.append(Gate(gate.name,
                                                      targets=[i+1, i+2]))
                            qc_temp.gates.append(Gate("SWAP", targets=[i, i+1]))
                        else:
                            qc_temp.gates.append(Gate("SWAP", targets=[i, i+1]))
                            qc_temp.gates.append(Gate("SWAP",
                                                      targets=[start+end-i-1,
                                                               start+end-i]))
                        i += 1

                else:
                    temp = QubitCircuit(N-end+start)
                    i = 0
                    while i < (N-end+start):

                        if N+start-end-i-i == 1 and (N-end+start)%2 == 0:
                            if end == gate.controls[0]:
                                temp.gates.append(Gate(gate.name, targets=[i],
                                                       controls=[i+1]))
                            else:
                                temp.gates.append(Gate(gate.name, targets=[i+1],
                                                       controls=[i]))

                        elif N+start-end-i-i == 2 and (N-end+start)%2 == 1:
                            temp.gates.append(Gate("SWAP", targets=[i, i+1]))
                            if end == gate.controls[0]:
                                temp.gates.append(Gate(gate.name, targets=[i+1],
                                                       controls=[i+2]))
                            else:
                                temp.gates.append(Gate(gate.name, targets=[i+2],
                                                       controls=[i+1]))
                            temp.gates.append(Gate("SWAP", targets=[i, i+1]))
                            i += 1

                        else:
                            temp.gates.append(Gate("SWAP", targets=[i, i+1]))
                            temp.gates.append(Gate("SWAP",
                                                   targets=[N+start-end-i-1,
                                                            N+start-end-i]))
                        i += 1

                    j = 0
                    for gate in temp.gates:
                        if(j<N-end+2):
                            qc_temp.append(Gate(gate.name, [end+gate.targets[0],
                                                end+gate.targets[1]]))
                        elif(j==N-end+2):
                            qc_temp.append(Gate(gate.name,
                                                [end+gate.targets[0],
                                                 (end+gate.targets[1])%N]))
                        else:
                            qc_temp.append(Gate(gate.name,
                                                [(end+gate.targets[0]%N),
                                                 (end+gate.targets[1])%N]))
                        j = j + 1

            else:
                qc_temp.gates.append(gate)

        return qc_temp

class LinearSpinChain(SpinChain):
    """
    Representation of the physical implementation of a quantum program/algorithm
    on a spin chain qubit system arranged in a linear formation. It is a
    sub-class of SpinChain.
    """

    def __init__(self, N, correct_global_phase=True):

        super(LinearSpinChain, self).__init__(N, correct_global_phase)

    def optimize_circuit(self, qc):
        self.qc0 = qc
        qc_temp = SpinChain.adjacent_gates(qc, "linear")
        self.qc1 = qc_temp
        qc = qc_temp.resolve_gates(basis=["ISWAP", "RX", "RZ"])
        self.qc2 = qc
        return qc

class CircularSpinChain(SpinChain):
    """
    Representation of the physical implementation of a quantum program/algorithm
    on a spin chain qubit system arranged in a circular formation. It is a
    sub-class of SpinChain.
    """

    def __init__(self, N, correct_global_phase=True):

        super(CircularSpinChain, self).__init__(N, correct_global_phase)

    def optimize_circuit(self, qc):
        self.qc0 = qc
        qc_temp = SpinChain.adjacent_gates(qc, "circular")
        self.qc1 = qc_temp
        qc = qc_temp.resolve_gates(basis=["ISWAP", "RX", "RZ"])
        self.qc2 = qc
        return qc
<commit_msg>bug fixes<commit_after># This file is part of QuTiP: Quantum Toolbox in Python.
#
#    Copyright (c) 2011 and later, Paul D. Nation and Robert J. Johansson.
#    All rights reserved.
#
#    Redistribution and use in source and binary forms, with or without
#    modification, are permitted provided that the following conditions are
#    met:
#
#    1. Redistributions of source code must retain the above copyright notice,
#       this list of conditions and the following disclaimer.
#
#    2. Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#
#    3. Neither the name of the QuTiP: Quantum Toolbox in Python nor the names
#       of its contributors may be used to endorse or promote products derived
#       from this software without specific prior written permission.
#
#    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
#    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
#    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
#    PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
#    HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
#    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
#    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
#    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
#    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
###############################################################################
import numpy as np
import scipy.sparse as sp
from qutip.qobj import *
from qutip.qip.gates import *
from qutip.qip.circuit import QubitCircuit
from qutip.qip.models.circuitprocessor import CircuitProcessor

class SpinChain(CircuitProcessor):
    """
    Representation of the physical implementation of a quantum program/algorithm
    on a spin chain qubit system.
    """

    def __init__(self, N, correct_global_phase=True):

        super(SpinChain, self).__init__(N, correct_global_phase)

        self.sx_ops = [tensor([sigmax() if m == n else identity(2)
                               for n in range(N)])
                       for m in range(N)]
        self.sz_ops = [tensor([sigmaz() if m == n else identity(2)
                               for n in range(N)])
                       for m in range(N)]

        self.sxsy_ops = []
        for n in range(N-1):
            x = [identity(2)] * N
            x[n] = x[n+1] = sigmax()
            y = [identity(2)] * N
            y[n] = y[n+1] = sigmay()
            self.sxsy_ops.append(tensor(x) + tensor(y))

        self.sx_coeff = [0.25 * 2 * pi] * N
        self.sz_coeff = [1.0 * 2 * pi] * N
        self.sxsy_coeff = [0.1 * 2 * pi] * (N - 1)

    def get_ops_and_u(self):
        return (self.sx_ops + self.sz_ops + self.sxsy_ops,
                hstack((self.sx_u, self.sz_u, self.sxsy_u)))

    def get_ops_labels(self):
        return ([r"$\sigma_x^%d$" % n for n in range(self.N)] +
                [r"$\sigma_z^%d$" % n for n in range(self.N)] +
                [r"$\sigma_x^%d\sigma_x^{%d} + \sigma_y^%d\sigma_y^{%d}$"
                 % (n, n, n, n) for n in range(self.N-1)])

    def load_circuit(self, qc):

        gates = self.optimize_circuit(qc).gates

        self.global_phase = 0
        self.sx_u = np.zeros((len(gates), len(self.sx_ops)))
        self.sz_u = np.zeros((len(gates), len(self.sz_ops)))
        self.sxsy_u = np.zeros((len(gates), len(self.sxsy_ops)))
        self.T_list = []

        n = 0
        for gate in gates:

            if gate.name == "ISWAP":
                g = self.sxsy_coeff[min(gate.targets)]
                self.sxsy_u[n, min(gate.targets)] = -g
                T = pi / (4 * g)
                self.T_list.append(T)
                n += 1

            elif gate.name == "SQRTISWAP":
                g = self.sxsy_coeff[min(gate.targets)]
                self.sxsy_u[n, min(gate.targets)] = -g
                T = pi / (8 * g)
                self.T_list.append(T)
                n += 1

            elif gate.name == "RZ":
                g = self.sz_coeff[gate.targets[0]]
                self.sz_u[n, gate.targets[0]] = np.sign(gate.arg_value) * g
                T = abs(gate.arg_value) / (2 * g)
                self.T_list.append(T)
                n += 1

            elif gate.name == "RX":
                g = self.sx_coeff[gate.targets[0]]
                self.sx_u[n, gate.targets[0]] = np.sign(gate.arg_value) * g
                T = abs(gate.arg_value) / (2 * g)
                self.T_list.append(T)
                n += 1

            elif gate.name == "GLOBALPHASE":
                self.global_phase += gate.arg_value

            else:
                raise ValueError("Unsupported gate %s" % gate.name)

def adjacent_gates(qc, setup="linear"):
        """
        Method to resolve 2 qubit gates with non-adjacent control/s or target/s
        in terms of gates with adjacent interactions for linear/circular spin
        chain system.

        Parameters
        ----------
        qc: Qobj
            The circular spin chain circuit to be resolved

        setup: Boolean
            Linear of Circular spin chain setup

        Returns
        ----------
        qc_temp: Qobj
            Returns Qobj of resolved gates for the qubit circuit in the desired
            basis.
        """
        qc_temp = QubitCircuit(qc.N, qc.reverse_states)
        swap_gates = ["SWAP", "ISWAP", "SQRTISWAP", "SQRTSWAP", "BERKELEY",
                      "SWAPalpha"]

        for gate in qc.gates:
            if gate.name == "CNOT" or gate.name == "CSIGN":
                start = min([gate.targets[0], gate.controls[0]])
                end = max([gate.targets[0], gate.controls[0]])

                if (setup == "linear" or
                    (setup == "circular" and (end - start) <= N//2)):
                    i = start
                    while i < end:
                        if start+end-i-i == 1 and (end-start+1)%2 == 0:
                            #Apply required gate if control and target are
                            #adjacent to each other, provided |control-target|
                            #is even.
                            if end == gate.controls[0]:
                                qc_temp.gates.append(Gate(gate.name,
                                                          targets=[i],
                                                          controls=[i+1]))
                            else:
                                qc_temp.gates.append(Gate(gate.name,
                                                          targets=[i+1],
                                                          controls=[i]))

                        elif start+end-i-i == 2 and (end-start+1)%2 == 1:
                            #Apply a swap between i and its adjacent gate, then
                            #the required gate if and then another swap if
                            #control and target have one qubit between them,
                            #provided |control-target| is odd.
                            qc_temp.gates.append(Gate("SWAP", targets=[i, i+1]))
                            if end == gate.controls[0]:
                                qc_temp.gates.append(Gate(gate.name,
                                                          targets=[i+1],
                                                          controls=[i+2]))
                            else:
                                qc_temp.gates.append(Gate(gate.name,
                                                          targets=[i+2],
                                                          controls=[i+1]))
                            qc_temp.gates.append(Gate("SWAP", targets=[i, i+1]))
                            i += 1

                        else:
                            #Swap the target/s and/or control with their
                            #adjacent qubit to bring them closer.
                            qc_temp.gates.append(Gate("SWAP", targets=[i, i+1]))
                            qc_temp.gates.append(Gate("SWAP",
                                                      targets=[start+end-i-1,
                                                               start+end-i]))
                        i += 1

                else:
                    temp = QubitCircuit(N-end+start)
                    i = 0
                    while i < (N-end+start):

                        if N+start-end-i-i == 1 and (N-end+start)%2 == 0:
                            if end == gate.controls[0]:
                                temp.gates.append(Gate(gate.name, targets=[i],
                                                       controls=[i+1]))
                            else:
                                temp.gates.append(Gate(gate.name, targets=[i+1],
                                                       controls=[i]))

                        elif N+start-end-i-i == 2 and (N-end+start)%2 == 1:
                            temp.gates.append(Gate("SWAP", targets=[i, i+1]))
                            if end == gate.controls[0]:
                                temp.gates.append(Gate(gate.name, targets=[i+1],
                                                       controls=[i+2]))
                            else:
                                temp.gates.append(Gate(gate.name, targets=[i+2],
                                                       controls=[i+1]))
                            temp.gates.append(Gate("SWAP", targets=[i, i+1]))
                            i += 1

                        else:
                            temp.gates.append(Gate("SWAP", targets=[i, i+1]))
                            temp.gates.append(Gate("SWAP",
                                                   targets=[N+start-end-i-1,
                                                            N+start-end-i]))
                        i += 1

                    j = 0
                    for gate in temp.gates:
                        if(j<N-end+2):
                            if gate.name in ["CNOT", "CSIGN"]:
                                qc_temp.append(Gate(gate.name,
                                                    end+gate.targets[0],
                                                    end+gate.targets[1]))
                            else:
                                qc_temp.append(Gate(gate.name,
                                                    [end+gate.targets[0],
                                                     end+gate.targets[1]]))
                        elif(j==N-end+2):
                            if gate.name in ["CNOT", "CSIGN"]:
                                qc_temp.append(Gate(gate.name,
                                                    end+gate.targets[0],
                                                    (end+gate.targets[1])%N))
                            else:
                                qc_temp.append(Gate(gate.name,
                                                    [end+gate.targets[0],
                                                     (end+gate.targets[1])%N]))
                        else:
                            if gate.name in ["CNOT", "CSIGN"]:
                                qc_temp.append(Gate(gate.name,
                                                    (end+gate.targets[0])%N,
                                                    (end+gate.targets[1])%N))
                            else:
                                qc_temp.append(Gate(gate.name,
                                               [(end+gate.targets[0]%N),
                                                (end+gate.targets[1])%N]))
                        j = j + 1

            elif gate.name in swap_gates:
                start = min([gate.targets[0], gate.targets[1]])
                end = max([gate.targets[0], gate.targets[1]])

                if (setup == "linear" or
                    (setup == "circular" and (end - start) <= N//2)):
                    i = start
                    while i < end:
                        if start+end-i-i == 1 and (end-start+1)%2 == 0:
                            qc_temp.gates.append(Gate(gate.name,
                                                      targets=[i, i+1]))
                        elif (start+end-i-i) == 2 and (end-start+1)%2 == 1:
                            qc_temp.gates.append(Gate("SWAP", targets=[i, i+1]))
                            qc_temp.gates.append(Gate(gate.name,
                                                      targets=[i+1, i+2]))
                            qc_temp.gates.append(Gate("SWAP", targets=[i, i+1]))
                        else:
                            qc_temp.gates.append(Gate("SWAP", targets=[i, i+1]))
                            qc_temp.gates.append(Gate("SWAP",
                                                      targets=[start+end-i-1,
                                                               start+end-i]))
                        i += 1

                else:
                    temp = QubitCircuit(N-end+start)
                    i = 0
                    while i < (N-end+start):

                        if N+start-end-i-i == 1 and (N-end+start)%2 == 0:
                            if end == gate.controls[0]:
                                temp.gates.append(Gate(gate.name, targets=[i],
                                                       controls=[i+1]))
                            else:
                                temp.gates.append(Gate(gate.name, targets=[i+1],
                                                       controls=[i]))

                        elif N+start-end-i-i == 2 and (N-end+start)%2 == 1:
                            temp.gates.append(Gate("SWAP", targets=[i, i+1]))
                            if end == gate.controls[0]:
                                temp.gates.append(Gate(gate.name, targets=[i+1],
                                                       controls=[i+2]))
                            else:
                                temp.gates.append(Gate(gate.name, targets=[i+2],
                                                       controls=[i+1]))
                            temp.gates.append(Gate("SWAP", targets=[i, i+1]))
                            i += 1

                        else:
                            temp.gates.append(Gate("SWAP", targets=[i, i+1]))
                            temp.gates.append(Gate("SWAP",
                                                   targets=[N+start-end-i-1,
                                                            N+start-end-i]))
                        i += 1

                    j = 0
                    for gate in temp.gates:
                        if(j<N-end+2):
                            qc_temp.append(Gate(gate.name, [end+gate.targets[0],
                                                end+gate.targets[1]]))
                        elif(j==N-end+2):
                            qc_temp.append(Gate(gate.name,
                                                [end+gate.targets[0],
                                                 (end+gate.targets[1])%N]))
                        else:
                            qc_temp.append(Gate(gate.name,
                                                [(end+gate.targets[0]%N),
                                                 (end+gate.targets[1])%N]))
                        j = j + 1

            else:
                qc_temp.gates.append(gate)

        return qc_temp

class LinearSpinChain(SpinChain):
    """
    Representation of the physical implementation of a quantum program/algorithm
    on a spin chain qubit system arranged in a linear formation. It is a
    sub-class of SpinChain.
    """

    def __init__(self, N, correct_global_phase=True):

        super(LinearSpinChain, self).__init__(N, correct_global_phase)

    def optimize_circuit(self, qc):
        self.qc0 = qc
        qc_temp = SpinChain.adjacent_gates(qc, setup="linear")
        self.qc1 = qc_temp
        qc = qc_temp.resolve_gates(basis=["ISWAP", "RX", "RZ"])
        self.qc2 = qc
        return qc

class CircularSpinChain(SpinChain):
    """
    Representation of the physical implementation of a quantum program/algorithm
    on a spin chain qubit system arranged in a circular formation. It is a
    sub-class of SpinChain.
    """

    def __init__(self, N, correct_global_phase=True):

        super(CircularSpinChain, self).__init__(N, correct_global_phase)

    def optimize_circuit(self, qc):
        self.qc0 = qc
        qc_temp = SpinChain.adjacent_gates(qc, setup="circular")
        self.qc1 = qc_temp
        qc = qc_temp.resolve_gates(basis=["ISWAP", "RX", "RZ"])
        self.qc2 = qc
        return qc
<|endoftext|>


<commit_before>import pygame
import math
import numpy as np
import random
import time
import mss

class Minesweeper(object):
    def __init__(self, ROWS = 10, COLS = 10, SIZEOFSQ = 100, MINES = 13, display = False):
        """ Initialize Minesweeper
            Rows, Cols: int  - Number of rows and cols on the board
            SIZEOFSQ: pixels -  Determines the size of the window, reduce to get smaller window
            Mines: integer - Number of mines generated on the board
            display: bool - chooses weather to display the game with pygame
        """

        self.ROWS = ROWS
        self.COLS = COLS
        self.MINES = MINES
        self.display = display

        self.grid = np.zeros((self.ROWS, self.COLS), dtype=object)
        self.state = np.zeros((self.ROWS, self.COLS), dtype=object)
        self.state_last = np.copy(self.state)

        self.won = 0
        self.lost = 0

        if display: #Load pygame stuff

            #Scale to resolutions
            with mss.mss() as sct:
                img = np.array(sct.grab(sct.monitors[1]))
                self.SIZEOFSQ = int(SIZEOFSQ * img.shape[1] / 3840)
                SIZEOFSQ = self.SIZEOFSQ

            pygame.init()
            pygame.font.init()

            self.myfont = pygame.font.SysFont("monospace-bold", SIZEOFSQ)
            self.screen = pygame.display.set_mode((COLS * SIZEOFSQ, ROWS * SIZEOFSQ))

        self.initGame()

    def initGame(self):
        if self.display:
            self.initPattern()
            pygame.display.set_caption('Minesweeper |            Won: {}   Lost: {}'.format(self.won, self.lost))
        self.grid = self.initBoard(startcol = 5, startrow = 5)
        self.state = np.ones((self.ROWS, self.COLS), dtype=object) * 'U'
        self.state_last = np.copy(self.state)

        self.action(5,5) #Hack alert, to start off with non empty board. Can be removed but then agent has to learn
                         #what to do when the board starts out empty.

    def initBoard(self, startcol, startrow):
        """ Initializes the board """

        COLS = self.COLS
        ROWS = self.ROWS
        grid = np.zeros((self.ROWS, self.COLS), dtype=object)
        mines = self.MINES

        #Randomly place bombs
        while mines > 0:
            (row, col) = (random.randint(0, ROWS-1), random.randint(0, COLS-1))
            #if (col,row) not in findNeighbors(startcol, startrow, grid) and grid[col][row] != 'B' and (col, row) not in (startcol, startrow):
            if (row,col) not in self.findNeighbors(startrow, startcol) and (row,col) != (startrow, startcol) and grid[row][col] != 'B':
                grid[row][col] = 'B'
                mines = mines - 1

        #Get rest of board when bombs have been placed
        for col in range(COLS):
            for row in range(ROWS):
                if grid[row][col] != 'B':
                    totMines = self.sumMines(col, row, grid)
                    if totMines > 0:
                        grid[row][col] = totMines
                    else:
                        grid[row][col] = 'E'

        return grid

    def findNeighbors(self, rowin, colin):
        """ Takes col, row and grid as input and returns as list of neighbors
        """
        COLS = self.grid.shape[1]
        ROWS = self.grid.shape[0]
        neighbors = []
        for col in range(colin-1, colin+2):
            for row in range(rowin-1, rowin+2):
                if (-1 < rowin < ROWS and
                    -1 < colin < COLS and
                    (rowin != row or colin != col) and
                    (0 <= col < COLS) and
                    (0 <= row < ROWS)):
                    neighbors.append((row,col))

        return neighbors

    def sumMines(self, col, row, grid):
        """ Finds amount of mines adjacent to a field.
        """
        mines = 0
        neighbors = self.findNeighbors(row, col)
        for n in neighbors:
            if grid[n[0],n[1]] == 'B':
                mines = mines + 1
        return mines

    def printState(self):
        """Prints the current state"""
        grid = self.state
        COLS = grid.shape[1]
        ROWS = grid.shape[0]
        for row in range(0,ROWS):
            print(' ')
            for col in range(0,COLS):
                print(grid[row][col], end=' ')

    def printBoard(self):
        """Prints the board """
        grid = self.grid
        COLS = grid.shape[1]
        ROWS = grid.shape[0]
        for row in range(0,ROWS):
            print(' ')
            for col in range(0,COLS):
                print(grid[row][col], end=' ')

    def reveal(self, col, row, checked, press = "LM"):
        """Finds out which values to show in the state when a square is pressed
           Checked : np.array((row,col)) to check which squares has already been checked
                     If the field is not a bomb we want to reveal it, if the field is empty
                     we want to find it's neighbors and reveal them too if they are not a bomb.
        """
        if press == "LM":
            if checked[row][col] != 0:
                return
            checked[row][col] = checked[row][col] + 1
            if self.grid[row][col] != 'B':

                if self.display:
                    self.drawSpirit(col, row, self.grid[row][col])

                #Reveal to state space
                self.state[row][col] = self.grid[row][col]

                if self.grid[row][col] == 'E':
                    neighbors = self.findNeighbors(row, col)
                    for n in neighbors:
                        if not checked[n[0],n[1]]:
                            self.reveal(n[1], n[0], checked)

        elif press == "RM":
            #Draw flag, not used for agent
            pass

            if self.display:
                self.drawSpirit(col, row, "flag")

    def action(self, row, col):
        """ External action, taken by human or agent
            row,col: integer - where the agent want to press
         """

        #If press a bomb game over, start new game and return bad reward, -10 in this case
        if self.grid[row][col] == "B":
            self.lost += 1
            self.initGame()
            return({"s" : self.state, "r" : -10})

        #Take action and reveal new state
        self.reveal(col, row , np.zeros_like(self.grid))
        if self.display == True:
            pygame.display.flip()

        #Winning condition
        if np.sum(self.state == "U") == self.MINES:
            self.won += 1
            self.initGame()
            return({"s" : self.state, "r" : 10})

        #Get the reward for the given action
        reward = self.compute_reward()

        #return the state and the reward
        return({"s" : self.state, "r" : reward})

    def compute_reward(self):
        """Computes the reward for a given action"""

        #Reward = 1 if we get less unknowns, 0 otherwise
        if (np.sum(self.state_last == 'U') - np.sum(self.state == 'U')) == self.MINES:
            reward = 1
        else:
            reward = 0

        self.state_last = np.copy(self.state)
        return(reward)

    def drawSpirit(self, col, row, type):
        """
        Draws a spirit at pos col, row of type = [E (empty), B (bomb), 1, 2, 3, 4, 5, 6, F (flag)]
        """
        myfont = self.myfont
        SIZEOFSQ = self.SIZEOFSQ

        if type == 'E':
            if self.checkpattern(col,row):
                c = (242, 244, 247)
            else:
                c = (247, 249, 252)
            pygame.draw.rect(self.screen, c, pygame.Rect(col*SIZEOFSQ, row*SIZEOFSQ, SIZEOFSQ, SIZEOFSQ))

        else:
            self.drawSpirit(col, row, 'E')
            if type == 1:
                text = myfont.render("1", 1, (0, 204, 0))
            elif type == 2:
                text = myfont.render("2", 1, (255, 204, 0))
            elif type == 3:
                text = myfont.render("3", 1, (204, 0, 0))
            elif type == 4:
                text = myfont.render("4", 1, (0, 51, 153))
            elif type == 5:
                text = myfont.render("5", 1, (255, 102, 0))
            elif type == 6:
                text = myfont.render("6", 1, (255, 102, 0))
            elif type == 'flag':
                text = myfont.render("F", 1, (255, 0, 0))

            #Get the text rectangle and center it inside the rectangles
            textRect = text.get_rect()
            textRect.center = (col*self.SIZEOFSQ + int(0.5*self.SIZEOFSQ)),(row*self.SIZEOFSQ + int(0.5*self.SIZEOFSQ))
            self.screen.blit(text, textRect)

    def checkpattern(self, col, row):
        #Function to construct the checked pattern in pygame
        if row % 2:
            if col % 2: #If unequal
                return True
            else: #if equal
                return False
        else:
            if col % 2: #If unequal
                return False
            else: #if equal
                return True

    def initPattern(self):
        #Initialize pattern:

        c1 = (4, 133, 223) #color1
        c2 = (4, 145, 223) #color2
        rects = []
        for row in range(self.ROWS):
            for col in range(self.COLS):
                if self.checkpattern(col, row):
                    c = c1
                else:
                    c = c2

                rects.append(pygame.draw.rect(self.screen, c, pygame.Rect(col*self.SIZEOFSQ, row*self.SIZEOFSQ, self.SIZEOFSQ, self.SIZEOFSQ)))

        pygame.display.flip()

    def get_state(self):
        return self.state

if __name__ == "__main__":

    game = Minesweeper(display=True)
    game.printState()

    i = 0
    #start = time.time()
    while True:

        inp = input("Enter input (ROW,COL)")
        row = int(inp[1])
        col = int(inp[3])
        v = game.action(row, col)
        game.printState()
        print("\nReward = {}".format(v["r"]))

        """#Test how fast it can run:
        i += 1
        print(i)
        act = [np.random.randint(0,10), np.random.randint(0,10)]
        game.action(act[0],act[1])
        if i >= 10000:
            break
        """

    #print("Took: " + str(time.time()-start))

<commit_msg>Test for mac users<commit_after>import pygame
import math
import numpy as np
import random
import time
import mss
import _thread

class Minesweeper(object):
    def __init__(self, ROWS = 10, COLS = 10, SIZEOFSQ = 100, MINES = 13, display = False):
        """ Initialize Minesweeper
            Rows, Cols: int  - Number of rows and cols on the board
            SIZEOFSQ: pixels -  Determines the size of the window, reduce to get smaller window
            Mines: integer - Number of mines generated on the board
            display: bool - chooses weather to display the game with pygame
        """

        self.ROWS = ROWS
        self.COLS = COLS
        self.MINES = MINES
        self.display = display

        self.grid = np.zeros((self.ROWS, self.COLS), dtype=object)
        self.state = np.zeros((self.ROWS, self.COLS), dtype=object)
        self.state_last = np.copy(self.state)

        self.won = 0
        self.lost = 0

        if display: #Load pygame stuff

            #Scale to resolutions
            with mss.mss() as sct:
                img = np.array(sct.grab(sct.monitors[1]))
                self.SIZEOFSQ = int(SIZEOFSQ * img.shape[1] / 3840)
                SIZEOFSQ = self.SIZEOFSQ

            pygame.init()
            pygame.font.init()

            self.myfont = pygame.font.SysFont("monospace-bold", SIZEOFSQ)
            self.screen = pygame.display.set_mode((COLS * SIZEOFSQ, ROWS * SIZEOFSQ))
            _thread.start_new_thread(self.pyloop())

        self.initGame()

    def pyloop(self):
        while True:
            for event in pygame.event.get():
            # check for closing window
                if event.type == pygame.QUIT:
                    running = False

                self.initGame()

                #pygame.display.flip()

    def initGame(self):
        if self.display:
            self.initPattern()
            pygame.display.set_caption('Minesweeper |            Won: {}   Lost: {}'.format(self.won, self.lost))
        self.grid = self.initBoard(startcol = 5, startrow = 5)
        self.state = np.ones((self.ROWS, self.COLS), dtype=object) * 'U'
        self.state_last = np.copy(self.state)

        self.action(5,5) #Hack alert, to start off with non empty board. Can be removed but then agent has to learn
                         #what to do when the board starts out empty.

    def initBoard(self, startcol, startrow):
        """ Initializes the board """

        COLS = self.COLS
        ROWS = self.ROWS
        grid = np.zeros((self.ROWS, self.COLS), dtype=object)
        mines = self.MINES

        #Randomly place bombs
        while mines > 0:
            (row, col) = (random.randint(0, ROWS-1), random.randint(0, COLS-1))
            #if (col,row) not in findNeighbors(startcol, startrow, grid) and grid[col][row] != 'B' and (col, row) not in (startcol, startrow):
            if (row,col) not in self.findNeighbors(startrow, startcol) and (row,col) != (startrow, startcol) and grid[row][col] != 'B':
                grid[row][col] = 'B'
                mines = mines - 1

        #Get rest of board when bombs have been placed
        for col in range(COLS):
            for row in range(ROWS):
                if grid[row][col] != 'B':
                    totMines = self.sumMines(col, row, grid)
                    if totMines > 0:
                        grid[row][col] = totMines
                    else:
                        grid[row][col] = 'E'

        return grid

    def findNeighbors(self, rowin, colin):
        """ Takes col, row and grid as input and returns as list of neighbors
        """
        COLS = self.grid.shape[1]
        ROWS = self.grid.shape[0]
        neighbors = []
        for col in range(colin-1, colin+2):
            for row in range(rowin-1, rowin+2):
                if (-1 < rowin < ROWS and
                    -1 < colin < COLS and
                    (rowin != row or colin != col) and
                    (0 <= col < COLS) and
                    (0 <= row < ROWS)):
                    neighbors.append((row,col))

        return neighbors

    def sumMines(self, col, row, grid):
        """ Finds amount of mines adjacent to a field.
        """
        mines = 0
        neighbors = self.findNeighbors(row, col)
        for n in neighbors:
            if grid[n[0],n[1]] == 'B':
                mines = mines + 1
        return mines

    def printState(self):
        """Prints the current state"""
        grid = self.state
        COLS = grid.shape[1]
        ROWS = grid.shape[0]
        for row in range(0,ROWS):
            print(' ')
            for col in range(0,COLS):
                print(grid[row][col], end=' ')

    def printBoard(self):
        """Prints the board """
        grid = self.grid
        COLS = grid.shape[1]
        ROWS = grid.shape[0]
        for row in range(0,ROWS):
            print(' ')
            for col in range(0,COLS):
                print(grid[row][col], end=' ')

    def reveal(self, col, row, checked, press = "LM"):
        """Finds out which values to show in the state when a square is pressed
           Checked : np.array((row,col)) to check which squares has already been checked
                     If the field is not a bomb we want to reveal it, if the field is empty
                     we want to find it's neighbors and reveal them too if they are not a bomb.
        """
        if press == "LM":
            if checked[row][col] != 0:
                return
            checked[row][col] = checked[row][col] + 1
            if self.grid[row][col] != 'B':

                if self.display:
                    self.drawSpirit(col, row, self.grid[row][col])

                #Reveal to state space
                self.state[row][col] = self.grid[row][col]

                if self.grid[row][col] == 'E':
                    neighbors = self.findNeighbors(row, col)
                    for n in neighbors:
                        if not checked[n[0],n[1]]:
                            self.reveal(n[1], n[0], checked)

        elif press == "RM":
            #Draw flag, not used for agent
            pass

            if self.display:
                self.drawSpirit(col, row, "flag")

    def action(self, row, col):
        """ External action, taken by human or agent
            row,col: integer - where the agent want to press
         """

        #If press a bomb game over, start new game and return bad reward, -10 in this case
        if self.grid[row][col] == "B":
            self.lost += 1
            self.initGame()
            return({"s" : self.state, "r" : -10})

        #Take action and reveal new state
        self.reveal(col, row , np.zeros_like(self.grid))
        if self.display == True:
            pygame.display.flip()

        #Winning condition
        if np.sum(self.state == "U") == self.MINES:
            self.won += 1
            self.initGame()
            return({"s" : self.state, "r" : 10})

        #Get the reward for the given action
        reward = self.compute_reward()

        #return the state and the reward
        return({"s" : self.state, "r" : reward})

    def compute_reward(self):
        """Computes the reward for a given action"""

        #Reward = 1 if we get less unknowns, 0 otherwise
        if (np.sum(self.state_last == 'U') - np.sum(self.state == 'U')) > 0:
            reward = 1
        else:
            reward = 0

        self.state_last = np.copy(self.state)
        return(reward)

    def drawSpirit(self, col, row, type):
        """
        Draws a spirit at pos col, row of type = [E (empty), B (bomb), 1, 2, 3, 4, 5, 6, F (flag)]
        """
        myfont = self.myfont
        SIZEOFSQ = self.SIZEOFSQ

        if type == 'E':
            if self.checkpattern(col,row):
                c = (242, 244, 247)
            else:
                c = (247, 249, 252)
            pygame.draw.rect(self.screen, c, pygame.Rect(col*SIZEOFSQ, row*SIZEOFSQ, SIZEOFSQ, SIZEOFSQ))

        else:
            self.drawSpirit(col, row, 'E')
            if type == 1:
                text = myfont.render("1", 1, (0, 204, 0))
            elif type == 2:
                text = myfont.render("2", 1, (255, 204, 0))
            elif type == 3:
                text = myfont.render("3", 1, (204, 0, 0))
            elif type == 4:
                text = myfont.render("4", 1, (0, 51, 153))
            elif type == 5:
                text = myfont.render("5", 1, (255, 102, 0))
            elif type == 6:
                text = myfont.render("6", 1, (255, 102, 0))
            elif type == 'flag':
                text = myfont.render("F", 1, (255, 0, 0))

            #Get the text rectangle and center it inside the rectangles
            textRect = text.get_rect()
            textRect.center = (col*self.SIZEOFSQ + int(0.5*self.SIZEOFSQ)),(row*self.SIZEOFSQ + int(0.5*self.SIZEOFSQ))
            self.screen.blit(text, textRect)

    def checkpattern(self, col, row):
        #Function to construct the checked pattern in pygame
        if row % 2:
            if col % 2: #If unequal
                return True
            else: #if equal
                return False
        else:
            if col % 2: #If unequal
                return False
            else: #if equal
                return True

    def initPattern(self):
        #Initialize pattern:

        c1 = (4, 133, 223) #color1
        c2 = (4, 145, 223) #color2
        rects = []
        for row in range(self.ROWS):
            for col in range(self.COLS):
                if self.checkpattern(col, row):
                    c = c1
                else:
                    c = c2

                rects.append(pygame.draw.rect(self.screen, c, pygame.Rect(col*self.SIZEOFSQ, row*self.SIZEOFSQ, self.SIZEOFSQ, self.SIZEOFSQ)))

        pygame.display.flip()

    def get_state(self):
        return self.state

if __name__ == "__main__":

    game = Minesweeper(display=True)
    game.printState()

    i = 0
    #start = time.time()
    while True:

        inp = input("Enter input (ROW,COL)")
        row = int(inp[1])
        col = int(inp[3])
        v = game.action(row, col)
        game.printState()
        print("\nReward = {}".format(v["r"]))

        """#Test how fast it can run:
        i += 1
        print(i)
        act = [np.random.randint(0,10), np.random.randint(0,10)]
        game.action(act[0],act[1])
        if i >= 10000:
            break
        """

    #print("Took: " + str(time.time()-start))

<|endoftext|>


<commit_before>from cumulus.settings import CUMULUS
from cumulus.storage import CloudFilesStorage
from random import choice

__author__ = 'rudy'

class MultiContainerCloudFilesStorage(CloudFilesStorage):
    """
    Custom storage for Rackspace Cloud Files.
    """
    active_containers = CUMULUS['ACTIVE_CONTAINERS']
    all_containers = CUMULUS['ALL_CONTAINERS']

    def _open(self, name, mode='rb'):
        name = self.set_current_container(name)
        return super(MultiContainerCloudFilesStorage, self)._open(name, mode)

    def _save(self, name, content):
        name = super(MultiContainerCloudFilesStorage, self)._save(name, content)
        return "%s/%s" % (self.container.name, name)

    def delete(self, name):
        name = self.set_current_container(name)
        return super(MultiContainerCloudFilesStorage, self).delete(name)

    def exists(self, name):
        self.set_random_container()
        return super(MultiContainerCloudFilesStorage, self).exists(name)

    def size(self, name):
        name = self.set_current_container(name)
        return super(MultiContainerCloudFilesStorage, self).size(name)

    def url(self, name):
        name = self.set_current_container(name)
        return super(MultiContainerCloudFilesStorage, self).url(name)

    def modified_time(self, name):
        name = self.set_current_container(name)
        return super(MultiContainerCloudFilesStorage, self).modified_time(name)

    def set_current_container(self, name):
        """
        Set the current container based on the first folder portion of 'name'
        """
        container_name, separator, new_name = name.partition("/")
        if container_name in self.all_containers:
            self.container = self.connection.get_container(container_name)
            if hasattr(self, '_container_public_uri'):
                delattr(self, '_container_public_uri')
            return new_name
        else:
            return name

    def set_random_container(self):
        """
        Set the container to a random container for load balancing
        """
        self.container = self.connection.get_container(choice(self.active_containers))
        if hasattr(self, '_container_public_uri'):
            delattr(self, '_container_public_uri')<commit_msg>use default container if no container specified<commit_after>from cumulus.settings import CUMULUS
from cumulus.storage import CloudFilesStorage
from random import choice

__author__ = 'rudy'

class MultiContainerCloudFilesStorage(CloudFilesStorage):
    """
    Custom storage for Rackspace Cloud Files.
    """
    active_containers = CUMULUS['ACTIVE_CONTAINERS']
    all_containers = CUMULUS['ALL_CONTAINERS']

    def _open(self, name, mode='rb'):
        name = self.set_current_container(name)
        return super(MultiContainerCloudFilesStorage, self)._open(name, mode)

    def _save(self, name, content):
        name = super(MultiContainerCloudFilesStorage, self)._save(name, content)
        return "%s/%s" % (self.container.name, name)

    def delete(self, name):
        name = self.set_current_container(name)
        return super(MultiContainerCloudFilesStorage, self).delete(name)

    def exists(self, name):
        self.set_random_container()
        return super(MultiContainerCloudFilesStorage, self).exists(name)

    def size(self, name):
        name = self.set_current_container(name)
        return super(MultiContainerCloudFilesStorage, self).size(name)

    def url(self, name):
        name = self.set_current_container(name)
        return super(MultiContainerCloudFilesStorage, self).url(name)

    def modified_time(self, name):
        name = self.set_current_container(name)
        return super(MultiContainerCloudFilesStorage, self).modified_time(name)

    def set_current_container(self, name):
        """
        Set the current container based on the first folder portion of 'name'
        """
        container_name, separator, new_name = name.partition("/")
        if container_name in self.all_containers:
            self.container = self.connection.get_container(container_name)
            if hasattr(self, '_container_public_uri'):
                delattr(self, '_container_public_uri')
            return new_name
        else:  # Else we need to use the default container
            self.container = self.connection.get_container(self.container_name)
            if hasattr(self, '_container_public_uri'):
                delattr(self, '_container_public_uri')
            return name

    def set_random_container(self):
        """
        Set the container to a random container for load balancing
        """
        self.container = self.connection.get_container(choice(self.active_containers))
        if hasattr(self, '_container_public_uri'):
            delattr(self, '_container_public_uri')<|endoftext|>


<commit_before><commit_msg>Fixed some import errors, change to new BIDS names<commit_after><|endoftext|>


<commit_before>#! /usr/bin/env python
# -*- encoding: utf-8 -*-
#
# Copyright (c) <2014> <leon.max.liew@gmail.com>
#

"""recursive neural tensor network

Implemention of algorithm descripted "Stanford Recursive Deep Models for
sentiment compositionality over a Sentiment Treebank". Socker(2013).
"""
__author__ = "xiaoliang liu"

import sys
sys.path.append("../../common")
import math
import random
import numpy as np

class RNTN(object):
    """rnn model

    """

    def __init__(self, op=None, model=None,
                 feature_file=None, dev_file=None,
                 train_file=None, model_file=None):
        self.op = op # training options
        self.model = model # model parameters
        self.feature_file = feature_file
        self.dev_file = dev_file
        self.train_file = train_file
        self.model_file = model_file

        self.t = Timing()

    def develop(self):
        """adjust parameters

        """
        pass

    def train(self):
        """train wordvec and classified model

        """
        training_trees =
        sum_grad_square = [] # init sum_grad_square
        batches_num = training_trees.size() / float(self.batch_size) + 1;

        # adagrad
        for epoch in range(self.epochs):
            print "starting epoch %d" % epoch
            if (epoch > 0 and self.adagrad_reset_frequency > 0 and
                (epoch % self.adagrad_reset_frequency) == 0):
                sum_grad_square = [] # reset sum_grad_square
            shuffle(training_trees)
            for batch in range(batches_num):
                print "Epoch %d, batch %d" % (epoch, batch)
                tree_start = self.batch_size * batch
                tree_end = self.batch_size * (batch + 1)
                if tree_end + self.batch_size > train_trees.size():
                    tree_end = train_trees.size()
                self.one_batch_traning(self.rnnloss, training_trees[tree_start, tree_end], sum_grad_square)

    def one_batch_training(self, rnnloss, rnntree, sum_grad_square):
        # adagrad
        for t in rnntree:

    def wordvec(self):
        """get distributed represent for sentence

        """
        pass

    def classify(self, cflie):
        """predict sentiment label

        """
        pass

    def softmax(self, w, nodevec):
        """softmax classifer

        Args:
            w: cate weight with size C * (N +1), including bias.
            nodevec: sample vector with size N + 1, including bias.

        Returns:
            A matrix with one column
        """
        resm = np.dot(w, nodevec)
        return resm/(resm.sum()+1)

    def get_cate(self, resm):
        """get cate

        Args:
            returned value from softmax

        Returns:
            index with max value in resm
        """
        argmax = 0
        for i in range(1, resm.size):
            if resm[i] > resm[argmax]:
                argmax = i
        return argmax

    def save_model(self):
        pass

    def load_model(self):
        pass

class RntnOptions(object):
    """training options

    """
    def __init__(self, batch_size=27, epochs=400, debug_output_epochs=8,
                 max_train_time_seconds=60*60*24, learning_rate=0.1,
                 adagrad_reset_frequency=1, regW=0.001, regV=0.001,
                 regWs=0.0001, regL=0.0001):
        self.batch_size = batch_size # batch size in each training
        self.epochs = epochs # training epochs
        self.debug_output_epochs = debug_output_epochs
        self.max_train_time_seconds = max_train_time_seconds #
        self.learning_rate = learning_rate
        self.adagrad_reset_frequency = adagrad_reset_frequency
        self.regW = regW # Regularization cost for the transform matrix
        self.regV = regV # Regularization cost for the transform tensor
        self.regWs = regWs # Regularization cost for the classification matrices
        self.regL = regL # Regularization cost for the word vectors

class RntnModel(object):
    """model parameters

    """
    def __init__(self, num_cate=5, num_hid=25):
        self.num_cate = num_cate # number of calsses
        self.num_hid = num_hid # Dimension of hidden layers, size of word vectors
        self.W = np.zeros(shape=(num_hid, 2*num_hid + 1), dtype=float) # transform
        self.V = np.zeros(shape=(2*num_hid, 2*num_hid, num_hid), dtype=float) # tensor
        self.Ws = np.zeros(shape=(num_cate, num_hid + 1), dtype=float) # cate weight
        self.L = {} # num_word * num_hid

    def randomW(self):
        # bias column values are initialized zero
        r = 1.0 / (math.sqrt(self.num_hid)*2.0)
        row, col = self.W.shape
        for i in range(row):
            for j in range(col-1):
                self.W[i, j] = random.randrange(-r, r)

    def randomV(self):
        r = 1.0 / (self.num_hid*4.0)
        row, col, s = self.V.shape
        for i in range(row):
            for j in range(col):
                for t in range(s):
                    self.V[i,j,t] = random.randrange(-r, r)

    def randomWs(self):
        # bias column values are initialized zero
        r = 1.0 / (math.sqrt(self.num_hid))
        row, col = self.W.shape
        for i in range(row):
            for j in range(col-1):
                self.Ws[i, j] = random.randrange(-r, r)

    def randomL(self):
        for w in self.L:
            for i in range(self.num_hid):
                self.L[w][i,0] = random.gauss(0,1)

class RntnLossAndGradient(object):
    """loss and gradient from rnn tree

    """
    def __init__(self, m, t):
        self.model = m
        self.sample = t

class SomeFunc(object):
    """common functions

    """
    def __init__(self):
        pass

    def tanh(self, x):
        """tanh

        Args:
            x: a matrix with float type

        Returns:
            tanh(x): also a matrix
        """
        row, col = x.shape
        out = np.ndarray(shape=(row, col),dtype=float)
        for i in range(row):
            for j in range(col):
                out[i, j] = math.tanh(x[i, j])
        return out

def rntn_train(dev_file, train_file, model_file):
    pass

def rntn_predict(test_file, model_file):
    pass

if __name__ == '__main__':
    pass
<commit_msg>rntn code<commit_after>#! /usr/bin/env python
# -*- encoding: utf-8 -*-
#
# Copyright (c) <2014> <leon.max.liew@gmail.com>
#

"""recursive neural tensor network

Implemention of algorithm descripted "Stanford Recursive Deep Models for
sentiment compositionality over a Sentiment Treebank". Socker(2013).
"""
__author__ = "xiaoliang liu"

import sys
sys.path.append("../../common")
import math
import random
import numpy as np
import data_prepare as dpp

class RNTN(object):
    """rnn model

    """

    def __init__(self, op=None, model=None,
                 feature_file=None, dev_file=None,
                 train_file=None, model_file=None):
        self.op = op # training options
        self.model = model # model parameters
        self.feature_file = feature_file
        self.dev_file = dev_file
        self.train_file = train_file
        self.model_file = model_file
        self.t = Timing() # Timing class
        self.trd = None # train or test dpp handle
        self.drd = None # dev dpp handle

    def load_data(self):
        """Load data

        """
        # read trees
        if self.train_file:
            self.trd = dpp.RNNDPP(self.feature_file, self.train_file)
            self.trd.load_feature()
            self.trd.load_sample()
        if self.dev_file:
            self.drd = dpp.RNNDPP(self.feature_file, self.dev_file)
            self.drd.load_feature()
            self.drd.load_sample()

    def train(self):
        """train wordvec and classified model

        """
        self.t.start()
        sum_grad_square = [0.0] * self.model.total_param_size() # init sum_grad_square
        if self.op.batch_size == 0:
            self.op.batch_size = 1
        batches_num = int(len(self.trd.slist) / float(self.op.batch_size));
        # adagrad
        for epoch in range(self.op.epochs):
            print "starting epoch %d" % epoch
            if (epoch > 0 and self.op.adagrad_reset_frequency > 0 and
                (epoch % self.op.adagrad_reset_frequency) == 0):
                sum_grad_square = [0.0] * self.model.total_param_size() # reset sum_grad_square
            shuffle_tree_list = self.trd.slist
            random.shuffle(shuffle_tree_list)
            for batch in range(batches_num):
                print "Epoch %d, batch %d" % (epoch, batch)
                tree_start = self.op.batch_size * batch
                tree_end = self.op.batch_size * (batch + 1)
                if tree_end + self.op.batch_size > len(shuffle_tree_list):
                    tree_end = len(shuffle_tree_list)
                self.one_batch_traning(self.model, shuffle_tree_list[tree_start, tree_end], sum_grad_square)
                # computer timing
                elapsed_time = self.t.report()
                print "epoch is %d, batch is %d, elapsed time %d" % (epoch, batch, elapsed_time)
                if(self.op.max_train_time_seconds > 0 and elapsed_time > self.op.max_train_time_seconds*1000):
                    break
                if (batch==0 and epoch > 0 and epoch % self.op.debug_output_epochs == 0):
                    score = 0.0
                    pass
            elapsed_time = self.t.report()
            if(self.op.max_train_time_seconds > 0 and elapsed_time > self.op.max_train_time_seconds*1000):
                print "Max training time exceeded, exiting"
                break

    def one_batch_training(self, t, sum_grad_square):
        # adagrad
        rlg = RntnLossAndGradient(self.model,t)
        theta = self.model.param2vector()
        eps = 1e-3
        cur_loss = 0
        gradf = rlg.derivative(theta)
        cur_loss = rlg.value(theta)
        print "cur batch loss %f" % cur_loss
        for f in range(len(gradf)):
            sum_grad_square[f] = sum_grad_square[f] + gradf[f]*gradf[f]
            theta[f] = theta[f] - (self.op.learning_rate * gradf[f]/(math.sqrt(sum_grad_square[f])+eps))
        self.model.vector2param(theta)

    def wordvec(self):
        """get distributed represent for sentence

        """
        pass

    def classify(self, cflie):
        """predict sentiment label

        """
        pass

    def softmax(self, w, nodevec):
        """softmax classifer

        Args:
            w: cate weight with size C * (N +1), including bias.
            nodevec: sample vector with size N + 1, including bias.

        Returns:
            A matrix with one column
        """
        resm = np.dot(w, nodevec)
        return resm/(resm.sum()+1)

    def get_cate(self, resm):
        """get cate

        Args:
            returned value from softmax

        Returns:
            index with max value in resm
        """
        argmax = 0
        for i in range(1, resm.size):
            if resm[i] > resm[argmax]:
                argmax = i
        return argmax

    def save_model(self):
        pass

    def load_model(self):
        pass

class RntnOptions(object):
    """training options

    """
    def __init__(self, batch_size=27, epochs=400, debug_output_epochs=8,
                 max_train_time_seconds=60*60*24, learning_rate=0.1,
                 adagrad_reset_frequency=1, regW=0.001, regV=0.001,
                 regWs=0.0001, regL=0.0001, isEval=True):
        self.batch_size = batch_size # batch size in each training
        self.epochs = epochs # training epochs
        self.debug_output_epochs = debug_output_epochs
        self.max_train_time_seconds = max_train_time_seconds #
        self.learning_rate = learning_rate
        self.adagrad_reset_frequency = adagrad_reset_frequency
        self.regW = regW # Regularization cost for the transform matrix
        self.regV = regV # Regularization cost for the transform tensor
        self.regWs = regWs # Regularization cost for the classification matrices
        self.regL = regL # Regularization cost for the word vectors

class RntnModel(object):
    """model parameters

    """
    def __init__(self, num_cate=5, num_hid=25, feature_file=None):
        self.num_cate = num_cate # number of calsses
        self.num_hid = num_hid # Dimension of hidden layers, size of word vectors
        self.W = np.zeros(shape=(num_hid, 2*num_hid + 1), dtype=float) # transform
        self.V = np.zeros(shape=(2*num_hid, 2*num_hid, num_hid), dtype=float) # tensor
        self.Ws = np.zeros(shape=(num_cate, num_hid + 1), dtype=float) # cate weight
        self.L = {} # num_word * num_hid
        rd = dpp.RNNDPP(feature_file, '')
        rd.load_feature()
        # init L in model
        for fid in rd.id2feature:
            self.model.L[fid] = np.zeros(shape=(num_hid, 1), dtype=float)

    def randomW(self):
        # bias column values are initialized zero
        r = 1.0 / (math.sqrt(self.num_hid)*2.0)
        row, col = self.W.shape
        for i in range(row):
            for j in range(col-1):
                self.W[i, j] = random.randrange(-r, r)

    def randomV(self):
        r = 1.0 / (self.num_hid*4.0)
        row, col, s = self.V.shape
        for i in range(row):
            for j in range(col):
                for t in range(s):
                    self.V[i,j,t] = random.randrange(-r, r)

    def randomWs(self):
        # bias column values are initialized zero
        r = 1.0 / (math.sqrt(self.num_hid))
        row, col = self.W.shape
        for i in range(row):
            for j in range(col-1):
                self.Ws[i, j] = random.randrange(-r, r)

    def randomL(self):
        for w in self.L:
            for i in range(self.num_hid):
                self.L[w][i,0] = random.gauss(0,1)

    def total_param_size(self):
        return self.W.size + self.V.size + self.Ws.size + len(self.L) * self.num_hid

class RntnLossAndGradient(object):
    """loss and gradient from rnn tree

    """
    def __init__(self, m, t):
        self.model = m
        self.sample = t

class SomeFunc(object):
    """common functions

    """
    def __init__(self):
        pass

    def tanh(self, x):
        """tanh

        Args:
            x: a matrix with float type

        Returns:
            tanh(x): also a matrix
        """
        row, col = x.shape
        out = np.ndarray(shape=(row, col),dtype=float)
        for i in range(row):
            for j in range(col):
                out[i, j] = math.tanh(x[i, j])
        return out

def rntn_train(feature_file, dev_file, train_file, model_file):
    pass

def rntn_predict(test_file, model_file):
    pass

if __name__ == '__main__':
    pass
<|endoftext|>