Exemple #1
0
    def post(self, request, format=None):
        if request.user.is_authenticated:
            try:
                # Start connection with VASSAR
                user_info = get_or_create_user_information(
                    request.session, request.user, 'EOSS')
                port = user_info.eosscontext.vassar_port
                client = VASSARClient(port)
                client.startConnection()

                inputType = request.data['inputType']

                status = None
                if inputType == 'binary':
                    status = client.client.isGABinaryInputRunning()

                elif inputType == 'discrete':
                    status = client.client.isGADiscreteInputRunning()
                else:
                    raise ValueError(
                        'Unrecognized input type: {0}'.format(inputType))

                # End the connection before return statement
                client.endConnection()

                return Response({'ga_status': status})

            except Exception:
                logger.exception('Exception in starting the GA!')
                client.endConnection()
                return Response('')

        else:
            return Response('This is only available to registered users!')
Exemple #2
0
class RunLocalSearch(APIView):
    def post(self, request, format=None):
        try:
            # Start connection with VASSAR
            user_info = get_or_create_user_information(request.session,
                                                       request.user, 'EOSS')
            port = user_info.eosscontext.vassar_port
            self.VASSARClient = VASSARClient(port)
            self.VASSARClient.startConnection()

            inputs = request.data['inputs']
            inputs = json.loads(inputs)

            architectures = self.VASSARClient.runLocalSearch(inputs)

            for arch in architectures:
                arch['id'] = user_info.eosscontext.last_arch_id
                add_design(arch, user_info.eosscontext, False)

            user_info.save()

            # End the connection before return statement
            self.VASSARClient.endConnection()
            return Response(architectures)

        except Exception:
            logger.exception('Exception in evaluating an architecture')
            self.VASSARClient.endConnection()
            return Response('')
Exemple #3
0
class CriticizeArchitecture(APIView):
    
    
    def post(self, request, format=None):
        try:
            user_info = get_or_create_user_information(request.session, request.user, 'EOSS')
            port = user_info.eosscontext.vassar_port
            
            inputs = request.POST['inputs']   
                        
            inputs = json.loads(inputs)            
            
            critique = self.get_history_critique(inputs)
            
            critique += self.get_expert_critique(inputs, port)
            
            critiques = json.dumps(critique)
                                
            return Response(critiques)
        
        except Exception:
            
            logger.exception('Exception in generating a critique of an architecture')
            return Response('')
                

    def get_history_critique(self, inputs):

        try:
            pass
#            historian_critic = Critic()
#
#            critique = historian_critic.criticizeArchitecture(inputs)
#            
#            return critique

        except Exception:
            logger.exception('Exc in generating a critique using historical database')
            
            raise


    def get_expert_critique(self, inputs, port):

        try:
            self.VASSARClient = VASSARClient(port)

            # Start connection with VASSAR
            self.VASSARClient.startConnection()

            critique = self.VASSARClient.critiqueArchitecture(inputs, False)

            # End the connection before return statement
            self.VASSARClient.endConnection()

        except Exception:
            logger.exception('Exc in generating a critique using expert knowledge base')
            self.VASSARClient.endConnection()
            
            raise
Exemple #4
0
def get_panel_scores(design_id, designs, panel, context: UserInformation):
    port = context.eosscontext.vassar_port
    client = VASSARClient(port)

    try:
        # Start connection with VASSAR
        client.startConnection()
        num_design_id = int(design_id)
        stakeholders_to_excel = {
            "atmospheric": "ATM",
            "oceanic": "OCE",
            "terrestrial": "TER",
            "weather": "WEA",
            "climate": "CLI",
            "land and ecosystems": "ECO",
            "water": "WAT",
            "human health": "HEA"
        }

        panel_code = stakeholders_to_excel[panel.lower()]
        panel_scores = client.client.getPanelScoreExplanation(
            context.eosscontext.problem,
            json.loads(designs[num_design_id].inputs), panel_code)

        # End the connection before return statement
        client.endConnection()
        return panel_scores

    except Exception:
        logger.exception('Exception in loading panel score information')
        client.endConnection()
        return None
Exemple #5
0
def get_instruments_for_stakeholder(stakeholder, context: UserInformation):
    port = context.eosscontext.vassar_port
    client = VASSARClient(port)

    try:
        # Start connection with VASSAR
        client.startConnection()
        stakeholders_to_excel = {
            "atmospheric": "ATM",
            "oceanic": "OCE",
            "terrestrial": "TER",
            "weather": "WEA",
            "climate": "CLI",
            "land and ecosystems": "ECO",
            "water": "WAT",
            "human health": "HEA"
        }
        panel_code = stakeholders_to_excel[stakeholder.lower()]
        stakeholder_instruments = client.client.getInstrumentsForPanel(
            context.eosscontext.problem, panel_code)

        # End the connection before return statement
        client.endConnection()
        return stakeholder_instruments

    except Exception:
        logger.exception('Exception in loading related instruments to a panel')
        client.endConnection()
        return None
Exemple #6
0
class GetOrbitList(APIView):
    def post(self, request, format=None):
        try:
            # Start connection with VASSAR
            user_info = get_or_create_user_information(request.session,
                                                       request.user, 'EOSS')
            port = user_info.eosscontext.vassar_port
            self.VASSARClient = VASSARClient(port)
            self.VASSARClient.startConnection()
            list = self.VASSARClient.getOrbitList(request.data['problem_name'])

            # End the connection before return statement
            self.VASSARClient.endConnection()
            return Response(list)

        except Exception:
            logger.exception('Exception in getting the orbit list')
            self.VASSARClient.endConnection()
            return Response('')
Exemple #7
0
def get_instruments_for_objective(objective, context: UserInformation):
    port = context.eosscontext.vassar_port
    client = VASSARClient(port)

    try:
        # Start connection with VASSAR
        client.startConnection()
        instruments = client.client.getInstrumentsForObjective(
            context.eosscontext.problem, objective)

        # End the connection before return statement
        client.endConnection()
        return instruments

    except Exception:
        logger.exception(
            'Exception in loading related instruments to an objective')
        client.endConnection()
        return None
Exemple #8
0
def get_architecture_scores(design_id, designs, context: UserInformation):
    port = context.eosscontext.vassar_port
    client = VASSARClient(port)

    try:
        # Start connection with VASSAR
        client.startConnection()
        num_design_id = int(design_id)
        scores = client.client.getArchitectureScoreExplanation(
            context.eosscontext.problem,
            json.loads(designs[num_design_id].inputs))

        # End the connection before return statement
        client.endConnection()
        return scores

    except Exception:
        logger.exception('Exception in loading architecture score information')
        client.endConnection()
        return None
Exemple #9
0
class EvaluateArchitecture(APIView):
    def post(self, request, format=None):
        try:
            user_info = get_or_create_user_information(request.session,
                                                       request.user, 'EOSS')
            port = user_info.eosscontext.vassar_port
            self.VASSARClient = VASSARClient(port)
            # Start connection with VASSAR
            self.VASSARClient.startConnection()

            inputs = request.data['inputs']
            inputs = json.loads(inputs)

            architecture = self.VASSARClient.evaluateArchitecture(
                user_info.eosscontext.problem, inputs)

            is_same = True
            for old_arch in user_info.eosscontext.design_set.all():
                is_same = True
                old_arch_outputs = json.loads(old_arch.outputs)
                for i in range(len(old_arch_outputs)):
                    if old_arch_outputs[i] != architecture['outputs'][i]:
                        is_same = False
                if is_same:
                    break

            if not is_same:
                architecture['id'] = user_info.eosscontext.last_arch_id
                print(user_info.eosscontext.last_arch_id)
                add_design(architecture, user_info.eosscontext, False)

            user_info.save()

            # End the connection before return statement
            self.VASSARClient.endConnection()
            return Response(architecture)

        except Exception:
            logger.exception('Exception in evaluating an architecture')
            self.VASSARClient.endConnection()
            return Response('')
Exemple #10
0
    def post(self, request, format=None):
        if request.user.is_authenticated:
            try:
                user_info = get_or_create_user_information(
                    request.session, request.user, 'EOSS')

                # Start connection with VASSAR
                port = user_info.eosscontext.vassar_port
                client = VASSARClient(port)
                client.startConnection()

                problem = request.data['problem']
                inputType = request.data['inputType']

                # Call the GA stop function on Engineer
                if inputType == 'binary':
                    client.client.stopGABinaryInput(request.user.username)
                    while client.client.isGABinaryInputRunning():
                        time.sleep(0.1)
                elif inputType == 'discrete':
                    client.client.stopGADiscreteInput(request.user.username)
                    while client.client.isGADiscreteInputRunning():
                        time.sleep(0.1)
                else:
                    raise ValueError(
                        'Unrecognized input type: {0}'.format(inputType))

                # End the connection before return statement
                client.endConnection()

                return Response('GA stopped correctly!')

            except Exception:
                logger.exception('Exception in stopping the GA!')
                client.endConnection()
                return Response('')

        else:
            return Response('This is only available to registered users!')
Exemple #11
0
    def expert_critic(self, design):
        # Criticize architecture (based on rules)
        port = self.context.eosscontext.vassar_port
        problem = self.context.eosscontext.problem
        inputs = json.loads(design.inputs)
        client = VASSARClient(port)
        client.startConnection()

        result_list = None
        if problem in self.assignation_problems:
            result_list = client.client.getCritiqueBinaryInputArch(
                problem, inputs)
        elif problem in self.partition_problems:
            result_list = client.client.getCritiqueDiscreteInputArch(
                problem, inputs)

        client.endConnection()

        result = []
        for advice in result_list:
            result.append({"type": "Expert", "advice": advice})

        return result
Exemple #12
0
    def post(self, request, format=None):
        try:
            # Start connection with VASSAR
            user_info = get_or_create_user_information(request.session,
                                                       request.user, 'EOSS')
            port = user_info.eosscontext.vassar_port
            client = VASSARClient(port)
            client.startConnection()

            assignation_problems = [
                'SMAP', 'SMAP_JPL1', 'SMAP_JPL2', 'ClimateCentric'
            ]
            partition_problems = ['Decadal2017Aerosols']

            # Get the correct architecture
            this_arch = None
            arch_id = int(request.data['arch_id'])
            problem = request.data['problem']
            for arch in user_info.eosscontext.design_set.all():
                if arch.id == arch_id:
                    if problem in assignation_problems:
                        this_arch = BinaryInputArchitecture(
                            arch.id, json.loads(arch.inputs),
                            json.loads(arch.outputs))
                    elif problem in partition_problems:
                        this_arch = DiscreteInputArchitecture(
                            arch.id, json.loads(arch.inputs),
                            json.loads(arch.outputs))
                    break

            subobjective_explanation = None
            if problem in assignation_problems:
                subobjective_explanation = client.client.getSubscoreDetailsBinaryInput(
                    problem, this_arch, request.data['subobjective'])
            elif problem in partition_problems:
                subobjective_explanation = client.client.getSubscoreDetailsDiscreteInput(
                    problem, this_arch, request.data['subobjective'])

            # End the connection before return statement
            client.endConnection()

            def explanation_to_json(explanation):
                json_exp = {
                    'subobjective': request.data['subobjective'],
                    'param': explanation.param,
                    'attr_names': explanation.attr_names,
                    'attr_values': explanation.attr_values,
                    'scores': explanation.scores,
                    'taken_by': explanation.taken_by,
                    'justifications': explanation.justifications
                }
                return json_exp

            return Response({
                'subobjective':
                explanation_to_json(subobjective_explanation)
            })

        except Exception:
            logger.exception(
                'Exception when retrieving information from the current architecture!'
            )
            client.endConnection()
            return Response('')
Exemple #13
0
    def post(self, request, format=None):
        try:
            # Start connection with VASSAR
            user_info = get_or_create_user_information(request.session,
                                                       request.user, 'EOSS')
            port = user_info.eosscontext.vassar_port
            client = VASSARClient(port)
            client.startConnection()

            assignation_problems = [
                'SMAP', 'SMAP_JPL1', 'SMAP_JPL2', 'ClimateCentric'
            ]
            partition_problems = ['Decadal2017Aerosols']

            # Get the correct architecture
            this_arch = None
            arch_id = int(request.data['arch_id'])
            problem = request.data['problem']
            for arch in user_info.eosscontext.design_set.all():
                if arch.id == arch_id:
                    if problem in assignation_problems:
                        this_arch = BinaryInputArchitecture(
                            arch.id, json.loads(arch.inputs),
                            json.loads(arch.outputs))
                    elif problem in partition_problems:
                        this_arch = DiscreteInputArchitecture(
                            arch.id, json.loads(arch.inputs),
                            json.loads(arch.outputs))
                    break

            score_explanation = None
            cost_explanation = None
            if problem in assignation_problems:
                score_explanation = client.client.getArchScienceInformationBinaryInput(
                    problem, this_arch)
                cost_explanation = client.client.getArchCostInformationBinaryInput(
                    problem, this_arch)
            elif problem in partition_problems:
                score_explanation = client.client.getArchScienceInformationDiscreteInput(
                    problem, this_arch)
                cost_explanation = client.client.getArchCostInformationDiscreteInput(
                    problem, this_arch)

            # End the connection before return statement
            client.endConnection()

            def score_to_json(explanation):
                json_list = []
                for exp in explanation:
                    json_exp = {
                        'name': exp.name,
                        'description': exp.description,
                        'value': exp.value,
                        'weight': exp.weight
                    }
                    if exp.subscores is not None:
                        json_exp['subscores'] = score_to_json(exp.subscores)
                    json_list.append(json_exp)
                return json_list

            def budgets_to_json(explanation):
                json_list = []
                for exp in explanation:
                    json_exp = {
                        'orbit_name': exp.orbit_name,
                        'payload': exp.payload,
                        'launch_vehicle': exp.launch_vehicle,
                        'total_mass': exp.total_mass,
                        'total_power': exp.total_power,
                        'total_cost': exp.total_cost,
                        'mass_budget': exp.mass_budget,
                        'power_budget': exp.power_budget,
                        'cost_budget': exp.cost_budget
                    }
                    json_list.append(json_exp)
                return json_list

            return Response({
                'score': score_to_json(score_explanation),
                'budgets': budgets_to_json(cost_explanation)
            })

        except Exception:
            logger.exception(
                'Exception when retrieving information from the current architecture!'
            )
            client.endConnection()
            return Response('')
Exemple #14
0
    def post(self, request, format=None):
        if request.user.is_authenticated:
            try:
                # Start listening for redis inputs to share through websockets
                connection = pika.BlockingConnection(
                    pika.ConnectionParameters(host='localhost'))
                channel = connection.channel()

                channel.queue_declare(queue=request.user.username + '_gabrain')
                channel.queue_purge(queue=request.user.username + '_gabrain')

                def callback(ch, method, properties, body):
                    thread_user_info = get_or_create_user_information(
                        request.session, request.user, 'EOSS')
                    message = json.loads(body)
                    if message['type'] == 'new_arch':
                        print('Processing some new archs!')
                        nonlocal inputs_unique_set
                        # Archs are added one by one
                        new_archs = [message['data']]
                        send_back = []
                        # Add archs to the context data before sending back to user
                        for arch in new_archs:
                            hashed_input = hash(tuple(arch['inputs']))
                            if hashed_input not in inputs_unique_set:
                                full_arch = {
                                    'id':
                                    thread_user_info.eosscontext.last_arch_id,
                                    'inputs': arch['inputs'],
                                    'outputs': arch['outputs']
                                }
                                if thread_user_info.eosscontext.activecontext.show_background_search_feedback:
                                    add_design(full_arch,
                                               thread_user_info.eosscontext,
                                               False)
                                else:
                                    add_design(full_arch,
                                               thread_user_info.eosscontext,
                                               True)
                                send_back.append(full_arch)
                                inputs_unique_set.add(hashed_input)
                                thread_user_info.save()

                        # Look for channel to send back to user
                        channel_layer = get_channel_layer()

                        background_queue_qs = Design.objects.filter(
                            activecontext_id__exact=thread_user_info.
                            eosscontext.activecontext.id)
                        if background_queue_qs.count() >= 10:
                            async_to_sync(
                                channel_layer.send
                            )(thread_user_info.channel_name, {
                                'type': 'active.notification',
                                'notification': {
                                    'title': 'Background search results',
                                    'message':
                                    'The background search has found more than 10 architectures, but you have chosen to not show them. Do you want to see them now?',
                                    'setting':
                                    'show_background_search_feedback'
                                }
                            })
                        if thread_user_info.eosscontext.activecontext.show_background_search_feedback:
                            back_list = send_archs_from_queue_to_main_dataset(
                                thread_user_info)
                            send_back.extend(back_list)
                            send_archs_back(channel_layer,
                                            thread_user_info.channel_name,
                                            send_back)
                    if message['type'] == 'ga_started':
                        # Look for channel to send back to user
                        channel_layer = get_channel_layer()
                        async_to_sync(channel_layer.send)(
                            thread_user_info.channel_name, {
                                'type': 'ga.started'
                            })
                    if message['type'] == 'ga_done':
                        channel_layer = get_channel_layer()
                        async_to_sync(channel_layer.send)(
                            thread_user_info.channel_name, {
                                'type': 'ga.finished'
                            })
                        print('Ending the thread!')
                        channel.stop_consuming()

                channel.basic_consume(callback,
                                      queue=request.user.username + '_gabrain',
                                      no_ack=True)

                thread = threading.Thread(target=channel.start_consuming)
                thread.start()

                # Start connection with VASSAR
                user_info = get_or_create_user_information(
                    request.session, request.user, 'EOSS')
                port = user_info.eosscontext.vassar_port
                client = VASSARClient(port)
                client.startConnection()

                problem = request.data['problem']
                inputType = request.data['inputType']

                # Restart archs queue before starting the GA again
                Design.objects.filter(activecontext__exact=user_info.
                                      eosscontext.activecontext).delete()
                user_info.eosscontext.last_arch_id = user_info.eosscontext.design_set.count(
                )
                user_info.eosscontext.save()

                # Convert the architecture list and wait for threads to be available (ask for stop again just in case)
                thrift_list = []
                inputs_unique_set = set()

                if inputType == 'binary':
                    for arch in user_info.eosscontext.design_set.all():
                        thrift_list.append(
                            BinaryInputArchitecture(arch.id,
                                                    json.loads(arch.inputs),
                                                    json.loads(arch.outputs)))
                        hashed_input = hash(tuple(json.loads(arch.inputs)))
                        inputs_unique_set.add(hashed_input)
                    client.client.stopGABinaryInput(request.user.username)
                    while client.client.isGABinaryInputRunning():
                        time.sleep(0.1)
                    client.client.startGABinaryInput(problem, thrift_list,
                                                     request.user.username)

                elif inputType == 'discrete':
                    for arch in user_info.eosscontext.design_set.all():
                        thrift_list.append(
                            DiscreteInputArchitecture(arch.id,
                                                      json.loads(arch.inputs),
                                                      json.loads(
                                                          arch.outputs)))
                        hashed_input = hash(tuple(json.loads(arch.inputs)))
                        inputs_unique_set.add(hashed_input)
                    client.client.stopGADiscreteInput(request.user.username)
                    while client.client.isGADiscreteInputRunning():
                        time.sleep(0.1)
                    client.client.startGADiscreteInput(problem, thrift_list,
                                                       request.user.username)
                else:
                    raise ValueError(
                        'Unrecognized input type: {0}'.format(inputType))

                # End the connection before return statement
                client.endConnection()

                return Response('GA started correctly!')

            except Exception:
                logger.exception('Exception in starting the GA!')
                return Response('')

        else:
            return Response('This is only available to registered users!')
Exemple #15
0
    def explorer_critic(self, design):
        def get_advices_from_bit_string_diff(difference):
            out = []
            ninstr = len(self.instruments_dataset)

            for i in range(len(difference)):
                advice = []
                if difference[i] == 1:
                    advice.append("add")
                elif difference[i] == -1:
                    advice.append("remove")
                else:
                    continue

                orbit_index = i // ninstr  # Floor division
                instr_index = i % ninstr  # Get the remainder
                advice.append("instrument {}".format(
                    self.instruments_dataset[instr_index]['name']))

                if difference[i] == 1:
                    advice.append("to")
                elif difference[i] == -1:
                    advice.append("from")

                advice.append("orbit {}".format(
                    self.orbits_dataset[orbit_index]['name']))

                advice = " ".join(advice)
                out.append(advice)

            out = ", and ".join(out)
            out = out[0].upper() + out[1:]
            return out

        original_outputs = json.loads(design.outputs)
        original_inputs = json.loads(design.inputs)
        problem = self.context.eosscontext.problem
        port = self.context.eosscontext.vassar_port
        client = VASSARClient(port)
        client.startConnection()

        archs = None
        advices = []
        if problem in self.assignation_problems:
            archs = client.client.runLocalSearchBinaryInput(
                problem, json.loads(design.inputs))

            for arch in archs:
                new_outputs = arch.outputs

                new_design_inputs = arch.inputs
                diff = [
                    a - b for a, b in zip(new_design_inputs, original_inputs)
                ]
                advice = [get_advices_from_bit_string_diff(diff)]

                # TODO: Generalize the code for comparing each metric. Currently it assumes two metrics: science and cost
                if new_outputs[0] > original_outputs[0] and new_outputs[
                        1] < original_outputs[1]:
                    # New solution dominates the original solution
                    advice.append(
                        " to increase the science benefit and lower the cost.")
                elif new_outputs[0] > original_outputs[0]:
                    advice.append(
                        " to increase the science benefit (but cost may increase!)."
                    )
                elif new_outputs[1] < original_outputs[1]:
                    advice.append(
                        " to lower the cost (but science may decrease too!).")
                else:
                    continue

                advice = "".join(advice)
                advices.append(advice)
        elif problem in self.partition_problems:
            archs = client.client.runLocalSearchDiscreteInput(
                problem, json.loads(design.inputs))

            # TODO: Add the delta code for discrete architectures

        client.endConnection()
        result = []
        for advice in advices:
            result.append({"type": "Explorer", "advice": advice})
        return result