关于使用深度强化学习Actor-Critic算法玩gym库中CartPole游戏不收敛的问题,高分悬赏。 500C

小弟最近在自学深度强化学习,看的莫烦大佬的视频。其中有一个用AC算法玩gym库中CartPole的游戏实例,自己写的代码不知为何不能够收敛。考虑到自己自己写的程序中将AC网络写到一个类里去了,尝试过在A网络训练时截断C网络的梯度反向传播防止干扰,但还是不收敛。
小弟小白初学者自己瞎琢磨的,实在找不出原因,高分悬赏,希望大佬们能解惑。代码如下,其中有两个文件,一个是用以运行的主程序,另一个是主程序要调用的类,大佬们跑一下试试。
另外,真心诚意提问,请勿复制粘贴答非所问。

########主程序:AC_RL_run_this##########

import gym
from AC_RL_brain import ACNetwork


def run_game():
    step = 0
    for episode in range(100000):
        episode_reward = 0
        observation = env.reset()
        while True:
            if episode_reward > 20:
                env.render()
            action = RL.choose_action(observation)
            observation_, reward, done, _ = env.step(action)
            if done:
                reward = -20
            RL.C_learn(observation, reward, observation_)
            RL.A_learn(observation, action)
            episode_reward += reward
            if done:
                break
            observation = observation_
            step += 1
        print('%d回合总回报:%f' % (episode, episode_reward))
    print('game over')
    env.close()


if __name__ == '__main__':
    env = gym.make('CartPole-v0')
    env.seed(1)
    RL = ACNetwork(
        n_actions=env.action_space.n,
        n_features=env.observation_space.shape[0],
        gamma=0.95,
        A_lr=0.001,
        C_lr=0.01,
    )
    run_game()

########需要调用的类:AC_RL_brain##########

import tensorflow as tf
import numpy as np


np.random.seed(2)
tf.set_random_seed(2)  # reproducible


class ACNetwork:
    def __init__(
            self,
            n_actions,
            n_features,
            gamma,
            A_lr,
            C_lr,
    ):
        self.n_actions = n_actions
        self.n_features = n_features
        self.gamma = gamma
        self.A_lr = A_lr
        self.C_lr = C_lr
        self.td_error_real = 0
        self._build_net()
        self.sess = tf.Session()
        self.sess.run(tf.global_variables_initializer())

    def _build_net(self):
        # placeholder
        self.s = tf.placeholder(tf.float32, [1, self.n_features], "state")
        self.v_ = tf.placeholder(tf.float32, [1, 1], "v_next")
        self.r = tf.placeholder(tf.float32, None, 'r')
        self.a = tf.placeholder(tf.int32, None, "act")

        # A_net
        l1_A = tf.layers.dense(
            inputs=self.s,
            units=20,  # number of hidden units
            activation=tf.nn.relu,
            kernel_initializer=tf.random_normal_initializer(0., .1),  # weights
            bias_initializer=tf.constant_initializer(0.1),  # biases
        )

        self.acts_prob = tf.layers.dense(
            inputs=l1_A,
            units=self.n_actions,  # output units
            activation=tf.nn.softmax,  # get action probabilities
            kernel_initializer=tf.random_normal_initializer(0., .1),  # weights
            bias_initializer=tf.constant_initializer(0.1),  # biases
        )

        self.log_prob = tf.log(self.acts_prob[0, self.a])
        self.exp_v = tf.reduce_mean(self.log_prob * self.td_error_real)  # advantage (TD_error) guided loss
        self.train_op_A = tf.train.AdamOptimizer(self.A_lr).minimize(-self.exp_v)  # minimize(-exp_v) = maximize(exp_v)

        # C_net
        l1_C = tf.layers.dense(
            inputs=self.s,
            units=20,  # number of hidden units
            activation=tf.nn.relu,  # None
            # have to be linear to make sure the convergence of actor.
            # But linear approximator seems hardly learns the correct Q.
            kernel_initializer=tf.random_normal_initializer(0., .1),  # weights
            bias_initializer=tf.constant_initializer(0.1),  # biases
        )

        self.v = tf.layers.dense(
            inputs=l1_C,
            units=1,  # output units
            activation=None,
            kernel_initializer=tf.random_normal_initializer(0., .1),  # weights
            bias_initializer=tf.constant_initializer(0.1),  # biases
        )

        self.td_error = self.r + self.gamma * self.v_ - self.v
        self.loss = tf.square(self.td_error)  # TD_error = (r+gamma*V_next) - V_eval
        self.train_op_C = tf.train.AdamOptimizer(self.C_lr).minimize(self.loss)

    def choose_action(self, s):
        s = s[np.newaxis, :]
        probs = self.sess.run(self.acts_prob, {self.s: s})  # get probabilities for all actions
        return np.random.choice(np.arange(probs.shape[1]), p=probs.ravel())  # return a int

    def A_learn(self, s, a):
        s = s[np.newaxis, :]
        feed_dict = {self.s: s, self.a: a}
        _, exp_v = self.sess.run([self.train_op_A, self.exp_v], feed_dict)

    def C_learn(self, s, r, s_):
        s, s_ = s[np.newaxis, :], s_[np.newaxis, :]
        v_ = self.sess.run(self.v, {self.s: s_})
        self.td_error_real, _ = self.sess.run([self.td_error, self.train_op_C],
                                    {self.s: s, self.v_: v_, self.r: r})

1个回答

class Brain:
def init(self):
self.params = {}
self.model, self.trainer, self.loss = self._create()

def _create(self):
    observation = C.sequence.input_variable(n_state, np.float32, name='s')
    q_target = C.sequence.input_variable(n_action, np.float32, name='q')

    l1 = C.layers.Dense(hidden_size, activation=C.relu)
    l2 = C.layers.Dense(n_action)
    unbound_model = C.layers.Sequential([l1, l2])
    self.model = unbound_model(observation)

    self.params = dict(W1=l1.W, b1=l1.b, W2=l2.W, b2=l2.b)

    self.loss = C.reduce_mean(C.square(self.model-q_target), axis=0)
    meas = C.reduce_mean(C.square(self.model-q_target), axis=0)

    lr_schedule = C.learning_rate_schedule(learning_rate, C.UnitType.minibatch)
    learner = C.sgd(self.model.parameters,
                    lr_schedule,
                    gradient_clipping_threshold_per_sample=10)

    progress_printer = C.logging.ProgressPrinter(500)
    self.trainer = C.Trainer(self.model, (self.loss, meas), learner, progress_printer)

    return self.model, self.trainer, self.loss

def train(self, x, y):
    arguments = dict(zip(self.loss.arguments, [x,y]))
    updated, results = self.trainer.train_minibatch(arguments, outputs=[self.loss.output])

def predict(self, s):
    return self.model.eval([s])

class Memory: # stored as ( s, a, r, s_ )
samples = []

def __init(self):
    pass

def add(self, sample):
    self.samples.append(sample)

def sample(self, n):
    n = min(n, len(self.samples))
    return random.sample(self.samples, n)

class Agent:
steps = 0
epsilon = max_epsilon

def __init__(self):
    self.brain = Brain()
    self.memory = Memory()

def act(self, s):
    if random.random() < self.epsilon:
        return random.randint(0, n_action-1)
    else:
        return np.argmax(self.brain.predict(s))

def observe(self, sample): # in (s, a, r, s_) format
    self.memory.add(sample)
    self.steps += 1
    self.epsilon = min_epsilon + (max_epsilon - min_epsilon) * math.exp(-epsilon_decay * self.steps)

def replay(self):
    batch = self.memory.sample(batch_size)

    no_state = np.zeros(n_state)

    states = np.array([ o[0] for o in batch ], dtype=np.float32)
    states_ = np.array([ (no_state if o[3] is None else o[3]) for o in batch ], dtype=np.float32)

    p = self.brain.predict(states)
    p_ = self.brain.predict((states_))

    x = np.zeros((len(batch), n_state)).astype(np.float32)
    y = np.zeros((len(batch), n_action)).astype(np.float32)

    for i in range(len(batch)):
        s, a, r, s_ = batch[i]

        t = p[0][i] # CNTK: [0] because of sequence dimension
        if s_ is None:
            t[a] = r
        else:
            t[a] = r + reward_discount * np.amax(p_[0][i])

        x[i] = s
        y[i] = t

    self.brain.train(x, y)

def run(agent):
s = env.reset()
R = 0

while True:
    env.render()

    a = agent.act(s.astype(np.float32))
    s_, r, done, info = env.step(a)

    if done:
        s_ = None

    agent.observe((s, a, r, s_))
    agent.replay()

    s = s_
    R += r

    if done:
        return R

agent = Agent()

epoch = 0
reward_sum = 0
while epoch < 30000:
reward = run(agent)
reward_sum += reward
epoch += 1
if epoch % epoch_baseline == 0:
print('Epoch %d, average reward is %f, memory size is %d'
% (epoch, reward_sum / epoch_baseline, len(agent.memory.samples)))

    if reward_sum / epoch_baseline > reward_target:
        print('Task solved in %d epoch' % epoch)
        break

    reward_sum = 0
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
关于actor platform 服务器的搭建

有朋友研究过github上的actir platform 吗?目前我需要搭建一个这样的服务器,但是确实不知道如何入手。如果有朋友能教我搭建成功这个服务器,我愿意有偿答谢!

有人了解github上面的actor messaging platform 吗?

有人了解github上面的actor messaging platform 吗?我照着官方文档配置服务器,sbt编译一直没有通过,求大神指点!

如何在Actor系统中为Kafka使用者建模

<div class="post-text" itemprop="text"> <p>I have been looking into designing an actor system using proto actor and Go. </p> <p>I would like to include a kafka consumer, but I have been trying to understand how the communication would work with the parent/child relationship with the data from kafka. </p> <p>It makes sense to me that i should have a child actor that is a kafka consumer, also a kafka parent that managed how many consumers to create based on partitions. </p> <p><a href="https://i.stack.imgur.com/D7GGi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/D7GGi.png" alt="enter image description here"></a></p> <p>The idea is the unbound kafka data consumed is then passed to Group 1/2 actors which would send data to child actors, the data received from kafka would contain an ID which is relevant to only one child actor and that child would update its state.</p> <p>My question is how would the kafka child actors data be sent to Group 1/2 actors? Should it be a defined "route" and Group 1/2 actors broadcast the messages to all children, then the children filter on id? Or should the data propagate back up the hierarchy to the manager and then to Group 1/2? </p> </div>

虚幻4引擎中将Actor放入关卡中怎么重建对象

![图片说明](https://img-ask.csdn.net/upload/201903/11/1552285559_300765.jpg) 就是这个东西

HTTP处理程序中actor模式的好处

<div class="post-text" itemprop="text"> <p>I've been reading a few go blogs and and more recently I stubbled upon Peter Bourgon's talk titled "Ways to do things". He shows a few examples of the actor pattern for concurrency in GO. Here is a handler example using such pattern:</p> <pre><code>func (a *API) handleNext(w http.ResponseWriter, r *http.Request) { var ( notFound = make(chan struct{}) otherError = make(chan error) nextID = make(chan string) ) a.action &lt;- func() { s, err := a.log.Oldest() if err == ErrNoSegmentsAvailable { close(notFound) return } if err != nil { otherError &lt;- err return } id := uuid.New() a.pending[id] = pendingSegment{s, time.Now().Add(a.timeout), false} nextID &lt;- id } select { case &lt;-notFound: http.NotFound(w, r) case err := &lt;-otherError: http.Error(w, err.Error(), http.StatusInternalServerError) case id := &lt;-nextID: fmt.Fprint(w, id) } } </code></pre> <p>And there's a loop behind the scenes listening for the <code>action</code> channel:</p> <pre><code>func (a *API) loop() { for { select { case f := &lt;-a.action: f() } } } </code></pre> <p>My question is what is the benefit to all of this? The handler isn't any faster because it is still blocking until some action in the action func returns something to it. Which is essentially the same thing as just returning the function from outside the go routine. What am I missing here?</p> </div>

如何以编程方式从具有特殊语法的字符串派生嵌套对象字段的值?

<div class="post-text" itemprop="text"> <p>In the following example, we have a string consisting of words separated by spaces. Each word in the string will represent a nesting level of an object. In actual implementation, I will not know the string ahead of time. </p> <pre><code>$string = "Actor Name"; $object-&gt;Actor-&gt;Name = "John Doe"; function getValue($string, $object) { // do stuff return $value; // John Doe } </code></pre> <p>Another example:</p> <pre><code>$string = "Actor Email"; $object-&gt;Actor-&gt;Email = "johndoe@example.com"; $value = getValue($string, $object); // johndoe@example.com </code></pre> </div>

编译protobuf并尝试在其他软件包中使用时,未定义结构的MarshalToSizedBuffer

<div class="post-text" itemprop="text"> <p>I'm using gogo/protobuf to generate protobufs for gRPC calls but recently they started failing due a marshalling method not being correctly generated. The error looks like this:</p> <pre><code>service/v1/service.pb.go:1347:27: m.ListMeta.MarshalToSizedBuffer undefined (type *"k8s.io/apimachinery/pkg/apis/meta/v1".ListMeta has no field or method MarshalToSizedBuffer) </code></pre> <p>I've tried reinstalling go and goenv, using a different gvm, and reverting changes to the proto files. The issue seems to resolve on our last stable branch of our proto files but if I make any changes the issue recurs. This is across two repos, one (proto files repo) using golang 1.11.1 and the other using golang 1.12.5.</p> <p>I expected all methods that the protobufs needed to be generated in using the package.</p> </div>

PHP SOAP客户端无效的actor

<div class="post-text" itemprop="text"> <p>I've never worked with SOAP before. I have got everything to work, but I get a PHP warning in the errors log.</p> <p>Warning:</p> <pre><code>[05-Nov-2015 11:06:06 UTC] PHP Warning: SoapHeader::SoapHeader(): Invalid actor in C:\www\srf.cdab.eu\wp-content\themes\mypage\includes\soap.php on line 61 </code></pre> <p>soap.php:61</p> <pre><code>$objSoapVarWSSEHeader = new SoapHeader($strWSSENS, 'Security',&gt; $objSoapVarHeaderVal, true, NULL); </code></pre> <p>I've googled and haven't find anything that explains why I get this error. What could be the reason of this problem?</p> </div>

为什么解析xml 文件 解析出来的信息 向数据库中添加的时候 说 空指针?解析的内容也是对的。。。

[code="java"] protected synchronized String doInBackground(String... params) { System.out.println("进入判断区了======》"); List<Movie> personList; try { String path= params[0]; System.out.println("解析的地址是。。。。。"+path); File file = new File(path); personList = SaxService.readXML(file);//将服务器地址传过来开始解析 for(Movie movie : personList) { movieId = movie.getMovieId();//得到int 类型的id号 television = movie.getTelevision(); name = movie.getName(); type = movie.getType(); region = movie.getRegion(); year = movie.getYear(); actor = movie.getActor(); num = movie.getNum(); link = movie.getLink(); System.out.println("得到的movieId ---呵呵呵呵呵呵呵----"+movieId+"------"); System.out.println("得到的television ---呵呵呵呵呵呵呵----"+television+"------"); System.out.println("得到的name ---呵呵呵呵呵呵呵----"+name+"------"); System.out.println("得到的type ---呵呵呵呵呵呵呵----"+type+"------"); System.out.println("得到的region ---呵呵呵呵呵呵呵----"+region+"------"); System.out.println("得到的year ---呵呵呵呵呵呵呵----"+year+"------"); System.out.println("得到的actor ---呵呵呵呵呵呵呵----"+actor+"------"); System.out.println("得到的num ---呵呵呵呵呵呵呵----"+num+"------"); System.out.println("得到的link ---呵呵呵呵呵呵呵----"+link+"------"); } movie = new Movie(); movie.setName(name); movie.setActor(actor); movie.setLink(link); movie.setNum(num); movie.setRegion(region); movie.setTelevision(television); movie.setType(type); movie.setYear(year); // 下面这行几行 打印结果 按说应该像上面一样 打印出两条信息的 可是只打印了 解析出来的最后一条信息??? System.out.println("得到的movieId -------"+movieId+"------"); System.out.println("得到的television ------"+television+"------"); System.out.println("得到的name -------"+name+"------"); System.out.println("得到的type -------"+type+"------"); System.out.println("得到的region ------"+region+"------"); System.out.println("得到的year ------"+year+"------"); System.out.println("得到的actor ------"+actor+"------"); System.out.println("得到的num ------"+num+"------"); System.out.println("得到的link ------"+link+"------"); //movieList.add(movie); ms.addOnline_All(movie);//向数据库中添加 System.out.println("movie.setName(name)"+movieList.size());//因为解析出来的 有两条信息 所以打印出来 应该是 2 可是打印能出来的却是 1 } catch (Exception e) { e.printStackTrace(); } newloaclversion = loaclversion+1; try { //ReadAndWriteFile.WriteFile(); System.out.println("新版本号=====是:"+newloaclversion); } catch (Exception e) { e.printStackTrace(); } return null; } [/code]

如何用vtk实时显示组织变形

我想要实时显示组织变形,写的代码如下: … vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New(); vtkSmartPointer<vtkRenderer> ren = vtkSmartPointer<vtkRenderer>::New(); vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New(); vtkSmartPointer<vtkUnstructuredGrid> ugrid = vtkSmartPointer<vtkUnstructuredGrid>::New(); vtkSmartPointer<vtkDataSetMapper> ugMapper = vtkSmartPointer<vtkDataSetMapper>::New(); for(int i=0;i<node_num;i++) points->InsertNextPoint(node_pos[i]); ugrid->SetPoints(points); for(int i=0;i<ele_num;i++) ugrid->InsertNextCell(VTK_TETRA,4,ele[i]); ugMapper->SetInputData(ugrid); actor->SetMapper(ugMapper); ren->AddActor(actor); ren->ResetCamera(); renWin->SetSize(400,400); while(1) { if(receivedData()) { for(int i=0;i<node_num;i++) points->InsertNextPoint(node_pos[i]); } renWin->Render(); } … 得不到想要的结果,估计是points改变了之后,ugrid并没有更新它的点坐标值。 我想问一下在vtk中怎么通知vtkUnstructuredGrid数据改变了呢? Thanks

如何使用LEFT JOIN连接多个mySQL表?

<div class="post-text" itemprop="text"> <p>table <code>projects</code></p> <pre><code>+----+--------+------------+ | id | title | project_id | +----+--------+------------+ | 1 | blue | 12345 | | 2 | red | 67890 | | 3 | yellow | 11111 | | 4 | rosa | 22222 | +----+--------+------------+ </code></pre> <p>table <code>connect</code></p> <pre><code>+----+------------+-----------+ | id | project_id | people_id | +----+------------+-----------+ | 1 | 12345 | 4 | | 2 | 12345 | 3 | | 3 | 12345 | 2 | | 4 | 22222 | 2 | +----+------------+-----------+ </code></pre> <p>table <code>people</code></p> <pre><code>+----+-----------+-----------+----------+ | id | firstname | name | position | +----+-----------+-----------+----------+ | 1 | Diana | Rose | singer | | 2 | Al | Capone | singer | | 3 | Barbara | Streisand | actor | | 4 | Ben | Harper | musician | +----+-----------+-----------+----------+ </code></pre> <p>This is the result I wish:</p> <pre><code>+----+---------+--------+----------+----------+ | id | project | singer | musician | actor | +----+---------+--------+----------+----------+ | 1 | blue | Capone | Harper | Sreisand | | 4 | rosa | Capone | | | +----+---------+--------+----------+----------+ </code></pre> <p>I tried to achieve the result like this:</p> <pre><code>&lt;?php $pdo = $db-&gt;query(' SELECT * FROM projects LEFT JOIN connect ON projects.project_id=connect.project_id LEFT JOIN people ON connect.people_id=people.id;'); while ($row = $pdo-&gt;fetch(PDO::FETCH_ASSOC)) { echo &lt;td&gt;$row['title']&lt;/td&gt;; echo "&lt;td&gt;"; if ($row['position']== "singer"){echo $row['name'];} echo "&lt;/td&gt;"; echo "&lt;td&gt;"; if ($row['position']== "musician"){echo $row['name'];} echo "&lt;/td&gt;"; echo "&lt;td&gt;"; if ($row['position']== "actor"){echo $row['name'];} echo "&lt;/td&gt;"; } ?&gt; </code></pre> <p>But my result is:</p> <pre><code>+----+---------+--------+----------+----------+ | id | project | singer | musician | actor | +----+---------+--------+----------+----------+ | 1 | blue | Capone | | | | 1 | blue | | Harper | | | 1 | blue | | | Sreisand | | 4 | rosa | Capone | | | +----+---------+--------+----------+----------+ </code></pre> </div>

如何在MongoDB中使用$ in查询中的ObjectId?

<div class="post-text" itemprop="text"> <p>I have the following query in MongoDB</p> <pre><code>$this-&gt;createQueryBuilder() -&gt;field('actor')-&gt;in($actorIdArray) -&gt;getQuery() -&gt;execute(); </code></pre> <p>where the field actor is an object reference with annotation</p> <pre><code>@MongoDB\ReferenceOne(targetDocument="User", simple=true) </code></pre> <p>which means it will store the object Id instead of the full reference.</p> <p>When $actorIdArray is an array of id with the form</p> <pre><code>["5706cb39821b166d3931f34f", "56015f7d4f8bd90b769e6e75"] </code></pre> <p>the query does not return nothing, which is the expected since the filed actor contains object id.</p> <p>However, if I build the array this way</p> <pre><code>[new MongoId("5706cb39821b166d3931f34f"), new MongoId("56015f7d4f8bd90b769e6e75")] </code></pre> <p>it doesn't work either, which is quite surprising for me.</p> <p>The log shows the query is made</p> <pre><code>{ "actor": {"$in":[{"$id":"5706cb39821b166d3931f34f"},{"$id":"56015f7d4f8bd90b769e6e75"}]}} </code></pre> <p>and I think it should be something like this</p> <pre><code>{ "actor": {"$in":[ObjectId("5706cb39821b166d3931f34f"),ObjectId("56015f7d4f8bd90b769e6e75"]}} </code></pre> <p>Not sure if I am doing something wrong, any ideas?</p> </div>

CakePHP在连接表HABTM中保存数据

<div class="post-text" itemprop="text"> <p>CakePHP 2.4 according to the official API docs, I can call Model::saveAll()</p> <p>however , when I call the function Model::saveAll() , the browser return an error of 101(net::ERR_CONNECTION_RESET)</p> <p>instead , I can save data in a join table in a complicated way</p> <p>ex: Actor Model Film Model Actor HABTM Film I have a join table named actor_film</p> <p>I can do like this</p> <pre><code>$this-&gt;Actor-&gt;ActorFilm-&gt;save(array("ActorFilm"=&gt;array("actor_id"=&gt;1,"film_id"=&gt;2))); </code></pre> <p>that's so complicated</p> <p>Any one can help me ?</p> </div>

c++写一个动态库,可以调用动态库里面的函数生产stl文件,这个具体思路是咋样的啊

vtkSTLReader *part = vtkSTLReader::New(); /*part->SetOutput(part->GetOutput());*/ if (G == 1) part->SetFileName("standard_ankle.stl"); else if (G == 2) part->SetFileName("standard_elbow.stl"); else if (G == 3) part->SetFileName("standard_wrist.stl"); else if (G == 4) part->SetFileName("standard_wrist_st.stl"); part->Update(); //vtkPolyData *pd = part->GetOutput(); // vtkSphereSource *sphere = vtkSphereSource::New(); sphere->SetThetaResolution(12); sphere->SetPhiResolution(12); vtkPolyDataMapper *mapper = vtkPolyDataMapper::New(); //mapper->SetInputData(pd); mapper->SetInputConnection(part->GetOutputPort()); vtkActor *va = vtkActor::New(); va->SetMapper(mapper); vtkRenderer *a = vtkRenderer::New(); //vtkRenderWindow *b =vtkRenderWindow::New(); //b->AddRenderer(a); a->AddActor(va); a->SetBackground(.3, .6, .3); 怎么写才能调用这个a呀

比较3个表中列的值 - MySQL

<div class="post-text" itemprop="text"> <p>following question:</p> <p>I'm working with 3 tables = actors, movies, roles. I'm trying to find all the movies a given actor, say 'Robin Williams' has been in by comparing the specific actor id since there be may more than one actor with the same name. The actors table has the following relevant columns: first_name, last_name, id - the movies table has columns: id (the movie's id) - and the roles table has: actor_id and movie_id. </p> <p>Do I JOIN the tables or use UNION? How do I compare columns from different tables when the columns have different names?</p> <p>Thank you!</p> <p>Just for reference:</p> <p>Table actors:</p> <pre><code>mysql&gt; SELECT * -&gt; FROM actors; +--------+--------------------+------------------------+--------+------------+ | id | first_name | last_name | gender | film_count | +--------+--------------------+------------------------+--------+------------+ | 933 | Lewis | Abernathy | M | 1 | | 2547 | Andrew | Adamson | M | 1 | | 2700 | William | Addy | M | 1 | </code></pre> <p>Table movies:</p> <pre><code>mysql&gt; SELECT * -&gt; FROM movies; +--------+------------------------------+------+------+ | id | name | year | rank | +--------+------------------------------+------+------+ | 10920 | Aliens | 1986 | 8.2 | | 17173 | Animal House | 1978 | 7.5 | | 18979 | Apollo 13 | 1995 | 7.5 | </code></pre> <p>Table roles:</p> <pre><code>mysql&gt; SELECT * -&gt; FROM roles; +----------+----------+-------------------------------+ | actor_id | movie_id | role | +----------+----------+-------------------------------+ | 16844 | 10920 | Lydecker | | 36641 | 10920 | Russ Jorden | | 42278 | 10920 | Cpl. Dwayne Hicks | </code></pre> <p>At first I tried setting each check equal to a PHP variable and comparing them but that seemed wrong, then I tried:</p> <pre><code>mysql&gt; SELECT roles.actor_id, roles.movie_id, movies.id, actors.id -&gt; FROM roles -&gt; JOIN movies, actors -&gt; ON roles.actor_id = actors.id &amp;&amp; roles.movie_id =movies.id; </code></pre> <p>which again does not work.</p> </div>

postgre SQL初学,restore dvdrental 数据库时报错,请教如何解决?

postgre SQL初学,restore dvdrental时报错,请教如何解决?感谢! 有没有完好的PostgreSQL数据库资源供练习?最好还是dvdrental,在complete SQL bootcamp教程里使用的范例数据库 pg_restore: connecting to database for restore pg_restore: implied data-only restore pg_restore: processing data for table "public.actor" pg_restore: while PROCESSING TOC: pg_restore: from TOC entry 3057; 0 16757 TABLE DATA actor postgres pg_restore: error: could not execute query: 错误: 关系 "public.actor" 不存在 Command was: COPY public.actor (actor_id, first_name, last_name, last_update) FROM stdin; pg_restore: error: could not find header for file "3057.dat" in tar archive

怎么配置actor messaging platform 服务器?

最近在配置这个服务器时sbt编译出错,主要是因为有几个包下载失败,不知道如何解决,有没有大侠配置成功分享经验啊!!

如何使用MySQL中的关系将文本字符串批量转换为唯一ID

<div class="post-text" itemprop="text"> <p>I have a movie database I am working on and before I start working on the php side I want to make sure the database is solid.</p> <p>As I have mass imported data scraped from the web currently my results in the columns for genre and actors are in text strings. I want to convert them to unique ids and create relationship tables.</p> <p>Essentially how it is now:</p> <p>Movie Table</p> <blockquote> <pre><code>Movie ID - Movie name - Genres - Actors 1 - Inception - Sci Fi - Leonardo Di Caprio, Ellen Page </code></pre> </blockquote> <p>How I want it:</p> <p>Movie Table</p> <blockquote> <pre><code>Movie ID - Movie Name 1 - Inception </code></pre> </blockquote> <p>Genre Table</p> <blockquote> <pre><code>Genre ID - Genre Name 1 - Sci Fi </code></pre> </blockquote> <p>Actor Table</p> <blockquote> <pre><code>Actor ID - Actor Name 1 - Leonardo Di Caprio 2 - Ellen Page </code></pre> </blockquote> <p>Genre Relationships Table</p> <blockquote> <pre><code>Movie ID - Genre ID 1 - 1 </code></pre> </blockquote> <p>Actor Relationships Table</p> <blockquote> <pre><code>Movie ID - Actor ID 1 - 1 1 - 2 </code></pre> </blockquote> <p>If it was just the genres then I could do this by hand but as there are thousands of movies and actors I am struggling to come up with a simple approach to convert all this data. </p> <p>I have a csv dump of all the data and figure it could be done by using a php script to import it in this format or I don't know if it's possible to run SQL commands to sort the data this way (the database has over 200,000 movies). </p> <p>Any hints or ideas on how to accomplish this would be much appreciated!</p> </div>

在Json中使用[] struct

<div class="post-text" itemprop="text"> <p>I'm trying to parse JSON to a []struct, the JSON is retrieved from <a href="https://api.github.com/events" rel="nofollow">https://api.github.com/events</a> However when I try to access each struct within the array I get an error:</p> <pre><code>type GITHUB_EVENT does not support indexing </code></pre> <p>How am I able to access each struct within the array?</p> <pre><code>func httpGetEvents() { eventDataRAW := httpPageGet("https://api.github.com/events", true) eventDataJSON := new(GITHUB_EVENT) _ = json.Unmarshal([]byte(eventDataRAW), &amp;eventDataJSON) fmt.Println(eventDataJSON[0].Id) } //--------------------------------------------------------------------------------------// type GITHUB_EVENT []struct { Id string `json:"id"` Type string `json:"type"` Actor struct { Id int `json:"id"` Login string `json:"login"` GravatarId string `json:"gravatar_id"` Url string `json:"url"` AvatarUrl string `json:"avatar_url"` } `json:"actor"` Repo struct { Id int `json:"id"` Name string `json:"name"` Url string `json:"url"` } `json:"repo"` Payload struct { PushId int `json:"push_id"` Size int `json:"size"` DistinctSize int `json:"distinct_size"` Ref string `json:"ref"` Head string `json:"head"` Before string `json:"before"` Commits []struct { Sha string `json:"sha"` Author struct { Email string `json:"email"` Name string `json:"name"` } `json:"author"` Message string `json:"message"` Distinct bool `json:"distinct"` Url string `json:"url"` } `json:"commits"` } `json:"payload"` Public bool `json:"public"` CreatedAt string `json:"created_at"` } </code></pre> </div>

大学四年自学走来,这些私藏的实用工具/学习网站我贡献出来了

大学四年,看课本是不可能一直看课本的了,对于学习,特别是自学,善于搜索网上的一些资源来辅助,还是非常有必要的,下面我就把这几年私藏的各种资源,网站贡献出来给你们。主要有:电子书搜索、实用工具、在线视频学习网站、非视频学习网站、软件下载、面试/求职必备网站。 注意:文中提到的所有资源,文末我都给你整理好了,你们只管拿去,如果觉得不错,转发、分享就是最大的支持了。 一、电子书搜索 对于大部分程序员...

在中国程序员是青春饭吗?

今年,我也32了 ,为了不给大家误导,咨询了猎头、圈内好友,以及年过35岁的几位老程序员……舍了老脸去揭人家伤疤……希望能给大家以帮助,记得帮我点赞哦。 目录: 你以为的人生 一次又一次的伤害 猎头界的真相 如何应对互联网行业的「中年危机」 一、你以为的人生 刚入行时,拿着傲人的工资,想着好好干,以为我们的人生是这样的: 等真到了那一天,你会发现,你的人生很可能是这样的: ...

Java基础知识面试题(2020最新版)

文章目录Java概述何为编程什么是Javajdk1.5之后的三大版本JVM、JRE和JDK的关系什么是跨平台性?原理是什么Java语言有哪些特点什么是字节码?采用字节码的最大好处是什么什么是Java程序的主类?应用程序和小程序的主类有何不同?Java应用程序与小程序之间有那些差别?Java和C++的区别Oracle JDK 和 OpenJDK 的对比基础语法数据类型Java有哪些数据类型switc...

我以为我学懂了数据结构,直到看了这个导图才发现,我错了

数据结构与算法思维导图

String s = new String(" a ") 到底产生几个对象?

老生常谈的一个梗,到2020了还在争论,你们一天天的,哎哎哎,我不是针对你一个,我是说在座的各位都是人才! 上图红色的这3个箭头,对于通过new产生一个字符串(”宜春”)时,会先去常量池中查找是否已经有了”宜春”对象,如果没有则在常量池中创建一个此字符串对象,然后堆中再创建一个常量池中此”宜春”对象的拷贝对象。 也就是说准确答案是产生了一个或两个对象,如果常量池中原来没有 ”宜春” ,就是两个。...

技术大佬:我去,你写的 switch 语句也太老土了吧

昨天早上通过远程的方式 review 了两名新来同事的代码,大部分代码都写得很漂亮,严谨的同时注释也很到位,这令我非常满意。但当我看到他们当中有一个人写的 switch 语句时,还是忍不住破口大骂:“我擦,小王,你丫写的 switch 语句也太老土了吧!” 来看看小王写的代码吧,看完不要骂我装逼啊。 private static String createPlayer(PlayerTypes p...

Linux面试题(2020最新版)

文章目录Linux 概述什么是LinuxUnix和Linux有什么区别?什么是 Linux 内核?Linux的基本组件是什么?Linux 的体系结构BASH和DOS之间的基本区别是什么?Linux 开机启动过程?Linux系统缺省的运行级别?Linux 使用的进程间通信方式?Linux 有哪些系统日志文件?Linux系统安装多个桌面环境有帮助吗?什么是交换空间?什么是root帐户什么是LILO?什...

Linux命令学习神器!命令看不懂直接给你解释!

大家都知道,Linux 系统有非常多的命令,而且每个命令又有非常多的用法,想要全部记住所有命令的所有用法,恐怕是一件不可能完成的任务。 一般情况下,我们学习一个命令时,要么直接百度去搜索它的用法,要么就直接用 man 命令去查看守冗长的帮助手册。这两个都可以实现我们的目标,但有没有更简便的方式呢? 答案是必须有的!今天给大家推荐一款有趣而实用学习神器 — kmdr,让你解锁 Linux 学习新姿势...

和黑客斗争的 6 天!

互联网公司工作,很难避免不和黑客们打交道,我呆过的两家互联网公司,几乎每月每天每分钟都有黑客在公司网站上扫描。有的是寻找 Sql 注入的缺口,有的是寻找线上服务器可能存在的漏洞,大部分都...

史上最全的 python 基础知识汇总篇,没有比这再全面的了,建议收藏

网友们有福了,小编终于把基础篇的内容全部涉略了一遍,这是一篇关于基础知识的汇总的文章,请朋友们收下,不用客气,不过文章篇幅肯能会有点长,耐心阅读吧爬虫(七十)多进程multiproces...

讲一个程序员如何副业月赚三万的真实故事

loonggg读完需要3分钟速读仅需 1 分钟大家好,我是你们的校长。我之前讲过,这年头,只要肯动脑,肯行动,程序员凭借自己的技术,赚钱的方式还是有很多种的。仅仅靠在公司出卖自己的劳动时...

女程序员,为什么比男程序员少???

昨天看到一档综艺节目,讨论了两个话题:(1)中国学生的数学成绩,平均下来看,会比国外好?为什么?(2)男生的数学成绩,平均下来看,会比女生好?为什么?同时,我又联想到了一个技术圈经常讨...

85后蒋凡:28岁实现财务自由、34岁成为阿里万亿电商帝国双掌门,他的人生底层逻辑是什么?...

蒋凡是何许人也? 2017年12月27日,在入职4年时间里,蒋凡开挂般坐上了淘宝总裁位置。 为此,时任阿里CEO张勇在任命书中力赞: 蒋凡加入阿里,始终保持创业者的冲劲,有敏锐的...

总结了 150 余个神奇网站,你不来瞅瞅吗?

原博客再更新,可能就没了,之后将持续更新本篇博客。

副业收入是我做程序媛的3倍,工作外的B面人生是怎样的?

提到“程序员”,多数人脑海里首先想到的大约是:为人木讷、薪水超高、工作枯燥…… 然而,当离开工作岗位,撕去层层标签,脱下“程序员”这身外套,有的人生动又有趣,马上展现出了完全不同的A/B面人生! 不论是简单的爱好,还是正经的副业,他们都干得同样出色。偶尔,还能和程序员的特质结合,产生奇妙的“化学反应”。 @Charlotte:平日素颜示人,周末美妆博主 大家都以为程序媛也个个不修边幅,但我们也许...

MySQL数据库面试题(2020最新版)

文章目录数据库基础知识为什么要使用数据库什么是SQL?什么是MySQL?数据库三大范式是什么mysql有关权限的表都有哪几个MySQL的binlog有有几种录入格式?分别有什么区别?数据类型mysql有哪些数据类型引擎MySQL存储引擎MyISAM与InnoDB区别MyISAM索引与InnoDB索引的区别?InnoDB引擎的4大特性存储引擎选择索引什么是索引?索引有哪些优缺点?索引使用场景(重点)...

新一代神器STM32CubeMonitor介绍、下载、安装和使用教程

关注、星标公众号,不错过精彩内容作者:黄工公众号:strongerHuang最近ST官网悄悄新上线了一款比较强大的工具:STM32CubeMonitor V1.0.0。经过我研究和使用之...

如果你是老板,你会不会踢了这样的员工?

有个好朋友ZS,是技术总监,昨天问我:“有一个老下属,跟了我很多年,做事勤勤恳恳,主动性也很好。但随着公司的发展,他的进步速度,跟不上团队的步伐了,有点...

我入职阿里后,才知道原来简历这么写

私下里,有不少读者问我:“二哥,如何才能写出一份专业的技术简历呢?我总感觉自己写的简历太烂了,所以投了无数份,都石沉大海了。”说实话,我自己好多年没有写过简历了,但我认识的一个同行,他在阿里,给我说了一些他当年写简历的方法论,我感觉太牛逼了,实在是忍不住,就分享了出来,希望能够帮助到你。 01、简历的本质 作为简历的撰写者,你必须要搞清楚一点,简历的本质是什么,它就是为了来销售你的价值主张的。往深...

大学一路走来,学习互联网全靠这几个网站,最终拿下了一把offer

大佬原来都是这样炼成的

离职半年了,老东家又发 offer,回不回?

有小伙伴问松哥这个问题,他在上海某公司,在离职了几个月后,前公司的领导联系到他,希望他能够返聘回去,他很纠结要不要回去? 俗话说好马不吃回头草,但是这个小伙伴既然感到纠结了,我觉得至少说明了两个问题:1.曾经的公司还不错;2.现在的日子也不是很如意。否则应该就不会纠结了。 老实说,松哥之前也有过类似的经历,今天就来和小伙伴们聊聊回头草到底吃不吃。 首先一个基本观点,就是离职了也没必要和老东家弄的苦...

为什么你不想学习?只想玩?人是如何一步一步废掉的

不知道是不是只有我这样子,还是你们也有过类似的经历。 上学的时候总有很多光辉历史,学年名列前茅,或者单科目大佬,但是虽然慢慢地长大了,你开始懈怠了,开始废掉了。。。 什么?你说不知道具体的情况是怎么样的? 我来告诉你: 你常常潜意识里或者心理觉得,自己真正的生活或者奋斗还没有开始。总是幻想着自己还拥有大把时间,还有无限的可能,自己还能逆风翻盘,只不是自己还没开始罢了,自己以后肯定会变得特别厉害...

什么时候跳槽,为什么离职,你想好了么?

都是出来打工的,多为自己着想

为什么程序员做外包会被瞧不起?

二哥,有个事想询问下您的意见,您觉得应届生值得去外包吗?公司虽然挺大的,中xx,但待遇感觉挺低,马上要报到,挺纠结的。

当HR压你价,说你只值7K,你该怎么回答?

当HR压你价,说你只值7K时,你可以流畅地回答,记住,是流畅,不能犹豫。 礼貌地说:“7K是吗?了解了。嗯~其实我对贵司的面试官印象很好。只不过,现在我的手头上已经有一份11K的offer。来面试,主要也是自己对贵司挺有兴趣的,所以过来看看……”(未完) 这段话主要是陪HR互诈的同时,从公司兴趣,公司职员印象上,都给予对方正面的肯定,既能提升HR的好感度,又能让谈判气氛融洽,为后面的发挥留足空间。...

面试阿里p7,被按在地上摩擦,鬼知道我经历了什么?

面试阿里p7被问到的问题(当时我只知道第一个):@Conditional是做什么的?@Conditional多个条件是什么逻辑关系?条件判断在什么时候执...

你期望月薪4万,出门右拐,不送,这几个点,你也就是个初级的水平

先来看几个问题通过注解的方式注入依赖对象,介绍一下你知道的几种方式@Autowired和@Resource有何区别说一下@Autowired查找候选者的...

面试了一个 31 岁程序员,让我有所触动,30岁以上的程序员该何去何从?

最近面试了一个31岁8年经验的程序猿,让我有点感慨,大龄程序猿该何去何从。

大三实习生,字节跳动面经分享,已拿Offer

说实话,自己的算法,我一个不会,太难了吧

程序员垃圾简历长什么样?

已经连续五年参加大厂校招、社招的技术面试工作,简历看的不下于万份 这篇文章会用实例告诉你,什么是差的程序员简历! 疫情快要结束了,各个公司也都开始春招了,作为即将红遍大江南北的新晋UP主,那当然要为小伙伴们做点事(手动狗头)。 就在公众号里公开征简历,义务帮大家看,并一一点评。《启舰:春招在即,义务帮大家看看简历吧》 一石激起千层浪,三天收到两百多封简历。 花光了两个星期的所有空闲时...

立即提问
相关内容推荐