遗传算法编码方式整数编码_如何编码卫星算法并从头开始烹制西班牙海鲜饭

本文通过制作西班牙海鲜饭的比喻,介绍了如何运用“最重要的问题”方法来解决问题。作者以计算卫星轨道周期为例,展示了如何从零开始,通过遗传算法的整数编码方式来理解和解决问题,同时提醒读者在面对复杂问题时,可以通过拆解成小问题来简化任务。
摘要由CSDN通过智能技术生成

遗传算法编码方式整数编码

What if I told you that by the end of this article, you’d be able to calculate the orbital period of satellites around Earth using their average altitudes and… You tuned out already, didn’t you?

如果我告诉您到本文结尾时,您将能够使用它们的平均高度来计算地球周围卫星的轨道周期,并且……您已经调好了,不是吗?

Okay, how about this: I’m going to teach you how to make paella!

好的,这怎么样:我将教你如何制作西班牙海鲜饭!

And you’ll learn how to write a function that does the stuff I mentioned above, just like I did for this freeCodeCamp challenge.

将学习如何写,做我上面提到的东西的功能,就像我做了这个freeCodeCamp挑战

I promise there’s an overarching moral lesson that will benefit you every day for the rest of your life. Or, at least, feed you for one night. Let’s get started.

我保证会有一个全面的道德课,对您的余生每天都将有益。 或者,至少要养活你一晚。 让我们开始吧。

我对海鲜饭的唯一了解是表情符号 (The only thing I know about paella is that it’s an emoticon)

One of my favorite things about living in the world today is that it’s totally fine to know next-to-nothing about something. A hundred years ago, you might have gone your whole life not knowing anything more about paella other than that it’s an emoticon.* But today? You can simply look it up.

我的一个今天生活在世界上最喜欢的事情是,它是完全好下一个要一无所知的东西就知道了。 一百年前,您可能一辈子都不知道西班牙海鲜饭,而是表情符号。*但是今天呢? 您可以简单地查找它

*That was a joke.

*那是个玩笑。

As with all things in life, when we are unsure, we turn to the Internet. In this case, the entry for paella on Wikipedia, which reads:

与生活中的所有事物一样,当我们不确定时,我们将转向Internet。 在这种情况下,维基百科上西班牙海鲜饭的条目显示为:

“Paella …is a Valencian rice dish. Paella has ancient roots, but its modern form originated in the mid-19th century near the Albufera lagoon on the east coast of Spain adjacent to the city of Valencia. Many non-Spaniards view paella as Spain’s national dish, but most Spaniards consider it to be a regional Valencian dish. Valencians, in turn, regard paella as one of their identifying symbols.

“海鲜饭……是巴伦西亚的米饭。 西班牙海鲜饭具有悠久的历史,但其现代形式起源于19世纪中叶,毗邻西班牙东海岸瓦伦西亚市的阿尔布费拉泻湖。 许多非西班牙人将西班牙海鲜饭视为西班牙的国菜,但大多数西班牙人都将西班牙海鲜饭视为巴伦西亚自治区的美食。 反过来,巴伦西亚人则认为海鲜饭是他们的识别标志之一。

Types of paella include Valencian paella, vegetarian/vegan paella (Spanish: paella de verduras), seafood paella (Spanish: paella de marisco), and mixed paella (Spanish: paella mixta), among many others.” — Wikipedia

西班牙海鲜饭的类型包括巴伦西亚海鲜饭,素食/纯素食海鲜饭(西班牙语:西班牙海鲜饭),海鲜海鲜饭(西班牙西班牙海鲜饭)和混合海鲜饭(西班牙西班牙海鲜饭),等等。” 维基百科

At this point, you’re probably full of questions. Do I need to talk to a Valencian? Should I take an online course on the history of Spain? What type of paella should I try to make? What is the common opinion of modern chefs when it comes to paella types?

在这一点上,您可能充满了疑问。 我需要和巴伦西亚人谈谈吗? 我应该在线上学习西班牙的历史吗? 我应该尝试制作哪种西班牙海鲜饭? 现代厨师对西班牙海鲜饭类型的普遍看法是什么?

If you set out to answer all these questions, one thing is certain: you’ll never end up actually making paella. You’ll spend hours upon hours typing questions into search engines, and years later you’ll wake up with a Masters in Valencian Cuisine.

如果您打算回答所有这些问题,那么可以肯定的是:您永远不会真正制作出西班牙海鲜饭。 您将花费数小时的时间在搜索引擎中输入问题,几年后,您将获得巴伦西亚美食大师的殊荣。

“最重要的问题”方法 (The “Most Important Question” method)

When I talk to myself out loud in public (doesn’t everyone?) I refer to this as “MIQ” (rhymes with “Nick”). I also imagine MIQ to be a rather crunchy and quite adorable, anthropomorphized tortilla chip. Couldn’t tell you why.

当我在公开场合大声自言自语(不是所有人吗?)时,我将其称为“ MIQ”(与“ Nick”相对应的押韵)。 我还认为MIQ是一种相当松脆,非常可爱的,拟人化的玉米粉圆饼。 无法告诉你为什么。

MIQ swings his crunchy triangular body around to point me in the right direction. The right direction always takes the form of the most important question that you need to ask yourself at any stage of problem solving. The first most important question is always this:

MIQ摆动着他那松脆的三角形身体,将我指向正确的方向。 正确的方向总是采取在解决问题的任何阶段都需要问自己的最重要问题的形式。 第一个最重要的问题始终是:

What is the scope of the objective I want to achieve?

我要达到的目标范围是什么?

Well, you want to make paella.

好吧,你想做海鲜饭。

The next MIQ then becomes: how much do I actually need to know about paella in order to start making it?

接下来的下一个MIQ成为:要开始制作西班牙海鲜饭,我实际上需要了解多少?

You’ve heard this advice before: any big problem can be broken down into multiple, but more manageable, bite-size problems. In this little constellation of bite-size problems, there’s only one that you need to solve in order to get most of the way to a complete solution.

您以前曾听过这样的建议:任何大问题都可以分解为多个但更易于管理的小问题。 在这个很小的问题中,只有一小部分需要解决,才能获得完整解决方案的大部分方法

In the case of making paella, we need a recipe. That’s a bite-size problem that a search engine can solve for us:

如果要制作西班牙海鲜饭,我们需要一个食谱。 这是搜索引擎可以为我们解决的一个小问题:

Simple Paella Recipe

简单的西班牙海鲜饭食谱

In a medium bowl, mix together 2 tablespoons olive oil, paprika, oregano, and salt and pepper. Stir in chicken pieces to coat. Cover, and refrigerate.
在一个中等大小的碗中,将2汤匙橄榄油,辣椒粉,牛至,盐和胡椒粉混合在一起。 搅拌入鸡块上衣。 盖好,然后冷藏。
Heat 2 tablespoons olive oil in a large skillet or paella pan over medium heat. Stir in garlic, red pepper flakes, and rice. Cook, stirring, to coat rice with oil, about 3 minutes. Stir in saffron threads, bay leaf, parsley, chicken stock, and lemon zest. Bring to a boil, cover, and reduce heat to medium low. Simmer 20 minutes.
在大火锅或西班牙海鲜饭中用中火加热2汤匙橄榄油。 加入大蒜,红辣椒片和米饭。 煮熟,搅拌,在米饭上涂油约3分钟。 加入藏红花线,月桂叶,欧芹,鸡汤和柠檬皮。 烧开,盖上锅盖,将热量降低至中低水平。 炖20分钟。
Meanwhile, heat 2 tablespoons olive oil in a separate skillet over medium heat. Stir in marinated chicken and onion; cook 5 minutes. Stir in bell pepper and sausage; cook 5 minutes. Stir in shrimp; cook, turning the shrimp, until both sides are pink.
同时,在单独的煎锅中用中火加热2汤匙橄榄油。 放入腌制的鸡肉和洋葱搅拌; 煮5分钟。 加入甜椒和香肠搅拌; 煮5分钟。 搅拌入虾; 煮,翻虾,直到两面都是粉红色。

Spread rice mixture onto a serving tray. Top with meat and seafood mixture. (allrecipes.com)

将大米混合物撒到托盘上。 上面放肉和海鲜混合物。 ( allrecipes.com )

And voila! Believe it or not, we’re most of the way there already.

! 信不信由你,我们最的方式存在了。

Having a set of step-by-step instructions that are easy to understand is really most of the work. All that’s left is to go through the motions of gathering the ingredients and then making paella. From this point on, your MIQs may become fewer and farther between, and they may slowly decrease in importance in relation to the overall problem. (Where do I buy paprika? How do I know when sausage is cooked? How do I set the timer on my phone for 20 minutes? How do I stop thinking about this delicious smell? Which Instagram filter best captures the ecstasy of this paella right now?)

拥有大部分易于理解的逐步说明实际上是大多数工作。 剩下的就是经过收集食材然后制作海鲜饭的动作。 从那时起,您的MIQ可能会越来越少,并且相对于整个问题的重要性可能会逐渐降低。 (我在哪里买的辣椒粉?如何知道香肠煮熟的时间?如何在手机上设置计时器20分钟?如何停止思考这种美味的味道?哪个Instagram过滤器最能捕捉到这种西班牙海鲜饭的迷魂药,对吗?现在?)

我仍然对计算卫星的轨道周期一无所知 (I still know nothing about calculating the orbital periods of satellites)

Okay. Let’s examine the problem:

好的。 让我们检查一下问题:

Return a new array that transforms the element’s average altitude into their orbital periods.
返回一个新数组,该数组将元素的平均高度转换为其轨道周期。
The array will contain objects in the format {name: ‘name’, avgAlt: avgAlt}.
该数组将包含{name:'name',avgAlt:avgAlt}格式的对象。
You can read about orbital periods on wikipedia.
您可以在Wikipedia上了解有关轨道周期的信息。
The values should be rounded to the nearest whole number. The body being orbited is Earth.
这些值应四舍五入到最接近的整数。 被绕行的物体是地球。
The radius of the earth is 6367.4447 kilometers, and the Gravitational Mass (GM) value of earth is 398600.4418 km3s-2.
地球半径为6367.4447公里,地球的重力质量(GM)值为398600.4418 km3s-2。

orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}]) should return [{name: "sputnik", orbitalPeriod: 86400}].

orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}])应该返回[{name: "sputnik", orbitalPeriod: 86400}].

Well, as it turns out, in order to calculate the orbital period of satellites, we also need a recipe. Amazing, the things you can find on the internet these days.

好吧,事实证明,为了计算卫星的轨道周期,我们还需要一个配方。 令人惊讶的是,这些天您可以在互联网上找到这些东西。

Courtesy of dummies.com (yup! #noshame), here’s our recipe:

dummies.com提供 (是的!#noshame),这是我们的食谱:

That might look pretty complicated. But as we’ve already seen, we just need to answer the next MIQ: how much do I actually need to know about this formula in order to start using it?

这看起来可能很复杂。 但是,正如我们已经看到的那样,我们只需要回答下一个MIQ:为了开始使用它,我实际上需要了解多少这个公式?

In the case of this challenge, not too much. We’re already given earthRadius, and avgAlt is part of our arguments object. Together, they form the radius, r. With a couple search queries and some mental time-travel to your elementary math class, we can describe this formula in a smattering of English:

在这种挑战的情况下,不要太多。 我们已经获得了earthRadius ,而avgAlt是我们的arguments对象的一部分。 它们共同形成半径r 。 通过几次搜索查询以及对您的基础数学课的一些脑力旅行,我们可以用一点英语来描述此公式:

T, the orbital period, equals 2 multiplied by Pi, in turn multiplied by the square root of the radius, r cubed, divided by the gravitational mass, GM.

轨道周期T等于2乘以Pi,再乘以半径r的平方根r cube,再除以重力质量GM

JavaScript has a Math.PI property, as well as Math.sqrt() function and Math.pow() function. Using those combined with simple calculation, we can represent this equation in a single line assigned to a variable:

JavaScript具有Math.PI属性,以及Math.sqrt()函数和Math.pow()函数。 通过将它们与简单的计算结合使用,我们可以在分配给变量的单行中表示该方程:

var orbitalPeriod = 2 * Math.PI * (Math.sqrt(Math.pow((earthRadius + avgAlt), 3) / GM));

From the inside out:

从里到外:

  1. Add earthRadius and avgAlt

    添加earthRadiusavgAlt

  2. Cube the result of step 1

    多维数据集第1步的结果
  3. Divide the result of step 2 by GM

    将步骤2的结果除以GM
  4. Take the square root of the result of step 3

    取步骤3的结果的平方根
  5. Multiply 2 times Pi times the result of step 4

    将Pi乘以2乘以第4步的结果
  6. Assign the returned value to orbitalPeriod

    将返回值分配给orbitalPeriod

Believe it or not, we’re already most of the way there.

信不信由你,我们已经到了那里。

The next MIQ for this challenge is to take the arguments object, extract the information we need, and return the result of our equation in the required format. There are multiple ways to do this, but I’m happy with a straightforward for loop:

此挑战的下一个MIQ是采用arguments对象,提取我们需要的信息,并以所需格式返回方程式的结果。 有多种方法可以做到这一点,但我对简单的for循环感到满意:

function orbitalPeriod(arr) {
   var resultArr = [];
   for (var teapot = 0; teapot < arguments[0].length; teapot++) {
     var GM = 398600.4418;
     var earthRadius = 6367.4447;
     var avgAlt = arguments[0][teapot]['avgAlt'];
     var name = arguments[0][teapot]['name'];
     var orbitalPeriod = 2 * Math.PI * (Math.sqrt(Math.pow((earthRadius + avgAlt), 3) / GM));
     var result = {
       name: name,
       orbitalPeriod: Math.round(orbitalPeriod)
     }
     resultArr.push(result);
   }
   return resultArr; 
}

If you need a refresher on iterating through arrays, have a look at my article on iterating, featuring breakfast arrays! (5 minutes read)

如果您需要遍历数组的复习,请看一下我的有关早餐数组的迭代文章 ! (阅读5分钟)

Don’t look now, but you just gained the ability to calculate the orbital period of satellites. You could even do it while making paella, if you wanted to. Seriously. Put it on your resume.

现在不要看,但是您刚刚获得了计算卫星轨道周期的功能。 如果愿意,您甚至可以制作西班牙海鲜饭的时候这样做。 说真的 把它放在你的简历上。

Tl; dr:总体道德课 (Tl;dr: the overarching moral lesson)

Whether it’s cooking, coding, or anything else, problems may at first seem confusing, insurmountable, or downright boring. If you’re faced with such a challenge, just remember: they’re a lot more digestible with a side of bite-sized MIQ chips.

无论是烹饪,编码还是其他任何事情,问题乍一看似乎令人困惑,难以克服或无聊。 如果您面临这样的挑战,请记住:一口大小的MIQ芯片可简化它们的消化。

Thanks for reading!

谢谢阅读!

If you liked this article, I’d love to know! You can find this and other articles explaining coding concepts with food on my blog.

如果您喜欢这篇文章,我很想知道! 您可以在我的博客上找到这篇和其他文章,其中包括用食物解释编码概念的文章。

翻译自: https://www.freecodecamp.org/news/how-to-build-a-program-or-make-dinner-from-scratch-9eb1263ecdbc/

遗传算法编码方式整数编码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值