โŒ

Normal view

There are new articles available, click to refresh the page.
Before yesterdayMain stream

Do I need declare as volatile the supplier class member in order to preserve thread-safety

I am reading this book "Functional Programming in Java Harnessing the Power Of Java 8 Lambda Expressions" and in chapter 6 (lazy evaluations) there's an example of heavy object creation. Basically the example looks like this:

public class HeavyObjectCreation {

    public static void main(String[] args) {
        HeavyObjectCreation h = new HeavyObjectCreation();
        h.getHeavy();
        h.getHeavy();
        h.getHeavy();
    }

    public static class Heavy {}
    private Supplier<Heavy> heavy = this::createAndCacheHeavy;
    public HeavyObjectCreation() {

    }
    public Heavy getHeavy() {
        System.out.println("Getting heavy");
        return heavy.get();
    }
    private synchronized Heavy createAndCacheHeavy() {
        class HeavyFactory implements Supplier<Heavy> {
            private final Heavy heavyInstance = new Heavy();
            public Heavy get() { return heavyInstance; }
        }

        System.out.println("bypassing");

        if(!(heavy instanceof HeavyFactory)) {
            System.out.println("Creating heavy");
            heavy = new HeavyFactory();
        }
        return heavy.get();
    }
}

I was thinking whether I should declare "heavy" class member as volatile since it is accessed in two places: createAndCacheHeavy() which is synchronized and getHeavy() which is not synchronized. So my assumption is that there is no happens-before relationship between writing of "heavy" reference and reading it and that is why I think it should be declared as volatile:

private volatile Supplier heavy = ...

I would appreciate any help. Thanks!

P.S getHeavy() is invoked by other threads.

Assignment problem using scipy optimize linprog to solve in Python

I am running into an error when trying to run my code for this Assignment Cost minimization problem in Python and was hoping someone could illuminate me on what I have done incorrectly here:

The Question:

My code:

import numpy as np
import time
n = 10   # n: number of jobs/workers
cost = np.random.rand(n,n)   # generate an n-by-n random matrix
from scipy.optimize import linprog
c = cost.reshape((n**2,1))   # reshape the cost matrix to a column vector
#row constraints (sum of each row equals 1)
A_eq_rows = np.ones((n, n**2))
b_eq_rows = np.ones(n)

#column constraints (sum of each column equals 1)
A_eq_cols = np.zeros((n, n**2))
for i in range(n):
    A_eq_cols[i, i::n] = 1
b_eq_cols = np.ones(n)

#solve for A_eq and 
A_eq=np.vstack((A_eq_rows, A_eq_cols))
b_eq=np.hstack((b_eq_rows, b_eq_cols))
start_time = time.time()   # start timing

res = linprog(c, None, None, A_eq, b_eq)

end_time = time.time()   # end timing
elapsed_LP = end_time - start_time
print("Minimum cost = ", res.fun)
print("Elapsed time = ", elapsed_LP)

I keep getting my Minimum Cost as None and am not entirely sure what I've done wrong with this. I have a decent fundamental understanding of how to resolve this problem but have limited experience in doing this through Python.

I know that it has n^2 variables and 2n-1 linearly independent constraints. Therefore, a BFS of the assignment problem has 2n-1 basic variables, and for each assignments, exactly n of the n2 variables are nonzero so every BFS is degenerate. Any help in this would be greatly appreciated.

NOTE: I am aware I can solve this through the function scipy.optimize.linear sum assignment() and have done this but wanted to compare my answer by running this problem as a linear program.

Neataptic Machine-Learning - Flatten inputs or nested & based on features or samples

I am learning ML NEAT with Neataptic within NodeJS.

I am making a basic weather prediction model. Essentially predict the next temperature.

I have this type of data, where I want to give samples of the last 3 days with 2 features date and avgTemp.

      {
        "inputs": [
          {
            "AvgTemperature": "47.9",
            "Date": "1/1/2020"
          },
          {
            "AvgTemperature": "47.8",
            "Date": "1/2/2020"
          },
          {
            "AvgTemperature": "48.8",
            "Date": "1/3/2020"
          },
        ],
        "expectedOutput": {
          "AvgTemperature": "46.9"
        }
      },
    ]

I am unsure on what format the network is expecting.

these are a few of the ideas I had:

genom.activate([
[47.9, 1/1/2020],
[50, 1/2/2020],
[55, 1/3/2020]
])

with a network setup of maybe 
/*
const inputLen = 3 //To represent 3 samples
//or
const inputLen = 2 //To represent 2 features
const inputLen = [3,2] //To represent 3 samples and 2 features
*/
            const outputLen = 1

            this.neat = new Neat(
                inputLen,
                outputLen,
                null,
                {
                    elitism: 5,
                    popsize: 50,
                    mutationRate: 0.3,
                    network: new architect.Random(inputLen, 3, outputLen),
                }
            );


or which I think is most likely, I need to flatten the inputs. And if that's the case, how should I flatten it, like either of the below. and if I do need to flatten it I assume the would be the features*samples=inputLen And how will it know which are different features and which are like via the date, or does that not matter it will just find its own way of related the temps and dates.

genom.activate([date, temp, date, temp ...])
//or 
genom.activate([date, date, temp, temp ...])  

---- SIDE NOTES ----

  • Yes I will be normalizing my input values
  • I have about 600 blocks of samples I will be feeding it.
  • I did google this and looked on this site. Though I couldn't find much on neataptic spesfically and on wether its required to flatten the data, more so I found just how to .flatten() an array.

Can't initialize boost::asio::io_service

I'm trying to write a server using boost/asio, I have "class Server", when I try to pass in the parameters of this class io_service, An error occurs that this io_service not available in boost/asio.

Server.h

#pragma once

#define _WIN32_WINNT

#include <iostream>
#include <string>
#include <boost/asio.hpp>

class Server
{
    std::string server_address;
    std::string server_port;
    boost::asio::io_service server_io_service;
    boost::asio::ip::tcp::endpoint server_ep;
    boost::asio::ip::tcp::acceptor server_acceptor;
    boost::asio::ip::tcp::socket server_socket;


public:
    explicit Server(std::string address, std::string port, boost::asio::io_service& io_service);
    explicit Server();
    ~Server();

    void start_listening() noexcept; 
};

Server.cpp

#include "../headers/server.h"

Server::Server(std::string address, std::string port, boost::asio::io_service& io_service) :
    server_address(address),
    server_port(port),
    server_io_service(io_service), // error ----------------------------------------
    server_ep(boost::asio::ip::tcp::v4(), std::stoi(port)),
    server_acceptor(io_service, server_ep),
    server_socket(io_service)
{}

output [1/2] Building CXX object CMakeFiles\Server.dir\source\Server.cpp.obj FAILED: CMakeFiles/Server.dir/source/Server.cpp.obj C:\PROGRA~1\MICROS~4\2022\COMMUN~1\VC\Tools\MSVC\1438~1.331\bin\Hostx64\x64\cl.exe /nologo /TP -ID:\boost_1_84_0\boost_1_84_0 /DWIN32 /D_WINDOWS /W3 /GR /EHsc /MDd /Ob0 /Od /RTC1 -std:c++20 -ZI /showIncludes /FoCMakeFiles\Server.dir\source\Server.cpp.obj /FdCMakeFiles\Server.dir\ /FS -c D:\C++\Server\source\Server.cpp D:\boost_1_84_0\boost_1_84_0\boost\asio\detail\config.hpp(667): fatal error C1017: ะฝะตะดะพะฟัƒัั‚ะธะผะพะต ะบะพะฝัั‚ะฐะฝั‚ะฝะพะต ะฒั‹ั€ะฐะถะตะฝะธะต ั†ะตะปะพะณะพ ั‚ะธะฟะฐ ninja: build stopped: subcommand failed.

[enter image description here] (https://i.stack.imgur.com/rYYeN.png)

I tried to find information about this problem in the documentation "Boost/asio", but I couldn't find anything about it.

Clingo beginner looking for some guidance

I am very new to Clingo and I am having trouble developing an OR condition to work. My idea is to create a rule that find a mention of the word "burner" in the content OR the App (basically combining the "contains_burner_content" and "contains_burner_app"). I have tried using 'not' but it doesnt seem to work.

I am also trying to see if its possible to use some sort of wildcard in the content to find any mention of the word "burner" rather than exact, however everything I have seen suggests this isnt possible.

Below is my code that I have developed so far.

activity("Not", "unknown", "910-248-4781", "2022-12-01", "12:16", "Login", "burner").
activity("Burner", "unknown", "910-248-4781", "2022-12-01", "12:19", "Created My Burner number", "unknown").
activity("Burner", "unknown", "910-248-4781", "2022-12-01", "13:28", "Received Picture", "OG use of Cookies").
activity("Burner", "unknown", "910-248-4781", "2022-12-01", "13:30", "Saved Picture", "OG use of Cookies").
activity("Burner", "unknown", "910-248-4781", "2022-12-01", "13:31", "Sent Message", "burner").

% Rule to identify activities where 'Content' contains the word "burner"
contains_burner_content(App, ContactID, PhoneNumber, Date, Time, Action, Content) :-
    activity(App, ContactID, PhoneNumber, Date, Time, Action, Content),
    contains_word_burner(Content).
    
contains_burner_app(App, ContactID, PhoneNumber, Date, Time, Action, Content) :-
    activity(App, ContactID, PhoneNumber, Date, Time, Action, Content),
    contains_word_burner(App).
    
contains_word_burner(Content) :- Content = "burner".
contains_word_burner(App) :- App = "Burner".

#show contains_burner_content/7.
#show contains_burner_app/7.

I am hoping to combine my two rules into one, also any help in the wildcard would be greatly appreciated. :)

โŒ
โŒ